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