async.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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._isTickUsed = false;
  9. this._lateQueue = new Queue(16);
  10. this._normalQueue = new Queue(16);
  11. this._haveDrainedQueues = false;
  12. this._trampolineEnabled = true;
  13. var self = this;
  14. this.drainQueues = function () {
  15. self._drainQueues();
  16. };
  17. this._schedule = schedule;
  18. }
  19. Async.prototype.enableTrampoline = function() {
  20. this._trampolineEnabled = true;
  21. };
  22. Async.prototype.disableTrampolineIfNecessary = function() {
  23. if (util.hasDevTools) {
  24. this._trampolineEnabled = false;
  25. }
  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. if (!util.hasDevTools) {
  69. Async.prototype.invokeLater = AsyncInvokeLater;
  70. Async.prototype.invoke = AsyncInvoke;
  71. Async.prototype.settlePromises = AsyncSettlePromises;
  72. } else {
  73. Async.prototype.invokeLater = function (fn, receiver, arg) {
  74. if (this._trampolineEnabled) {
  75. AsyncInvokeLater.call(this, fn, receiver, arg);
  76. } else {
  77. this._schedule(function() {
  78. setTimeout(function() {
  79. fn.call(receiver, arg);
  80. }, 100);
  81. });
  82. }
  83. };
  84. Async.prototype.invoke = function (fn, receiver, arg) {
  85. if (this._trampolineEnabled) {
  86. AsyncInvoke.call(this, fn, receiver, arg);
  87. } else {
  88. this._schedule(function() {
  89. fn.call(receiver, arg);
  90. });
  91. }
  92. };
  93. Async.prototype.settlePromises = function(promise) {
  94. if (this._trampolineEnabled) {
  95. AsyncSettlePromises.call(this, promise);
  96. } else {
  97. this._schedule(function() {
  98. promise._settlePromises();
  99. });
  100. }
  101. };
  102. }
  103. Async.prototype.invokeFirst = function (fn, receiver, arg) {
  104. this._normalQueue.unshift(fn, receiver, arg);
  105. this._queueTick();
  106. };
  107. Async.prototype._drainQueue = function(queue) {
  108. while (queue.length() > 0) {
  109. var fn = queue.shift();
  110. if (typeof fn !== "function") {
  111. fn._settlePromises();
  112. continue;
  113. }
  114. var receiver = queue.shift();
  115. var arg = queue.shift();
  116. fn.call(receiver, arg);
  117. }
  118. };
  119. Async.prototype._drainQueues = function () {
  120. this._drainQueue(this._normalQueue);
  121. this._reset();
  122. this._haveDrainedQueues = true;
  123. this._drainQueue(this._lateQueue);
  124. };
  125. Async.prototype._queueTick = function () {
  126. if (!this._isTickUsed) {
  127. this._isTickUsed = true;
  128. this._schedule(this.drainQueues);
  129. }
  130. };
  131. Async.prototype._reset = function () {
  132. this._isTickUsed = false;
  133. };
  134. module.exports = Async;
  135. module.exports.firstLineError = firstLineError;