index.js 826 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. function ready(flagOrFunction) {
  3. this._ready = !!this._ready;
  4. this._readyCallbacks = this._readyCallbacks || [];
  5. if (arguments.length === 0) {
  6. // return a promise
  7. // support `this.ready().then(onready);` and `yield this.ready()`;
  8. return new Promise(function (resolve) {
  9. if (this._ready) {
  10. return resolve();
  11. }
  12. this._readyCallbacks.push(resolve);
  13. }.bind(this));
  14. } else if (typeof flagOrFunction === 'function') {
  15. this._readyCallbacks.push(flagOrFunction);
  16. } else {
  17. this._ready = !!flagOrFunction;
  18. }
  19. if (this._ready) {
  20. this._readyCallbacks.splice(0, Infinity).forEach(function(callback) {
  21. process.nextTick(callback);
  22. });
  23. }
  24. }
  25. function mixin(object) {
  26. object.ready = ready;
  27. }
  28. module.exports = mixin;
  29. module.exports.mixin = mixin;