UNPKG

29.1 kBJavaScriptView Raw
1!function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.Promise=e():"undefined"!=typeof global?global.Promise=e():"undefined"!=typeof self&&(self.Promise=e())}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2/** @license MIT License (c) copyright 2010-2014 original author or authors */
3/** @author Brian Cavalier */
4/** @author John Hann */
5
6/**
7 * ES6 global Promise shim
8 */
9var unhandledRejections = require('../lib/decorators/unhandledRejection');
10var PromiseConstructor = unhandledRejections(require('../lib/Promise'));
11
12module.exports = typeof global != 'undefined' ? (global.Promise = PromiseConstructor)
13 : typeof self != 'undefined' ? (self.Promise = PromiseConstructor)
14 : PromiseConstructor;
15
16},{"../lib/Promise":2,"../lib/decorators/unhandledRejection":6}],2:[function(require,module,exports){
17/** @license MIT License (c) copyright 2010-2014 original author or authors */
18/** @author Brian Cavalier */
19/** @author John Hann */
20
21(function(define) { 'use strict';
22define(function (require) {
23
24 var makePromise = require('./makePromise');
25 var Scheduler = require('./Scheduler');
26 var async = require('./async');
27
28 return makePromise({
29 scheduler: new Scheduler(async)
30 });
31
32});
33})(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });
34
35},{"./Scheduler":4,"./async":5,"./makePromise":7}],3:[function(require,module,exports){
36/** @license MIT License (c) copyright 2010-2014 original author or authors */
37/** @author Brian Cavalier */
38/** @author John Hann */
39
40(function(define) { 'use strict';
41define(function() {
42 /**
43 * Circular queue
44 * @param {number} capacityPow2 power of 2 to which this queue's capacity
45 * will be set initially. eg when capacityPow2 == 3, queue capacity
46 * will be 8.
47 * @constructor
48 */
49 function Queue(capacityPow2) {
50 this.head = this.tail = this.length = 0;
51 this.buffer = new Array(1 << capacityPow2);
52 }
53
54 Queue.prototype.push = function(x) {
55 if(this.length === this.buffer.length) {
56 this._ensureCapacity(this.length * 2);
57 }
58
59 this.buffer[this.tail] = x;
60 this.tail = (this.tail + 1) & (this.buffer.length - 1);
61 ++this.length;
62 return this.length;
63 };
64
65 Queue.prototype.shift = function() {
66 var x = this.buffer[this.head];
67 this.buffer[this.head] = void 0;
68 this.head = (this.head + 1) & (this.buffer.length - 1);
69 --this.length;
70 return x;
71 };
72
73 Queue.prototype._ensureCapacity = function(capacity) {
74 var head = this.head;
75 var buffer = this.buffer;
76 var newBuffer = new Array(capacity);
77 var i = 0;
78 var len;
79
80 if(head === 0) {
81 len = this.length;
82 for(; i<len; ++i) {
83 newBuffer[i] = buffer[i];
84 }
85 } else {
86 capacity = buffer.length;
87 len = this.tail;
88 for(; head<capacity; ++i, ++head) {
89 newBuffer[i] = buffer[head];
90 }
91
92 for(head=0; head<len; ++i, ++head) {
93 newBuffer[i] = buffer[head];
94 }
95 }
96
97 this.buffer = newBuffer;
98 this.head = 0;
99 this.tail = this.length;
100 };
101
102 return Queue;
103
104});
105}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }));
106
107},{}],4:[function(require,module,exports){
108/** @license MIT License (c) copyright 2010-2014 original author or authors */
109/** @author Brian Cavalier */
110/** @author John Hann */
111
112(function(define) { 'use strict';
113define(function(require) {
114
115 var Queue = require('./Queue');
116
117 // Credit to Twisol (https://github.com/Twisol) for suggesting
118 // this type of extensible queue + trampoline approach for next-tick conflation.
119
120 /**
121 * Async task scheduler
122 * @param {function} async function to schedule a single async function
123 * @constructor
124 */
125 function Scheduler(async) {
126 this._async = async;
127 this._queue = new Queue(15);
128 this._afterQueue = new Queue(5);
129 this._running = false;
130
131 var self = this;
132 this.drain = function() {
133 self._drain();
134 };
135 }
136
137 /**
138 * Enqueue a task
139 * @param {{ run:function }} task
140 */
141 Scheduler.prototype.enqueue = function(task) {
142 this._add(this._queue, task);
143 };
144
145 /**
146 * Enqueue a task to run after the main task queue
147 * @param {{ run:function }} task
148 */
149 Scheduler.prototype.afterQueue = function(task) {
150 this._add(this._afterQueue, task);
151 };
152
153 /**
154 * Drain the handler queue entirely, and then the after queue
155 */
156 Scheduler.prototype._drain = function() {
157 runQueue(this._queue);
158 this._running = false;
159 runQueue(this._afterQueue);
160 };
161
162 /**
163 * Add a task to the q, and schedule drain if not already scheduled
164 * @param {Queue} queue
165 * @param {{run:function}} task
166 * @private
167 */
168 Scheduler.prototype._add = function(queue, task) {
169 queue.push(task);
170 if(!this._running) {
171 this._running = true;
172 this._async(this.drain);
173 }
174 };
175
176 /**
177 * Run all the tasks in the q
178 * @param queue
179 */
180 function runQueue(queue) {
181 while(queue.length > 0) {
182 queue.shift().run();
183 }
184 }
185
186 return Scheduler;
187
188});
189}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));
190
191},{"./Queue":3}],5:[function(require,module,exports){
192/** @license MIT License (c) copyright 2010-2014 original author or authors */
193/** @author Brian Cavalier */
194/** @author John Hann */
195
196(function(define) { 'use strict';
197define(function(require) {
198
199 // Sniff "best" async scheduling option
200 // Prefer process.nextTick or MutationObserver, then check for
201 // vertx and finally fall back to setTimeout
202
203 /*jshint maxcomplexity:6*/
204 /*global process,document,setTimeout,MutationObserver,WebKitMutationObserver*/
205 var nextTick, MutationObs;
206
207 if (typeof process !== 'undefined' && process !== null &&
208 typeof process.nextTick === 'function') {
209 nextTick = function(f) {
210 process.nextTick(f);
211 };
212
213 } else if (MutationObs =
214 (typeof MutationObserver === 'function' && MutationObserver) ||
215 (typeof WebKitMutationObserver === 'function' && WebKitMutationObserver)) {
216 nextTick = (function (document, MutationObserver) {
217 var scheduled;
218 var el = document.createElement('div');
219 var o = new MutationObserver(run);
220 o.observe(el, { attributes: true });
221
222 function run() {
223 var f = scheduled;
224 scheduled = void 0;
225 f();
226 }
227
228 return function (f) {
229 scheduled = f;
230 el.setAttribute('class', 'x');
231 };
232 }(document, MutationObs));
233
234 } else {
235 nextTick = (function(cjsRequire) {
236 try {
237 // vert.x 1.x || 2.x
238 return cjsRequire('vertx').runOnLoop || cjsRequire('vertx').runOnContext;
239 } catch (ignore) {}
240
241 // capture setTimeout to avoid being caught by fake timers
242 // used in time based tests
243 var capturedSetTimeout = setTimeout;
244 return function (t) {
245 capturedSetTimeout(t, 0);
246 };
247 }(require));
248 }
249
250 return nextTick;
251});
252}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));
253
254},{}],6:[function(require,module,exports){
255/** @license MIT License (c) copyright 2010-2014 original author or authors */
256/** @author Brian Cavalier */
257/** @author John Hann */
258
259(function(define) { 'use strict';
260define(function(require) {
261
262 var timer = require('../timer');
263
264 return function unhandledRejection(Promise) {
265 var logError = noop;
266 var logInfo = noop;
267
268 if(typeof console !== 'undefined') {
269 logError = typeof console.error !== 'undefined'
270 ? function (e) { console.error(e); }
271 : function (e) { console.log(e); };
272
273 logInfo = typeof console.info !== 'undefined'
274 ? function (e) { console.info(e); }
275 : function (e) { console.log(e); };
276 }
277
278 Promise.onPotentiallyUnhandledRejection = function(rejection) {
279 enqueue(report, rejection);
280 };
281
282 Promise.onPotentiallyUnhandledRejectionHandled = function(rejection) {
283 enqueue(unreport, rejection);
284 };
285
286 Promise.onFatalRejection = function(rejection) {
287 enqueue(throwit, rejection.value);
288 };
289
290 var tasks = [];
291 var reported = [];
292 var running = false;
293
294 function report(r) {
295 if(!r.handled) {
296 reported.push(r);
297 logError('Potentially unhandled rejection [' + r.id + '] ' + formatError(r.value));
298 }
299 }
300
301 function unreport(r) {
302 var i = reported.indexOf(r);
303 if(i >= 0) {
304 reported.splice(i, 1);
305 logInfo('Handled previous rejection [' + r.id + '] ' + formatObject(r.value));
306 }
307 }
308
309 function enqueue(f, x) {
310 tasks.push(f, x);
311 if(!running) {
312 running = true;
313 running = timer.set(flush, 0);
314 }
315 }
316
317 function flush() {
318 running = false;
319 while(tasks.length > 0) {
320 tasks.shift()(tasks.shift());
321 }
322 }
323
324 return Promise;
325 };
326
327 function formatError(e) {
328 var s = typeof e === 'object' && e.stack ? e.stack : formatObject(e);
329 return e instanceof Error ? s : s + ' (WARNING: non-Error used)';
330 }
331
332 function formatObject(o) {
333 var s = String(o);
334 if(s === '[object Object]' && typeof JSON !== 'undefined') {
335 s = tryStringify(o, s);
336 }
337 return s;
338 }
339
340 function tryStringify(e, defaultValue) {
341 try {
342 return JSON.stringify(e);
343 } catch(e) {
344 // Ignore. Cannot JSON.stringify e, stick with String(e)
345 return defaultValue;
346 }
347 }
348
349 function throwit(e) {
350 throw e;
351 }
352
353 function noop() {}
354
355});
356}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));
357
358},{"../timer":8}],7:[function(require,module,exports){
359/** @license MIT License (c) copyright 2010-2014 original author or authors */
360/** @author Brian Cavalier */
361/** @author John Hann */
362
363(function(define) { 'use strict';
364define(function() {
365
366 return function makePromise(environment) {
367
368 var tasks = environment.scheduler;
369
370 var objectCreate = Object.create ||
371 function(proto) {
372 function Child() {}
373 Child.prototype = proto;
374 return new Child();
375 };
376
377 /**
378 * Create a promise whose fate is determined by resolver
379 * @constructor
380 * @returns {Promise} promise
381 * @name Promise
382 */
383 function Promise(resolver, handler) {
384 this._handler = resolver === Handler ? handler : init(resolver);
385 }
386
387 /**
388 * Run the supplied resolver
389 * @param resolver
390 * @returns {Pending}
391 */
392 function init(resolver) {
393 var handler = new Pending();
394
395 try {
396 resolver(promiseResolve, promiseReject, promiseNotify);
397 } catch (e) {
398 promiseReject(e);
399 }
400
401 return handler;
402
403 /**
404 * Transition from pre-resolution state to post-resolution state, notifying
405 * all listeners of the ultimate fulfillment or rejection
406 * @param {*} x resolution value
407 */
408 function promiseResolve (x) {
409 handler.resolve(x);
410 }
411 /**
412 * Reject this promise with reason, which will be used verbatim
413 * @param {Error|*} reason rejection reason, strongly suggested
414 * to be an Error type
415 */
416 function promiseReject (reason) {
417 handler.reject(reason);
418 }
419
420 /**
421 * Issue a progress event, notifying all progress listeners
422 * @param {*} x progress event payload to pass to all listeners
423 */
424 function promiseNotify (x) {
425 handler.notify(x);
426 }
427 }
428
429 // Creation
430
431 Promise.resolve = resolve;
432 Promise.reject = reject;
433 Promise.never = never;
434
435 Promise._defer = defer;
436 Promise._handler = getHandler;
437
438 /**
439 * Returns a trusted promise. If x is already a trusted promise, it is
440 * returned, otherwise returns a new trusted Promise which follows x.
441 * @param {*} x
442 * @return {Promise} promise
443 */
444 function resolve(x) {
445 return isPromise(x) ? x
446 : new Promise(Handler, new Async(getHandler(x)));
447 }
448
449 /**
450 * Return a reject promise with x as its reason (x is used verbatim)
451 * @param {*} x
452 * @returns {Promise} rejected promise
453 */
454 function reject(x) {
455 return new Promise(Handler, new Async(new Rejected(x)));
456 }
457
458 /**
459 * Return a promise that remains pending forever
460 * @returns {Promise} forever-pending promise.
461 */
462 function never() {
463 return foreverPendingPromise; // Should be frozen
464 }
465
466 /**
467 * Creates an internal {promise, resolver} pair
468 * @private
469 * @returns {Promise}
470 */
471 function defer() {
472 return new Promise(Handler, new Pending());
473 }
474
475 // Transformation and flow control
476
477 /**
478 * Transform this promise's fulfillment value, returning a new Promise
479 * for the transformed result. If the promise cannot be fulfilled, onRejected
480 * is called with the reason. onProgress *may* be called with updates toward
481 * this promise's fulfillment.
482 * @param {function=} onFulfilled fulfillment handler
483 * @param {function=} onRejected rejection handler
484 * @deprecated @param {function=} onProgress progress handler
485 * @return {Promise} new promise
486 */
487 Promise.prototype.then = function(onFulfilled, onRejected) {
488 var parent = this._handler;
489
490 if (typeof onFulfilled !== 'function' && parent.join().state() > 0) {
491 // Short circuit: value will not change, simply share handler
492 return new Promise(Handler, parent);
493 }
494
495 var p = this._beget();
496 var child = p._handler;
497
498 parent.chain(child, parent.receiver, onFulfilled, onRejected,
499 arguments.length > 2 ? arguments[2] : void 0);
500
501 return p;
502 };
503
504 /**
505 * If this promise cannot be fulfilled due to an error, call onRejected to
506 * handle the error. Shortcut for .then(undefined, onRejected)
507 * @param {function?} onRejected
508 * @return {Promise}
509 */
510 Promise.prototype['catch'] = function(onRejected) {
511 return this.then(void 0, onRejected);
512 };
513
514 /**
515 * Creates a new, pending promise of the same type as this promise
516 * @private
517 * @returns {Promise}
518 */
519 Promise.prototype._beget = function() {
520 var parent = this._handler;
521 var child = new Pending(parent.receiver, parent.join().context);
522 return new this.constructor(Handler, child);
523 };
524
525 // Array combinators
526
527 Promise.all = all;
528 Promise.race = race;
529
530 /**
531 * Return a promise that will fulfill when all promises in the
532 * input array have fulfilled, or will reject when one of the
533 * promises rejects.
534 * @param {array} promises array of promises
535 * @returns {Promise} promise for array of fulfillment values
536 */
537 function all(promises) {
538 /*jshint maxcomplexity:8*/
539 var resolver = new Pending();
540 var pending = promises.length >>> 0;
541 var results = new Array(pending);
542
543 var i, h, x, s;
544 for (i = 0; i < promises.length; ++i) {
545 x = promises[i];
546
547 if (x === void 0 && !(i in promises)) {
548 --pending;
549 continue;
550 }
551
552 if (maybeThenable(x)) {
553 h = isPromise(x)
554 ? x._handler.join()
555 : getHandlerUntrusted(x);
556
557 s = h.state();
558 if (s === 0) {
559 h.fold(settleAt, i, results, resolver);
560 } else if (s > 0) {
561 results[i] = h.value;
562 --pending;
563 } else {
564 resolver.become(h);
565 break;
566 }
567
568 } else {
569 results[i] = x;
570 --pending;
571 }
572 }
573
574 if(pending === 0) {
575 resolver.become(new Fulfilled(results));
576 }
577
578 return new Promise(Handler, resolver);
579
580 function settleAt(i, x, resolver) {
581 /*jshint validthis:true*/
582 this[i] = x;
583 if(--pending === 0) {
584 resolver.become(new Fulfilled(this));
585 }
586 }
587 }
588
589 /**
590 * Fulfill-reject competitive race. Return a promise that will settle
591 * to the same state as the earliest input promise to settle.
592 *
593 * WARNING: The ES6 Promise spec requires that race()ing an empty array
594 * must return a promise that is pending forever. This implementation
595 * returns a singleton forever-pending promise, the same singleton that is
596 * returned by Promise.never(), thus can be checked with ===
597 *
598 * @param {array} promises array of promises to race
599 * @returns {Promise} if input is non-empty, a promise that will settle
600 * to the same outcome as the earliest input promise to settle. if empty
601 * is empty, returns a promise that will never settle.
602 */
603 function race(promises) {
604 // Sigh, race([]) is untestable unless we return *something*
605 // that is recognizable without calling .then() on it.
606 if(Object(promises) === promises && promises.length === 0) {
607 return never();
608 }
609
610 var h = new Pending();
611 var i, x;
612 for(i=0; i<promises.length; ++i) {
613 x = promises[i];
614 if (x !== void 0 && i in promises) {
615 getHandler(x).visit(h, h.resolve, h.reject);
616 }
617 }
618 return new Promise(Handler, h);
619 }
620
621 // Promise internals
622 // Below this, everything is @private
623
624 /**
625 * Get an appropriate handler for x, without checking for cycles
626 * @param {*} x
627 * @returns {object} handler
628 */
629 function getHandler(x) {
630 if(isPromise(x)) {
631 return x._handler.join();
632 }
633 return maybeThenable(x) ? getHandlerUntrusted(x) : new Fulfilled(x);
634 }
635
636 /**
637 * Get a handler for potentially untrusted thenable x
638 * @param {*} x
639 * @returns {object} handler
640 */
641 function getHandlerUntrusted(x) {
642 try {
643 var untrustedThen = x.then;
644 return typeof untrustedThen === 'function'
645 ? new Thenable(untrustedThen, x)
646 : new Fulfilled(x);
647 } catch(e) {
648 return new Rejected(e);
649 }
650 }
651
652 /**
653 * Handler for a promise that is pending forever
654 * @constructor
655 */
656 function Handler() {}
657
658 Handler.prototype.when
659 = Handler.prototype.become
660 = Handler.prototype.notify
661 = Handler.prototype.fail
662 = Handler.prototype._unreport
663 = Handler.prototype._report
664 = noop;
665
666 Handler.prototype._state = 0;
667
668 Handler.prototype.state = function() {
669 return this._state;
670 };
671
672 /**
673 * Recursively collapse handler chain to find the handler
674 * nearest to the fully resolved value.
675 * @returns {object} handler nearest the fully resolved value
676 */
677 Handler.prototype.join = function() {
678 var h = this;
679 while(h.handler !== void 0) {
680 h = h.handler;
681 }
682 return h;
683 };
684
685 Handler.prototype.chain = function(to, receiver, fulfilled, rejected, progress) {
686 this.when({
687 resolver: to,
688 receiver: receiver,
689 fulfilled: fulfilled,
690 rejected: rejected,
691 progress: progress
692 });
693 };
694
695 Handler.prototype.visit = function(receiver, fulfilled, rejected, progress) {
696 this.chain(failIfRejected, receiver, fulfilled, rejected, progress);
697 };
698
699 Handler.prototype.fold = function(f, z, c, to) {
700 this.visit(to, function(x) {
701 f.call(c, z, x, this);
702 }, to.reject, to.notify);
703 };
704
705 /**
706 * Handler that invokes fail() on any handler it becomes
707 * @constructor
708 */
709 function FailIfRejected() {}
710
711 inherit(Handler, FailIfRejected);
712
713 FailIfRejected.prototype.become = function(h) {
714 h.fail();
715 };
716
717 var failIfRejected = new FailIfRejected();
718
719 /**
720 * Handler that manages a queue of consumers waiting on a pending promise
721 * @constructor
722 */
723 function Pending(receiver, inheritedContext) {
724 Promise.createContext(this, inheritedContext);
725
726 this.consumers = void 0;
727 this.receiver = receiver;
728 this.handler = void 0;
729 this.resolved = false;
730 }
731
732 inherit(Handler, Pending);
733
734 Pending.prototype._state = 0;
735
736 Pending.prototype.resolve = function(x) {
737 this.become(getHandler(x));
738 };
739
740 Pending.prototype.reject = function(x) {
741 if(this.resolved) {
742 return;
743 }
744
745 this.become(new Rejected(x));
746 };
747
748 Pending.prototype.join = function() {
749 if (!this.resolved) {
750 return this;
751 }
752
753 var h = this;
754
755 while (h.handler !== void 0) {
756 h = h.handler;
757 if (h === this) {
758 return this.handler = cycle();
759 }
760 }
761
762 return h;
763 };
764
765 Pending.prototype.run = function() {
766 var q = this.consumers;
767 var handler = this.join();
768 this.consumers = void 0;
769
770 for (var i = 0; i < q.length; ++i) {
771 handler.when(q[i]);
772 }
773 };
774
775 Pending.prototype.become = function(handler) {
776 if(this.resolved) {
777 return;
778 }
779
780 this.resolved = true;
781 this.handler = handler;
782 if(this.consumers !== void 0) {
783 tasks.enqueue(this);
784 }
785
786 if(this.context !== void 0) {
787 handler._report(this.context);
788 }
789 };
790
791 Pending.prototype.when = function(continuation) {
792 if(this.resolved) {
793 tasks.enqueue(new ContinuationTask(continuation, this.handler));
794 } else {
795 if(this.consumers === void 0) {
796 this.consumers = [continuation];
797 } else {
798 this.consumers.push(continuation);
799 }
800 }
801 };
802
803 Pending.prototype.notify = function(x) {
804 if(!this.resolved) {
805 tasks.enqueue(new ProgressTask(x, this));
806 }
807 };
808
809 Pending.prototype.fail = function(context) {
810 var c = typeof context === 'undefined' ? this.context : context;
811 this.resolved && this.handler.join().fail(c);
812 };
813
814 Pending.prototype._report = function(context) {
815 this.resolved && this.handler.join()._report(context);
816 };
817
818 Pending.prototype._unreport = function() {
819 this.resolved && this.handler.join()._unreport();
820 };
821
822 /**
823 * Abstract base for handler that delegates to another handler
824 * @param {object} handler
825 * @constructor
826 */
827 function Delegating(handler) {
828 this.handler = handler;
829 }
830
831 inherit(Handler, Delegating);
832
833 Delegating.prototype._report = function(context) {
834 this.join()._report(context);
835 };
836
837 Delegating.prototype._unreport = function() {
838 this.join()._unreport();
839 };
840
841 /**
842 * Wrap another handler and force it into a future stack
843 * @param {object} handler
844 * @constructor
845 */
846 function Async(handler) {
847 Delegating.call(this, handler);
848 }
849
850 inherit(Delegating, Async);
851
852 Async.prototype.when = function(continuation) {
853 tasks.enqueue(new ContinuationTask(continuation, this));
854 };
855
856 /**
857 * Handler that wraps an untrusted thenable and assimilates it in a future stack
858 * @param {function} then
859 * @param {{then: function}} thenable
860 * @constructor
861 */
862 function Thenable(then, thenable) {
863 Pending.call(this);
864 tasks.enqueue(new AssimilateTask(then, thenable, this));
865 }
866
867 inherit(Pending, Thenable);
868
869 /**
870 * Handler for a fulfilled promise
871 * @param {*} x fulfillment value
872 * @constructor
873 */
874 function Fulfilled(x) {
875 Promise.createContext(this);
876 this.value = x;
877 }
878
879 inherit(Handler, Fulfilled);
880
881 Fulfilled.prototype._state = 1;
882
883 Fulfilled.prototype.fold = function(f, z, c, to) {
884 runContinuation3(f, z, this, c, to);
885 };
886
887 Fulfilled.prototype.when = function(cont) {
888 runContinuation1(cont.fulfilled, this, cont.receiver, cont.resolver);
889 };
890
891 var errorId = 0;
892
893 /**
894 * Handler for a rejected promise
895 * @param {*} x rejection reason
896 * @constructor
897 */
898 function Rejected(x) {
899 Promise.createContext(this);
900
901 this.id = ++errorId;
902 this.value = x;
903 this.handled = false;
904 this.reported = false;
905
906 this._report();
907 }
908
909 inherit(Handler, Rejected);
910
911 Rejected.prototype._state = -1;
912
913 Rejected.prototype.fold = function(f, z, c, to) {
914 to.become(this);
915 };
916
917 Rejected.prototype.when = function(cont) {
918 if(typeof cont.rejected === 'function') {
919 this._unreport();
920 }
921 runContinuation1(cont.rejected, this, cont.receiver, cont.resolver);
922 };
923
924 Rejected.prototype._report = function(context) {
925 tasks.afterQueue(new ReportTask(this, context));
926 };
927
928 Rejected.prototype._unreport = function() {
929 this.handled = true;
930 tasks.afterQueue(new UnreportTask(this));
931 };
932
933 Rejected.prototype.fail = function(context) {
934 Promise.onFatalRejection(this, context === void 0 ? this.context : context);
935 };
936
937 function ReportTask(rejection, context) {
938 this.rejection = rejection;
939 this.context = context;
940 }
941
942 ReportTask.prototype.run = function() {
943 if(!this.rejection.handled) {
944 this.rejection.reported = true;
945 Promise.onPotentiallyUnhandledRejection(this.rejection, this.context);
946 }
947 };
948
949 function UnreportTask(rejection) {
950 this.rejection = rejection;
951 }
952
953 UnreportTask.prototype.run = function() {
954 if(this.rejection.reported) {
955 Promise.onPotentiallyUnhandledRejectionHandled(this.rejection);
956 }
957 };
958
959 // Unhandled rejection hooks
960 // By default, everything is a noop
961
962 // TODO: Better names: "annotate"?
963 Promise.createContext
964 = Promise.enterContext
965 = Promise.exitContext
966 = Promise.onPotentiallyUnhandledRejection
967 = Promise.onPotentiallyUnhandledRejectionHandled
968 = Promise.onFatalRejection
969 = noop;
970
971 // Errors and singletons
972
973 var foreverPendingHandler = new Handler();
974 var foreverPendingPromise = new Promise(Handler, foreverPendingHandler);
975
976 function cycle() {
977 return new Rejected(new TypeError('Promise cycle'));
978 }
979
980 // Task runners
981
982 /**
983 * Run a single consumer
984 * @constructor
985 */
986 function ContinuationTask(continuation, handler) {
987 this.continuation = continuation;
988 this.handler = handler;
989 }
990
991 ContinuationTask.prototype.run = function() {
992 this.handler.join().when(this.continuation);
993 };
994
995 /**
996 * Run a queue of progress handlers
997 * @constructor
998 */
999 function ProgressTask(value, handler) {
1000 this.handler = handler;
1001 this.value = value;
1002 }
1003
1004 ProgressTask.prototype.run = function() {
1005 var q = this.handler.consumers;
1006 if(q === void 0) {
1007 return;
1008 }
1009
1010 for (var c, i = 0; i < q.length; ++i) {
1011 c = q[i];
1012 runNotify(c.progress, this.value, this.handler, c.receiver, c.resolver);
1013 }
1014 };
1015
1016 /**
1017 * Assimilate a thenable, sending it's value to resolver
1018 * @param {function} then
1019 * @param {object|function} thenable
1020 * @param {object} resolver
1021 * @constructor
1022 */
1023 function AssimilateTask(then, thenable, resolver) {
1024 this._then = then;
1025 this.thenable = thenable;
1026 this.resolver = resolver;
1027 }
1028
1029 AssimilateTask.prototype.run = function() {
1030 var h = this.resolver;
1031 tryAssimilate(this._then, this.thenable, _resolve, _reject, _notify);
1032
1033 function _resolve(x) { h.resolve(x); }
1034 function _reject(x) { h.reject(x); }
1035 function _notify(x) { h.notify(x); }
1036 };
1037
1038 function tryAssimilate(then, thenable, resolve, reject, notify) {
1039 try {
1040 then.call(thenable, resolve, reject, notify);
1041 } catch (e) {
1042 reject(e);
1043 }
1044 }
1045
1046 // Other helpers
1047
1048 /**
1049 * @param {*} x
1050 * @returns {boolean} true iff x is a trusted Promise
1051 */
1052 function isPromise(x) {
1053 return x instanceof Promise;
1054 }
1055
1056 /**
1057 * Test just enough to rule out primitives, in order to take faster
1058 * paths in some code
1059 * @param {*} x
1060 * @returns {boolean} false iff x is guaranteed *not* to be a thenable
1061 */
1062 function maybeThenable(x) {
1063 return (typeof x === 'object' || typeof x === 'function') && x !== null;
1064 }
1065
1066 function runContinuation1(f, h, receiver, next) {
1067 if(typeof f !== 'function') {
1068 return next.become(h);
1069 }
1070
1071 Promise.enterContext(h);
1072 tryCatchReject(f, h.value, receiver, next);
1073 Promise.exitContext();
1074 }
1075
1076 function runContinuation3(f, x, h, receiver, next) {
1077 if(typeof f !== 'function') {
1078 return next.become(h);
1079 }
1080
1081 Promise.enterContext(h);
1082 tryCatchReject3(f, x, h.value, receiver, next);
1083 Promise.exitContext();
1084 }
1085
1086 function runNotify(f, x, h, receiver, next) {
1087 if(typeof f !== 'function') {
1088 return next.notify(x);
1089 }
1090
1091 Promise.enterContext(h);
1092 tryCatchReturn(f, x, receiver, next);
1093 Promise.exitContext();
1094 }
1095
1096 /**
1097 * Return f.call(thisArg, x), or if it throws return a rejected promise for
1098 * the thrown exception
1099 */
1100 function tryCatchReject(f, x, thisArg, next) {
1101 try {
1102 next.become(getHandler(f.call(thisArg, x)));
1103 } catch(e) {
1104 next.become(new Rejected(e));
1105 }
1106 }
1107
1108 /**
1109 * Same as above, but includes the extra argument parameter.
1110 */
1111 function tryCatchReject3(f, x, y, thisArg, next) {
1112 try {
1113 f.call(thisArg, x, y, next);
1114 } catch(e) {
1115 next.become(new Rejected(e));
1116 }
1117 }
1118
1119 /**
1120 * Return f.call(thisArg, x), or if it throws, *return* the exception
1121 */
1122 function tryCatchReturn(f, x, thisArg, next) {
1123 try {
1124 next.notify(f.call(thisArg, x));
1125 } catch(e) {
1126 next.notify(e);
1127 }
1128 }
1129
1130 function inherit(Parent, Child) {
1131 Child.prototype = objectCreate(Parent.prototype);
1132 Child.prototype.constructor = Child;
1133 }
1134
1135 function noop() {}
1136
1137 return Promise;
1138 };
1139});
1140}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }));
1141
1142},{}],8:[function(require,module,exports){
1143/** @license MIT License (c) copyright 2010-2014 original author or authors */
1144/** @author Brian Cavalier */
1145/** @author John Hann */
1146
1147(function(define) { 'use strict';
1148define(function(require) {
1149 /*global setTimeout,clearTimeout*/
1150 var cjsRequire, vertx, setTimer, clearTimer;
1151
1152 cjsRequire = require;
1153
1154 try {
1155 vertx = cjsRequire('vertx');
1156 setTimer = function (f, ms) { return vertx.setTimer(ms, f); };
1157 clearTimer = vertx.cancelTimer;
1158 } catch (e) {
1159 setTimer = function(f, ms) { return setTimeout(f, ms); };
1160 clearTimer = function(t) { return clearTimeout(t); };
1161 }
1162
1163 return {
1164 set: setTimer,
1165 clear: clearTimer
1166 };
1167
1168});
1169}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));
1170
1171},{}]},{},[1])
1172(1)
1173});
1174;
\No newline at end of file