raw.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. var domain; // The domain module is executed on demand
  3. var hasSetImmediate = typeof setImmediate === "function";
  4. // Use the fastest means possible to execute a task in its own turn, with
  5. // priority over other events including network IO events in Node.js.
  6. //
  7. // An exception thrown by a task will permanently interrupt the processing of
  8. // subsequent tasks. The higher level `asap` function ensures that if an
  9. // exception is thrown by a task, that the task queue will continue flushing as
  10. // soon as possible, but if you use `rawAsap` directly, you are responsible to
  11. // either ensure that no exceptions are thrown from your task, or to manually
  12. // call `rawAsap.requestFlush` if an exception is thrown.
  13. module.exports = rawAsap;
  14. function rawAsap(task) {
  15. if (!queue.length) {
  16. requestFlush();
  17. flushing = true;
  18. }
  19. // Avoids a function call
  20. queue[queue.length] = task;
  21. }
  22. var queue = [];
  23. // Once a flush has been requested, no further calls to `requestFlush` are
  24. // necessary until the next `flush` completes.
  25. var flushing = false;
  26. // The position of the next task to execute in the task queue. This is
  27. // preserved between calls to `flush` so that it can be resumed if
  28. // a task throws an exception.
  29. var index = 0;
  30. // If a task schedules additional tasks recursively, the task queue can grow
  31. // unbounded. To prevent memory excaustion, the task queue will periodically
  32. // truncate already-completed tasks.
  33. var capacity = 1024;
  34. // The flush function processes all tasks that have been scheduled with
  35. // `rawAsap` unless and until one of those tasks throws an exception.
  36. // If a task throws an exception, `flush` ensures that its state will remain
  37. // consistent and will resume where it left off when called again.
  38. // However, `flush` does not make any arrangements to be called again if an
  39. // exception is thrown.
  40. function flush() {
  41. while (index < queue.length) {
  42. var currentIndex = index;
  43. // Advance the index before calling the task. This ensures that we will
  44. // begin flushing on the next task the task throws an error.
  45. index = index + 1;
  46. queue[currentIndex].call();
  47. // Prevent leaking memory for long chains of recursive calls to `asap`.
  48. // If we call `asap` within tasks scheduled by `asap`, the queue will
  49. // grow, but to avoid an O(n) walk for every task we execute, we don't
  50. // shift tasks off the queue after they have been executed.
  51. // Instead, we periodically shift 1024 tasks off the queue.
  52. if (index > capacity) {
  53. // Manually shift all values starting at the index back to the
  54. // beginning of the queue.
  55. for (var scan = 0, newLength = queue.length - index; scan < newLength; scan++) {
  56. queue[scan] = queue[scan + index];
  57. }
  58. queue.length -= index;
  59. index = 0;
  60. }
  61. }
  62. queue.length = 0;
  63. index = 0;
  64. flushing = false;
  65. }
  66. rawAsap.requestFlush = requestFlush;
  67. function requestFlush() {
  68. // Ensure flushing is not bound to any domain.
  69. // It is not sufficient to exit the domain, because domains exist on a stack.
  70. // To execute code outside of any domain, the following dance is necessary.
  71. var parentDomain = process.domain;
  72. if (parentDomain) {
  73. if (!domain) {
  74. // Lazy execute the domain module.
  75. // Only employed if the user elects to use domains.
  76. domain = require("domain");
  77. }
  78. domain.active = process.domain = null;
  79. }
  80. // `setImmediate` is slower that `process.nextTick`, but `process.nextTick`
  81. // cannot handle recursion.
  82. // `requestFlush` will only be called recursively from `asap.js`, to resume
  83. // flushing after an error is thrown into a domain.
  84. // Conveniently, `setImmediate` was introduced in the same version
  85. // `process.nextTick` started throwing recursion errors.
  86. if (flushing && hasSetImmediate) {
  87. setImmediate(flush);
  88. } else {
  89. process.nextTick(flush);
  90. }
  91. if (parentDomain) {
  92. domain.active = process.domain = parentDomain;
  93. }
  94. }