Skip to content

Commit

Permalink
Merge branch 'iwehrman/v2' into ps-next
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrbrandt committed Apr 23, 2014
2 parents c82a680 + fa9940b commit 82d85cf
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions lib/documentmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@
DocumentManager.prototype._newClosedDocumentIds = null;

/**
* Whether the set of currently open documents is being updated.
* If non-null, resolves once the set of open document IDs is finished updating
*
* @private
* @type {boolean}
* @type {?Promise}
*/
DocumentManager.prototype._openDocumentIdsUpdating = false;
DocumentManager.prototype._openDocumentIdsUpdatingPromise = null;

/**
* Whether the set of currently open documents needs to be updated.
Expand Down Expand Up @@ -305,17 +305,21 @@
/**
* Asynchronously reset the set of open document IDs.
*
* Only one instance of this method will execute at a time. Concurrent
* executions will result in the method running a second time after the
* first instance has finished.
*
* @private
* @return {!Promise} Resolves once the set of open document IDs has been
* updated.
*/
DocumentManager.prototype._resetOpenDocumentIDs = function () {
if (this._openDocumentIdsUpdating) {
if (this._openDocumentIdsUpdatingPromise) {
this._openDocumentIdsStale = true;
return;
return this._openDocumentIdsUpdatingPromise;
}

this._openDocumentIdsUpdating = true;

return this._generator.getOpenDocumentIDs()
var promise = this._generator.getOpenDocumentIDs()
.then(function (ids) {
var originalIds = Object.keys(this._openDocumentIds);

Expand All @@ -336,13 +340,25 @@
}
}, this);

this._openDocumentIdsUpdating = false;
// In the case that there is an additional pending call to _resetOpenDocumentIDs,
// then we will re-call this function synchronously below. In order to
// not hit the early return, we need to clear the variable holding a reference to
// the orignal promise. (Or, in the normal case, this is just cleaning up after
// ourselves.)
this._openDocumentIdsUpdatingPromise = null;

if (this._openDocumentIdsStale) {
this._openDocumentIdsStale = false;
this._resetOpenDocumentIDs().done();

// Returning a new promise in this "then" handler has the effect of not resolving
// the original promise until we're done with the next update.
return this._resetOpenDocumentIDs();
}
}.bind(this));

this._openDocumentIdsUpdatingPromise = promise;

return promise;
};

/**
Expand Down

0 comments on commit 82d85cf

Please sign in to comment.