Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue/2743 - remove the flush for Adapt.wait #2934

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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