async.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. var firstLineError;
  3. try {throw new Error(); } catch (e) {firstLineError = e;}
  4. var schedule = require("./schedule");
  5. var Queue = require("./queue");
  6. function Async() {
  7. this._customScheduler = false;
  8. this._isTickUsed = false;
  9. this._lateQueue = new Queue(16);
  10. this._normalQueue = new Queue(16);
  11. this._haveDrainedQueues = false;
  12. var self = this;
  13. this.drainQueues = function () {
  14. self._drainQueues();
  15. };
  16. this._schedule = schedule;
  17. }
  18. Async.prototype.setScheduler = function(fn) {
  19. var prev = this._schedule;
  20. this._schedule = fn;
  21. this._customScheduler = true;
  22. return prev;
  23. };
  24. Async.prototype.hasCustomScheduler = function() {
  25. return this._customScheduler;
  26. };
  27. Async.prototype.haveItemsQueued = function () {
  28. return this._isTickUsed || this._haveDrainedQueues;
  29. };
  30. Async.prototype.fatalError = function(e, isNode) {
  31. if (isNode) {
  32. process.stderr.write("Fatal " + (e instanceof Error ? e.stack : e) +
  33. "\n");
  34. process.exit(2);
  35. } else {
  36. this.throwLater(e);
  37. }
  38. };
  39. Async.prototype.throwLater = function(fn, arg) {
  40. if (arguments.length === 1) {
  41. arg = fn;
  42. fn = function () { throw arg; };
  43. }
  44. if (typeof setTimeout !== "undefined") {
  45. setTimeout(function() {
  46. fn(arg);
  47. }, 0);
  48. } else try {
  49. this._schedule(function() {
  50. fn(arg);
  51. });
  52. } catch (e) {
  53. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  54. }
  55. };
  56. function AsyncInvokeLater(fn, receiver, arg) {
  57. this._lateQueue.push(fn, receiver, arg);
  58. this._queueTick();
  59. }
  60. function AsyncInvoke(fn, receiver, arg) {
  61. this._normalQueue.push(fn, receiver, arg);
  62. this._queueTick();
  63. }
  64. function AsyncSettlePromises(promise) {
  65. this._normalQueue._pushOne(promise);
  66. this._queueTick();
  67. }
  68. Async.prototype.invokeLater = AsyncInvokeLater;
  69. Async.prototype.invoke = AsyncInvoke;
  70. Async.prototype.settlePromises = AsyncSettlePromises;
  71. function _drainQueue(queue) {
  72. while (queue.length() > 0) {
  73. _drainQueueStep(queue);
  74. }
  75. }
  76. function _drainQueueStep(queue) {
  77. var fn = queue.shift();
  78. if (typeof fn !== "function") {
  79. fn._settlePromises();
  80. } else {
  81. var receiver = queue.shift();
  82. var arg = queue.shift();
  83. fn.call(receiver, arg);
  84. }
  85. }
  86. Async.prototype._drainQueues = function () {
  87. _drainQueue(this._normalQueue);
  88. this._reset();
  89. this._haveDrainedQueues = true;
  90. _drainQueue(this._lateQueue);
  91. };
  92. Async.prototype._queueTick = function () {
  93. if (!this._isTickUsed) {
  94. this._isTickUsed = true;
  95. this._schedule(this.drainQueues);
  96. }
  97. };
  98. Async.prototype._reset = function () {
  99. this._isTickUsed = false;
  100. };
  101. module.exports = Async;
  102. module.exports.firstLineError = firstLineError;