UI for Zipcoin Blue

index.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. 'use strict';
  2. var immediate = require('immediate');
  3. /* istanbul ignore next */
  4. function INTERNAL() {}
  5. var handlers = {};
  6. var REJECTED = ['REJECTED'];
  7. var FULFILLED = ['FULFILLED'];
  8. var PENDING = ['PENDING'];
  9. /* istanbul ignore else */
  10. if (!process.browser) {
  11. // in which we actually take advantage of JS scoping
  12. var UNHANDLED = ['UNHANDLED'];
  13. }
  14. module.exports = exports = Promise;
  15. function Promise(resolver) {
  16. if (typeof resolver !== 'function') {
  17. throw new TypeError('resolver must be a function');
  18. }
  19. this.state = PENDING;
  20. this.queue = [];
  21. this.outcome = void 0;
  22. /* istanbul ignore else */
  23. if (!process.browser) {
  24. this.handled = UNHANDLED;
  25. }
  26. if (resolver !== INTERNAL) {
  27. safelyResolveThenable(this, resolver);
  28. }
  29. }
  30. Promise.prototype.catch = function (onRejected) {
  31. return this.then(null, onRejected);
  32. };
  33. Promise.prototype.then = function (onFulfilled, onRejected) {
  34. if (typeof onFulfilled !== 'function' && this.state === FULFILLED ||
  35. typeof onRejected !== 'function' && this.state === REJECTED) {
  36. return this;
  37. }
  38. var promise = new this.constructor(INTERNAL);
  39. /* istanbul ignore else */
  40. if (!process.browser) {
  41. if (this.handled === UNHANDLED) {
  42. this.handled = null;
  43. }
  44. }
  45. if (this.state !== PENDING) {
  46. var resolver = this.state === FULFILLED ? onFulfilled : onRejected;
  47. unwrap(promise, resolver, this.outcome);
  48. } else {
  49. this.queue.push(new QueueItem(promise, onFulfilled, onRejected));
  50. }
  51. return promise;
  52. };
  53. function QueueItem(promise, onFulfilled, onRejected) {
  54. this.promise = promise;
  55. if (typeof onFulfilled === 'function') {
  56. this.onFulfilled = onFulfilled;
  57. this.callFulfilled = this.otherCallFulfilled;
  58. }
  59. if (typeof onRejected === 'function') {
  60. this.onRejected = onRejected;
  61. this.callRejected = this.otherCallRejected;
  62. }
  63. }
  64. QueueItem.prototype.callFulfilled = function (value) {
  65. handlers.resolve(this.promise, value);
  66. };
  67. QueueItem.prototype.otherCallFulfilled = function (value) {
  68. unwrap(this.promise, this.onFulfilled, value);
  69. };
  70. QueueItem.prototype.callRejected = function (value) {
  71. handlers.reject(this.promise, value);
  72. };
  73. QueueItem.prototype.otherCallRejected = function (value) {
  74. unwrap(this.promise, this.onRejected, value);
  75. };
  76. function unwrap(promise, func, value) {
  77. immediate(function () {
  78. var returnValue;
  79. try {
  80. returnValue = func(value);
  81. } catch (e) {
  82. return handlers.reject(promise, e);
  83. }
  84. if (returnValue === promise) {
  85. handlers.reject(promise, new TypeError('Cannot resolve promise with itself'));
  86. } else {
  87. handlers.resolve(promise, returnValue);
  88. }
  89. });
  90. }
  91. handlers.resolve = function (self, value) {
  92. var result = tryCatch(getThen, value);
  93. if (result.status === 'error') {
  94. return handlers.reject(self, result.value);
  95. }
  96. var thenable = result.value;
  97. if (thenable) {
  98. safelyResolveThenable(self, thenable);
  99. } else {
  100. self.state = FULFILLED;
  101. self.outcome = value;
  102. var i = -1;
  103. var len = self.queue.length;
  104. while (++i < len) {
  105. self.queue[i].callFulfilled(value);
  106. }
  107. }
  108. return self;
  109. };
  110. handlers.reject = function (self, error) {
  111. self.state = REJECTED;
  112. self.outcome = error;
  113. /* istanbul ignore else */
  114. if (!process.browser) {
  115. if (self.handled === UNHANDLED) {
  116. immediate(function () {
  117. if (self.handled === UNHANDLED) {
  118. process.emit('unhandledRejection', error, self);
  119. }
  120. });
  121. }
  122. }
  123. var i = -1;
  124. var len = self.queue.length;
  125. while (++i < len) {
  126. self.queue[i].callRejected(error);
  127. }
  128. return self;
  129. };
  130. function getThen(obj) {
  131. // Make sure we only access the accessor once as required by the spec
  132. var then = obj && obj.then;
  133. if (obj && typeof obj === 'object' && typeof then === 'function') {
  134. return function appyThen() {
  135. then.apply(obj, arguments);
  136. };
  137. }
  138. }
  139. function safelyResolveThenable(self, thenable) {
  140. // Either fulfill, reject or reject with error
  141. var called = false;
  142. function onError(value) {
  143. if (called) {
  144. return;
  145. }
  146. called = true;
  147. handlers.reject(self, value);
  148. }
  149. function onSuccess(value) {
  150. if (called) {
  151. return;
  152. }
  153. called = true;
  154. handlers.resolve(self, value);
  155. }
  156. function tryToUnwrap() {
  157. thenable(onSuccess, onError);
  158. }
  159. var result = tryCatch(tryToUnwrap);
  160. if (result.status === 'error') {
  161. onError(result.value);
  162. }
  163. }
  164. function tryCatch(func, value) {
  165. var out = {};
  166. try {
  167. out.value = func(value);
  168. out.status = 'success';
  169. } catch (e) {
  170. out.status = 'error';
  171. out.value = e;
  172. }
  173. return out;
  174. }
  175. exports.resolve = resolve;
  176. function resolve(value) {
  177. if (value instanceof this) {
  178. return value;
  179. }
  180. return handlers.resolve(new this(INTERNAL), value);
  181. }
  182. exports.reject = reject;
  183. function reject(reason) {
  184. var promise = new this(INTERNAL);
  185. return handlers.reject(promise, reason);
  186. }
  187. exports.all = all;
  188. function all(iterable) {
  189. var self = this;
  190. if (Object.prototype.toString.call(iterable) !== '[object Array]') {
  191. return this.reject(new TypeError('must be an array'));
  192. }
  193. var len = iterable.length;
  194. var called = false;
  195. if (!len) {
  196. return this.resolve([]);
  197. }
  198. var values = new Array(len);
  199. var resolved = 0;
  200. var i = -1;
  201. var promise = new this(INTERNAL);
  202. while (++i < len) {
  203. allResolver(iterable[i], i);
  204. }
  205. return promise;
  206. function allResolver(value, i) {
  207. self.resolve(value).then(resolveFromAll, function (error) {
  208. if (!called) {
  209. called = true;
  210. handlers.reject(promise, error);
  211. }
  212. });
  213. function resolveFromAll(outValue) {
  214. values[i] = outValue;
  215. if (++resolved === len && !called) {
  216. called = true;
  217. handlers.resolve(promise, values);
  218. }
  219. }
  220. }
  221. }
  222. exports.race = race;
  223. function race(iterable) {
  224. var self = this;
  225. if (Object.prototype.toString.call(iterable) !== '[object Array]') {
  226. return this.reject(new TypeError('must be an array'));
  227. }
  228. var len = iterable.length;
  229. var called = false;
  230. if (!len) {
  231. return this.resolve([]);
  232. }
  233. var i = -1;
  234. var promise = new this(INTERNAL);
  235. while (++i < len) {
  236. resolver(iterable[i]);
  237. }
  238. return promise;
  239. function resolver(value) {
  240. self.resolve(value).then(function (response) {
  241. if (!called) {
  242. called = true;
  243. handlers.resolve(promise, response);
  244. }
  245. }, function (error) {
  246. if (!called) {
  247. called = true;
  248. handlers.reject(promise, error);
  249. }
  250. });
  251. }
  252. }