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.

async.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. var util = require("./util");
  7. function Async() {
  8. this._customScheduler = false;
  9. this._isTickUsed = false;
  10. this._lateQueue = new Queue(16);
  11. this._normalQueue = new Queue(16);
  12. this._haveDrainedQueues = false;
  13. this._trampolineEnabled = true;
  14. var self = this;
  15. this.drainQueues = function () {
  16. self._drainQueues();
  17. };
  18. this._schedule = schedule;
  19. }
  20. Async.prototype.setScheduler = function(fn) {
  21. var prev = this._schedule;
  22. this._schedule = fn;
  23. this._customScheduler = true;
  24. return prev;
  25. };
  26. Async.prototype.hasCustomScheduler = function() {
  27. return this._customScheduler;
  28. };
  29. Async.prototype.enableTrampoline = function() {
  30. this._trampolineEnabled = true;
  31. };
  32. Async.prototype.disableTrampolineIfNecessary = function() {
  33. if (util.hasDevTools) {
  34. this._trampolineEnabled = false;
  35. }
  36. };
  37. Async.prototype.haveItemsQueued = function () {
  38. return this._isTickUsed || this._haveDrainedQueues;
  39. };
  40. Async.prototype.fatalError = function(e, isNode) {
  41. if (isNode) {
  42. process.stderr.write("Fatal " + (e instanceof Error ? e.stack : e) +
  43. "\n");
  44. process.exit(2);
  45. } else {
  46. this.throwLater(e);
  47. }
  48. };
  49. Async.prototype.throwLater = function(fn, arg) {
  50. if (arguments.length === 1) {
  51. arg = fn;
  52. fn = function () { throw arg; };
  53. }
  54. if (typeof setTimeout !== "undefined") {
  55. setTimeout(function() {
  56. fn(arg);
  57. }, 0);
  58. } else try {
  59. this._schedule(function() {
  60. fn(arg);
  61. });
  62. } catch (e) {
  63. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  64. }
  65. };
  66. function AsyncInvokeLater(fn, receiver, arg) {
  67. this._lateQueue.push(fn, receiver, arg);
  68. this._queueTick();
  69. }
  70. function AsyncInvoke(fn, receiver, arg) {
  71. this._normalQueue.push(fn, receiver, arg);
  72. this._queueTick();
  73. }
  74. function AsyncSettlePromises(promise) {
  75. this._normalQueue._pushOne(promise);
  76. this._queueTick();
  77. }
  78. if (!util.hasDevTools) {
  79. Async.prototype.invokeLater = AsyncInvokeLater;
  80. Async.prototype.invoke = AsyncInvoke;
  81. Async.prototype.settlePromises = AsyncSettlePromises;
  82. } else {
  83. Async.prototype.invokeLater = function (fn, receiver, arg) {
  84. if (this._trampolineEnabled) {
  85. AsyncInvokeLater.call(this, fn, receiver, arg);
  86. } else {
  87. this._schedule(function() {
  88. setTimeout(function() {
  89. fn.call(receiver, arg);
  90. }, 100);
  91. });
  92. }
  93. };
  94. Async.prototype.invoke = function (fn, receiver, arg) {
  95. if (this._trampolineEnabled) {
  96. AsyncInvoke.call(this, fn, receiver, arg);
  97. } else {
  98. this._schedule(function() {
  99. fn.call(receiver, arg);
  100. });
  101. }
  102. };
  103. Async.prototype.settlePromises = function(promise) {
  104. if (this._trampolineEnabled) {
  105. AsyncSettlePromises.call(this, promise);
  106. } else {
  107. this._schedule(function() {
  108. promise._settlePromises();
  109. });
  110. }
  111. };
  112. }
  113. function _drainQueue(queue) {
  114. while (queue.length() > 0) {
  115. _drainQueueStep(queue);
  116. }
  117. }
  118. function _drainQueueStep(queue) {
  119. var fn = queue.shift();
  120. if (typeof fn !== "function") {
  121. fn._settlePromises();
  122. } else {
  123. var receiver = queue.shift();
  124. var arg = queue.shift();
  125. fn.call(receiver, arg);
  126. }
  127. }
  128. Async.prototype._drainQueues = function () {
  129. _drainQueue(this._normalQueue);
  130. this._reset();
  131. this._haveDrainedQueues = true;
  132. _drainQueue(this._lateQueue);
  133. };
  134. Async.prototype._queueTick = function () {
  135. if (!this._isTickUsed) {
  136. this._isTickUsed = true;
  137. this._schedule(this.drainQueues);
  138. }
  139. };
  140. Async.prototype._reset = function () {
  141. this._isTickUsed = false;
  142. };
  143. module.exports = Async;
  144. module.exports.firstLineError = firstLineError;