Skip to content

Commit

Permalink
remove the flush for Adapt.wait. (#2934)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielghost authored Oct 27, 2020
1 parent 6addb16 commit bb5e71e
Showing 1 changed file with 30 additions and 85 deletions.
115 changes: 30 additions & 85 deletions src/core/js/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,29 @@ define(function() {

var Wait = Backbone.Controller.extend({

_waitCount: 0,
_callbackHandle: null,

initialize: function() {
_.bindAll(this, 'begin', 'end');
},

_waitCount: 0,
_callbackHandle: null,
_timeoutHandlerId: null,
_timeoutInSeconds: 7,

/**
* Returns true if there are items in the waiting count.
*
* @return {Boolean}
*/
* Returns true if there are items in the waiting count.
*
* @return {Boolean}
*/
isWaiting: function() {
return (this._waitCount !== 0);
},

/**
* Starts or re-starts a timer to ensure that pending calls to end()
* are actually executed after a timeout period.
*/
startTimer: function() {
this.stopTimer();

this._timeoutHandlerId = setInterval(function() {
// Flush Adapt.wait due to timeout
while (this._waitCount > 0) {
// Trigger an end() for anything waiting.
this.end();
}

if (this._waitCount === 0) {
this.stopTimer();
}
}.bind(this), this._timeoutInSeconds * 1000);
},

/**
* Clears the timer.
*/
stopTimer: function() {
if (this._timeoutHandlerId) {
clearInterval(this._timeoutHandlerId);
}
},

/**
* Add one item to the waiting count.
*
* @return {Object}
*/
* Add one item to the waiting count.
*
* @return {Object}
*/
begin: function() {

if (!this.isWaiting()) {
this.trigger('wait');
}
if (!this.isWaiting()) this.trigger('wait');

this._waitCount++;

Expand All @@ -67,58 +33,39 @@ define(function() {
this._callbackHandle = null;
}

this.startTimer();

return this;

},

/**
* Remove an item from the waiting count and trigger ready asynchronously if no more items are waiting.
*
* @return {Object}
*/
* Remove an item from the waiting count and trigger ready asynchronously if no more items are waiting.
*
* @return {Object}
*/
end: function() {

if (!this.isWaiting()) {
return this;
}
if (!this.isWaiting()) return this;

this._waitCount--;

if (this._waitCount === 0) {
this.stopTimer();
}

if (this.isWaiting()) {
return this;
}

if (this._callbackHandle) {
return this;
}
if (this.isWaiting() || this._callbackHandle) return this;

this._callbackHandle = setTimeout(function() {

this._callbackHandle = null;
this.trigger('ready');

}.bind(this), 0);

return this;

},

/**
* Queue this function until all open waits have been ended.
*
* @param {Function} [callback]
* @return {Object|Promise}
*/
* Queue this function until all open waits have been ended.
*
* @param {Function} [callback]
* @return {Object|Promise}
*/
queue: function(callback) {

if (!callback) {
this.begin();

return new Promise(resolve => {
this.once('ready', resolve);
this.end();
Expand All @@ -130,24 +77,22 @@ define(function() {
this.end();

return this;

},

/**
* Wait for this asynchronous function to execute before triggering ready event.
*
* @param {Function} callback [ Function to execute whilst holding queued callback. Once complete run first argument, done(). ]
* @return {Object}
*/
* Wait for this asynchronous function to execute before triggering ready event.
*
* @param {Function} callback [ Function to execute whilst holding queued callback. Once complete run first argument, done(). ]
* @return {Object}
*/
for: function(callback) {

this.begin();

_.defer(function() {
callback(_.once(this.end));
}.bind(this));

return this;

}

});
Expand Down

0 comments on commit bb5e71e

Please sign in to comment.