zousan.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* This code is based on:
  2. zousan - A Lightning Fast, Yet Very Small Promise A+ Compliant Implementation
  3. https://github.com/bluejava/zousan
  4. Author: Glenn Crownover <glenn@bluejava.com> (http://www.bluejava.com)
  5. Version 2.3.3
  6. License: MIT */
  7. "use strict";
  8. module.exports = function(tick){
  9. tick = tick || (typeof process==="object" && process.nextTick) || (typeof setImmediate==="function" && setImmediate) || function(f){setTimeout(f,0)};
  10. var soon = (function () {
  11. var fq = [], fqStart = 0, bufferSize = 1024;
  12. function callQueue() {
  13. while (fq.length - fqStart) {
  14. try { fq[fqStart]() } catch(ex) { /* console.error(ex) */ }
  15. fq[fqStart++] = undefined;
  16. if (fqStart === bufferSize) {
  17. fq.splice(0, bufferSize);
  18. fqStart = 0;
  19. }
  20. }
  21. }
  22. return function (fn) {
  23. fq.push(fn);
  24. if (fq.length - fqStart === 1)
  25. tick(callQueue);
  26. };
  27. })();
  28. function Zousan(func) {
  29. if (func) {
  30. var me = this;
  31. func(function (arg) {
  32. me.resolve(arg);
  33. }, function (arg) {
  34. me.reject(arg);
  35. });
  36. }
  37. }
  38. Zousan.prototype = {
  39. resolve: function (value) {
  40. if (this.state !== undefined)
  41. return;
  42. if (value === this)
  43. return this.reject(new TypeError("Attempt to resolve promise with self"));
  44. var me = this;
  45. if (value && (typeof value === "function" || typeof value === "object")) {
  46. try {
  47. var first = 0;
  48. var then = value.then;
  49. if (typeof then === "function") {
  50. then.call(value, function (ra) {
  51. if (!first++) {
  52. me.resolve(ra);
  53. }
  54. }, function (rr) {
  55. if (!first++) {
  56. me.reject(rr);
  57. }
  58. });
  59. return;
  60. }
  61. } catch (e) {
  62. if (!first)
  63. this.reject(e);
  64. return;
  65. }
  66. }
  67. this.state = STATE_FULFILLED;
  68. this.v = value;
  69. if (me.c)
  70. soon(function () {
  71. for (var n = 0, l = me.c.length;n < l; n++)
  72. STATE_FULFILLED(me.c[n], value);
  73. });
  74. },
  75. reject: function (reason) {
  76. if (this.state !== undefined)
  77. return;
  78. this.state = STATE_REJECTED;
  79. this.v = reason;
  80. var clients = this.c;
  81. if (clients)
  82. soon(function () {
  83. for (var n = 0, l = clients.length;n < l; n++)
  84. STATE_REJECTED(clients[n], reason);
  85. });
  86. },
  87. then: function (onF, onR) {
  88. var p = new Zousan();
  89. var client = {
  90. y: onF,
  91. n: onR,
  92. p: p
  93. };
  94. if (this.state === undefined) {
  95. if (this.c)
  96. this.c.push(client);
  97. else
  98. this.c = [client];
  99. } else {
  100. var s = this.state, a = this.v;
  101. soon(function () {
  102. s(client, a);
  103. });
  104. }
  105. return p;
  106. }
  107. };
  108. function STATE_FULFILLED(c, arg) {
  109. if (typeof c.y === "function") {
  110. try {
  111. var yret = c.y.call(undefined, arg);
  112. c.p.resolve(yret);
  113. } catch (err) {
  114. c.p.reject(err);
  115. }
  116. } else
  117. c.p.resolve(arg);
  118. }
  119. function STATE_REJECTED(c, reason) {
  120. if (typeof c.n === "function") {
  121. try {
  122. var yret = c.n.call(undefined, reason);
  123. c.p.resolve(yret);
  124. } catch (err) {
  125. c.p.reject(err);
  126. }
  127. } else
  128. c.p.reject(reason);
  129. }
  130. Zousan.resolve = function (val) {
  131. if (val && (val instanceof Zousan))
  132. return val ;
  133. var z = new Zousan();
  134. z.resolve(val);
  135. return z;
  136. };
  137. Zousan.reject = function (err) {
  138. if (err && (err instanceof Zousan))
  139. return err ;
  140. var z = new Zousan();
  141. z.reject(err);
  142. return z;
  143. };
  144. Zousan.version = "2.3.3-nodent" ;
  145. return Zousan ;
  146. };