-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue/1814: turn Adapt wait into hard API
- Loading branch information
1 parent
6d830ad
commit 20b3dd5
Showing
5 changed files
with
177 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
define(function() { | ||
|
||
var Wait = Backbone.Controller.extend({ | ||
|
||
initialize: function() { | ||
_.bindAll(this, "begin", "end"); | ||
}, | ||
|
||
_waitCount: 0, | ||
_callbackHandle: null, | ||
|
||
/** | ||
* Returns true if there are items in the waiting count. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isWaiting: function() { | ||
return (this._waitCount !== 0); | ||
}, | ||
|
||
/** | ||
* Add one item to the waiting count. | ||
* | ||
* @return {Object} | ||
*/ | ||
begin: function() { | ||
|
||
if (!this.isWaiting()) { | ||
this.trigger("wait"); | ||
} | ||
|
||
this._waitCount++; | ||
|
||
if (this._callbackHandle) { | ||
clearTimeout(this._callbackHandle); | ||
this._callbackHandle = null; | ||
} | ||
|
||
return this; | ||
|
||
}, | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
this._waitCount--; | ||
|
||
if (this.isWaiting()) { | ||
return this; | ||
} | ||
|
||
if (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} | ||
*/ | ||
queue: function(callback) { | ||
|
||
this.begin(); | ||
this.once("ready", callback); | ||
this.end(); | ||
|
||
return this; | ||
|
||
}, | ||
|
||
/** | ||
* Wait for this asynchonous function to execute before triggering ready event. | ||
* | ||
* @param {Function} callback [ Function to execute whilst holding queued callback. Once complete run first argiument, done(). ] | ||
* @return {Object} | ||
*/ | ||
for: function(callback) { | ||
|
||
this.begin(); | ||
_.defer(function() { | ||
callback(this.end); | ||
}.bind(this)); | ||
|
||
return this; | ||
|
||
} | ||
|
||
}); | ||
|
||
return Wait; | ||
|
||
}); |