重新进入安全异步功能执行

时间:2014-08-26 08:27:09

标签: javascript coffeescript windows-runtime promise winrt-async

我有一个saveAsync方法,可以从应用的其他几个部分调用。其中一些代码路径不是来自主UI线程,而是来自其他异步操作,例如从套接字/文件中读取数据。

在我的saveAsync功能中,我致电WinRT StorageFile信息流功能。一次只允许一个文件的写入者。所以我必须排队多次连续调用我的saveAsync函数并按顺序执行它们。这是我现在使用的代码:

SaveableMixin = {
  savesPending: 0,
  saveAsync: function() {
    self = this
    logger.debug("Save requested (_cachedFile=" + self._cachedFile + ")");
    if (self.saving) {
      self.savesPending += 1;
      logger.debug("saveAsync: already saving, " + self.savesPending + " pending now");
      return self.saving;
    }
    return self.saving = getFileNameAsync(self.serialNumber).then(function(file) {
      logger.debug("saveAsync: saving...");
      return self.node.ownerDocument.saveToFileAsync(file);
    }).then(null, (function(error) {
      return logger.error("saveAsync: failed but " + self.savesPending + " saves pending - good luck!", error);
    }).then((function() {
      logger.debug("saveAsync: saved and " + self.savesPending + " pending");
      self.saving = null;
      if (self.savesPending) {
        self.savesPending = 0;
        return self.saveAsync();
      }
    });
  }
}

CoffeeScript原始代码:

savesPending: 0
saveAsync: () ->
  logger.debug("Save requested (_cachedFile=#{@_cachedFile})")
  if @saving
    @savesPending += 1
    logger.debug("saveAsync: already saving, #{@savesPending} pending now")
    return @saving
  @saving = getCachedFile(@serialNumber)
  .then (file) =>
    logger.debug("saveAsync: saving...")
    @node.ownerDocument.saveToFileAsync(file)
  .then null, (error) =>
    logger.error("saveAsync: failed with inspection #{@serialNumber} but #{@savesPending} saves pending - good luck!", error)
  .then =>
    logger.debug("saveAsync: saved and #{@savesPending} pending")
    @saving = null
    if @savesPending
      @savesPending = 0
      return @saveAsync()

正如您所看到的,我记得上次saving承诺知道某个操作正在运行。然而,它可能发生,我仍然得到一个Access Denied异常原因,某些代码路径设法将saving视为空,因此现在考虑保存操作正在进行中。当第一个调用者即将完成第二个异步操作(例如套接字读取)完成并且第一个调用者获得saveAsync变量时调用saving时,似乎会发生这种情况。分配

在C ++中,我会使用锁来防止这种情况。在JavaScript世界中可以做些什么?

更新

_saving: WinJS.Promise.as(),
_saves: 0,
saveAsync: function() {
    var currentSave;
    currentSave = this._saves;
    this._saves += 1;
    return this._saving = this._saving.then((function(_this) {
      return function() {
        logger.debug("saveAsync: get file for save " + currentSave);
        return getCachedFile(_this.serialNumber);
      };
    })(this)).then((function(_this) {
      return function(file) {
        logger.debug("saveAsync: got file for save " + currentSave + " ...");
        return _this.node.ownerDocument.saveToFileAsync(file);
      };
    })(this)).then(null, function(error) {
      logger.debug("saveAsync: error for " + currentSave);
    });

这会在调试器中产生以下输出:

2014-12-07 23:46:52,286 - [inspection - DEBUG] - saveAsync: get file for save 0
2014-12-07 23:46:52,289 - [inspection - DEBUG] - saveAsync: get file for save 0
2014-12-07 23:46:52,349 - [inspection - DEBUG] - saveAsync: got file for save 0 ...
2014-12-07 23:46:52,444 - [inspection - DEBUG] - saveAsync: got file for save 0 ...
2014-12-07 23:46:52,446 - [inspection - DEBUG] - saveAsync: get file for save 1
2014-12-07 23:46:52,447 - [inspection - DEBUG] - saveAsync: got file for save 1 ...

当然,这是对相关文件的双重访问,并与AV崩溃。

1 个答案:

答案 0 :(得分:1)

在JavaScript世界中,您将使用promises来做到这一点,一个承诺抽象出一系列异步操作,您可以将它用作队列。

SaveableMixin = {
  queue: WinJS.promise.as(), // empty start of queue
  saveAsync: function() {
    this.queue = this.queue.then(function(){ // update the queue
      return getFileNameAsync(self.serialNumber).then(function(file) {
          logger.debug("saveAsync: saving...");
          return self.node.ownerDocument.saveToFileAsync(file);
    })..then(null, (function(error) {
      return logger.error("saveAsync: failed good luck!", error);
    }).then((function() {
      logger.debug("saveAsync: saved and " + self.savesPending + " pending");
    });
  }
}