index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. function throatInternal(size) {
  3. var queue = new Queue();
  4. var s = size | 0;
  5. function run(fn, self, args) {
  6. if ((s | 0) !== 0) {
  7. s = (s | 0) - 1;
  8. return new Promise(function (resolve) {
  9. resolve(fn.apply(self, args));
  10. }).then(onFulfill, onReject);
  11. }
  12. return new Promise(function (resolve) {
  13. queue.push(new Delayed(resolve, fn, self, args));
  14. }).then(runDelayed);
  15. }
  16. function runDelayed(d) {
  17. try {
  18. return Promise.resolve(d.fn.apply(d.self, d.args)).then(
  19. onFulfill,
  20. onReject
  21. );
  22. } catch (ex) {
  23. onReject(ex);
  24. }
  25. }
  26. function onFulfill(result) {
  27. release();
  28. return result;
  29. }
  30. function onReject(error) {
  31. release();
  32. throw error;
  33. }
  34. function release() {
  35. var next = queue.shift();
  36. if (next) {
  37. next.resolve(next);
  38. } else {
  39. s = (s | 0) + 1;
  40. }
  41. }
  42. return run;
  43. }
  44. function earlyBound(size, fn) {
  45. const run = throatInternal(size | 0);
  46. return function () {
  47. var args = new Array(arguments.length);
  48. for (var i = 0; i < arguments.length; i++) {
  49. args[i] = arguments[i];
  50. }
  51. return run(fn, this, args);
  52. };
  53. }
  54. function lateBound(size) {
  55. const run = throatInternal(size | 0);
  56. return function (fn) {
  57. if (typeof fn !== 'function') {
  58. throw new TypeError(
  59. 'Expected throat fn to be a function but got ' + typeof fn
  60. );
  61. }
  62. var args = new Array(arguments.length - 1);
  63. for (var i = 1; i < arguments.length; i++) {
  64. args[i - 1] = arguments[i];
  65. }
  66. return run(fn, this, args);
  67. };
  68. }
  69. module.exports = function throat(size, fn) {
  70. if (typeof size === 'function') {
  71. var temp = fn;
  72. fn = size;
  73. size = temp;
  74. }
  75. if (typeof size !== 'number') {
  76. throw new TypeError(
  77. 'Expected throat size to be a number but got ' + typeof size
  78. );
  79. }
  80. if (fn !== undefined && typeof fn !== 'function') {
  81. throw new TypeError(
  82. 'Expected throat fn to be a function but got ' + typeof fn
  83. );
  84. }
  85. if (typeof fn === 'function') {
  86. return earlyBound(size | 0, fn);
  87. } else {
  88. return lateBound(size | 0);
  89. }
  90. };
  91. module.exports.default = module.exports;
  92. function Delayed(resolve, fn, self, args) {
  93. this.resolve = resolve;
  94. this.fn = fn;
  95. this.self = self || null;
  96. this.args = args;
  97. }
  98. var blockSize = 64;
  99. function Queue() {
  100. this._s1 = [];
  101. this._s2 = [];
  102. this._shiftBlock = this._pushBlock = new Array(blockSize);
  103. this._pushIndex = 0;
  104. this._shiftIndex = 0;
  105. }
  106. Queue.prototype.push = function (value) {
  107. if (this._pushIndex === blockSize) {
  108. this._pushIndex = 0;
  109. this._s1[this._s1.length] = this._pushBlock = new Array(blockSize);
  110. }
  111. this._pushBlock[this._pushIndex++] = value;
  112. };
  113. Queue.prototype.shift = function () {
  114. if (this._shiftIndex === blockSize) {
  115. this._shiftIndex = 0;
  116. var s2 = this._s2;
  117. if (s2.length === 0) {
  118. var s1 = this._s1;
  119. if (s1.length === 0) {
  120. return undefined;
  121. }
  122. this._s1 = s2;
  123. s2 = this._s2 = s1.reverse();
  124. }
  125. this._shiftBlock = s2.pop();
  126. }
  127. if (
  128. this._pushBlock === this._shiftBlock &&
  129. this._pushIndex === this._shiftIndex
  130. ) {
  131. return undefined;
  132. }
  133. var result = this._shiftBlock[this._shiftIndex];
  134. this._shiftBlock[this._shiftIndex++] = null;
  135. return result;
  136. };