You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
826 B
35 lines
826 B
4 weeks ago
|
'use strict';
|
||
|
|
||
|
function ready(flagOrFunction) {
|
||
|
this._ready = !!this._ready;
|
||
|
this._readyCallbacks = this._readyCallbacks || [];
|
||
|
|
||
|
if (arguments.length === 0) {
|
||
|
// return a promise
|
||
|
// support `this.ready().then(onready);` and `yield this.ready()`;
|
||
|
return new Promise(function (resolve) {
|
||
|
if (this._ready) {
|
||
|
return resolve();
|
||
|
}
|
||
|
this._readyCallbacks.push(resolve);
|
||
|
}.bind(this));
|
||
|
} else if (typeof flagOrFunction === 'function') {
|
||
|
this._readyCallbacks.push(flagOrFunction);
|
||
|
} else {
|
||
|
this._ready = !!flagOrFunction;
|
||
|
}
|
||
|
|
||
|
if (this._ready) {
|
||
|
this._readyCallbacks.splice(0, Infinity).forEach(function(callback) {
|
||
|
process.nextTick(callback);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function mixin(object) {
|
||
|
object.ready = ready;
|
||
|
}
|
||
|
|
||
|
module.exports = mixin;
|
||
|
module.exports.mixin = mixin;
|