|
Line 0
a/Source/WebInspectorUI/UserInterface/External/RSVP/rsvp.js_sec1
|
|
|
1 |
/*! |
| 2 |
* @overview RSVP - a tiny implementation of Promises/A+. |
| 3 |
* @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors |
| 4 |
* @license Licensed under MIT license |
| 5 |
* See https://raw.githubusercontent.com/tildeio/rsvp.js/master/LICENSE |
| 6 |
* @version 3.0.13 |
| 7 |
*/ |
| 8 |
|
| 9 |
(function() { |
| 10 |
"use strict"; |
| 11 |
|
| 12 |
function $$rsvp$events$$indexOf(callbacks, callback) { |
| 13 |
for (var i=0, l=callbacks.length; i<l; i++) { |
| 14 |
if (callbacks[i] === callback) { return i; } |
| 15 |
} |
| 16 |
|
| 17 |
return -1; |
| 18 |
} |
| 19 |
|
| 20 |
function $$rsvp$events$$callbacksFor(object) { |
| 21 |
var callbacks = object._promiseCallbacks; |
| 22 |
|
| 23 |
if (!callbacks) { |
| 24 |
callbacks = object._promiseCallbacks = {}; |
| 25 |
} |
| 26 |
|
| 27 |
return callbacks; |
| 28 |
} |
| 29 |
|
| 30 |
var $$rsvp$events$$default = { |
| 31 |
|
| 32 |
/** |
| 33 |
`RSVP.EventTarget.mixin` extends an object with EventTarget methods. For |
| 34 |
Example: |
| 35 |
|
| 36 |
```javascript |
| 37 |
var object = {}; |
| 38 |
|
| 39 |
RSVP.EventTarget.mixin(object); |
| 40 |
|
| 41 |
object.on('finished', function(event) { |
| 42 |
// handle event |
| 43 |
}); |
| 44 |
|
| 45 |
object.trigger('finished', { detail: value }); |
| 46 |
``` |
| 47 |
|
| 48 |
`EventTarget.mixin` also works with prototypes: |
| 49 |
|
| 50 |
```javascript |
| 51 |
var Person = function() {}; |
| 52 |
RSVP.EventTarget.mixin(Person.prototype); |
| 53 |
|
| 54 |
var yehuda = new Person(); |
| 55 |
var tom = new Person(); |
| 56 |
|
| 57 |
yehuda.on('poke', function(event) { |
| 58 |
console.log('Yehuda says OW'); |
| 59 |
}); |
| 60 |
|
| 61 |
tom.on('poke', function(event) { |
| 62 |
console.log('Tom says OW'); |
| 63 |
}); |
| 64 |
|
| 65 |
yehuda.trigger('poke'); |
| 66 |
tom.trigger('poke'); |
| 67 |
``` |
| 68 |
|
| 69 |
@method mixin |
| 70 |
@for RSVP.EventTarget |
| 71 |
@private |
| 72 |
@param {Object} object object to extend with EventTarget methods |
| 73 |
*/ |
| 74 |
mixin: function(object) { |
| 75 |
object.on = this.on; |
| 76 |
object.off = this.off; |
| 77 |
object.trigger = this.trigger; |
| 78 |
object._promiseCallbacks = undefined; |
| 79 |
return object; |
| 80 |
}, |
| 81 |
|
| 82 |
/** |
| 83 |
Registers a callback to be executed when `eventName` is triggered |
| 84 |
|
| 85 |
```javascript |
| 86 |
object.on('event', function(eventInfo){ |
| 87 |
// handle the event |
| 88 |
}); |
| 89 |
|
| 90 |
object.trigger('event'); |
| 91 |
``` |
| 92 |
|
| 93 |
@method on |
| 94 |
@for RSVP.EventTarget |
| 95 |
@private |
| 96 |
@param {String} eventName name of the event to listen for |
| 97 |
@param {Function} callback function to be called when the event is triggered. |
| 98 |
*/ |
| 99 |
on: function(eventName, callback) { |
| 100 |
var allCallbacks = $$rsvp$events$$callbacksFor(this), callbacks; |
| 101 |
|
| 102 |
callbacks = allCallbacks[eventName]; |
| 103 |
|
| 104 |
if (!callbacks) { |
| 105 |
callbacks = allCallbacks[eventName] = []; |
| 106 |
} |
| 107 |
|
| 108 |
if ($$rsvp$events$$indexOf(callbacks, callback) === -1) { |
| 109 |
callbacks.push(callback); |
| 110 |
} |
| 111 |
}, |
| 112 |
|
| 113 |
/** |
| 114 |
You can use `off` to stop firing a particular callback for an event: |
| 115 |
|
| 116 |
```javascript |
| 117 |
function doStuff() { // do stuff! } |
| 118 |
object.on('stuff', doStuff); |
| 119 |
|
| 120 |
object.trigger('stuff'); // doStuff will be called |
| 121 |
|
| 122 |
// Unregister ONLY the doStuff callback |
| 123 |
object.off('stuff', doStuff); |
| 124 |
object.trigger('stuff'); // doStuff will NOT be called |
| 125 |
``` |
| 126 |
|
| 127 |
If you don't pass a `callback` argument to `off`, ALL callbacks for the |
| 128 |
event will not be executed when the event fires. For example: |
| 129 |
|
| 130 |
```javascript |
| 131 |
var callback1 = function(){}; |
| 132 |
var callback2 = function(){}; |
| 133 |
|
| 134 |
object.on('stuff', callback1); |
| 135 |
object.on('stuff', callback2); |
| 136 |
|
| 137 |
object.trigger('stuff'); // callback1 and callback2 will be executed. |
| 138 |
|
| 139 |
object.off('stuff'); |
| 140 |
object.trigger('stuff'); // callback1 and callback2 will not be executed! |
| 141 |
``` |
| 142 |
|
| 143 |
@method off |
| 144 |
@for RSVP.EventTarget |
| 145 |
@private |
| 146 |
@param {String} eventName event to stop listening to |
| 147 |
@param {Function} callback optional argument. If given, only the function |
| 148 |
given will be removed from the event's callback queue. If no `callback` |
| 149 |
argument is given, all callbacks will be removed from the event's callback |
| 150 |
queue. |
| 151 |
*/ |
| 152 |
off: function(eventName, callback) { |
| 153 |
var allCallbacks = $$rsvp$events$$callbacksFor(this), callbacks, index; |
| 154 |
|
| 155 |
if (!callback) { |
| 156 |
allCallbacks[eventName] = []; |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
callbacks = allCallbacks[eventName]; |
| 161 |
|
| 162 |
index = $$rsvp$events$$indexOf(callbacks, callback); |
| 163 |
|
| 164 |
if (index !== -1) { callbacks.splice(index, 1); } |
| 165 |
}, |
| 166 |
|
| 167 |
/** |
| 168 |
Use `trigger` to fire custom events. For example: |
| 169 |
|
| 170 |
```javascript |
| 171 |
object.on('foo', function(){ |
| 172 |
console.log('foo event happened!'); |
| 173 |
}); |
| 174 |
object.trigger('foo'); |
| 175 |
// 'foo event happened!' logged to the console |
| 176 |
``` |
| 177 |
|
| 178 |
You can also pass a value as a second argument to `trigger` that will be |
| 179 |
passed as an argument to all event listeners for the event: |
| 180 |
|
| 181 |
```javascript |
| 182 |
object.on('foo', function(value){ |
| 183 |
console.log(value.name); |
| 184 |
}); |
| 185 |
|
| 186 |
object.trigger('foo', { name: 'bar' }); |
| 187 |
// 'bar' logged to the console |
| 188 |
``` |
| 189 |
|
| 190 |
@method trigger |
| 191 |
@for RSVP.EventTarget |
| 192 |
@private |
| 193 |
@param {String} eventName name of the event to be triggered |
| 194 |
@param {Any} options optional value to be passed to any event handlers for |
| 195 |
the given `eventName` |
| 196 |
*/ |
| 197 |
trigger: function(eventName, options) { |
| 198 |
var allCallbacks = $$rsvp$events$$callbacksFor(this), callbacks, callback; |
| 199 |
|
| 200 |
if (callbacks = allCallbacks[eventName]) { |
| 201 |
// Don't cache the callbacks.length since it may grow |
| 202 |
for (var i=0; i<callbacks.length; i++) { |
| 203 |
callback = callbacks[i]; |
| 204 |
|
| 205 |
callback(options); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
var $$rsvp$config$$config = { |
| 212 |
instrument: false |
| 213 |
}; |
| 214 |
|
| 215 |
$$rsvp$events$$default.mixin($$rsvp$config$$config); |
| 216 |
|
| 217 |
function $$rsvp$config$$configure(name, value) { |
| 218 |
if (name === 'onerror') { |
| 219 |
// handle for legacy users that expect the actual |
| 220 |
// error to be passed to their function added via |
| 221 |
// `RSVP.configure('onerror', someFunctionHere);` |
| 222 |
$$rsvp$config$$config.on('error', value); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
if (arguments.length === 2) { |
| 227 |
$$rsvp$config$$config[name] = value; |
| 228 |
} else { |
| 229 |
return $$rsvp$config$$config[name]; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
function $$utils$$objectOrFunction(x) { |
| 234 |
return typeof x === 'function' || (typeof x === 'object' && x !== null); |
| 235 |
} |
| 236 |
|
| 237 |
function $$utils$$isFunction(x) { |
| 238 |
return typeof x === 'function'; |
| 239 |
} |
| 240 |
|
| 241 |
function $$utils$$isMaybeThenable(x) { |
| 242 |
return typeof x === 'object' && x !== null; |
| 243 |
} |
| 244 |
|
| 245 |
var $$utils$$_isArray; |
| 246 |
|
| 247 |
if (!Array.isArray) { |
| 248 |
$$utils$$_isArray = function (x) { |
| 249 |
return Object.prototype.toString.call(x) === '[object Array]'; |
| 250 |
}; |
| 251 |
} else { |
| 252 |
$$utils$$_isArray = Array.isArray; |
| 253 |
} |
| 254 |
|
| 255 |
var $$utils$$isArray = $$utils$$_isArray; |
| 256 |
var $$utils$$now = Date.now || function() { return new Date().getTime(); }; |
| 257 |
function $$utils$$F() { } |
| 258 |
|
| 259 |
var $$utils$$o_create = (Object.create || function (o) { |
| 260 |
if (arguments.length > 1) { |
| 261 |
throw new Error('Second argument not supported'); |
| 262 |
} |
| 263 |
if (typeof o !== 'object') { |
| 264 |
throw new TypeError('Argument must be an object'); |
| 265 |
} |
| 266 |
$$utils$$F.prototype = o; |
| 267 |
return new $$utils$$F(); |
| 268 |
}); |
| 269 |
|
| 270 |
var $$instrument$$queue = []; |
| 271 |
|
| 272 |
var $$instrument$$default = function instrument(eventName, promise, child) { |
| 273 |
if (1 === $$instrument$$queue.push({ |
| 274 |
name: eventName, |
| 275 |
payload: { |
| 276 |
guid: promise._guidKey + promise._id, |
| 277 |
eventName: eventName, |
| 278 |
detail: promise._result, |
| 279 |
childGuid: child && promise._guidKey + child._id, |
| 280 |
label: promise._label, |
| 281 |
timeStamp: $$utils$$now(), |
| 282 |
stack: new Error(promise._label).stack |
| 283 |
}})) { |
| 284 |
|
| 285 |
setTimeout(function() { |
| 286 |
var entry; |
| 287 |
for (var i = 0; i < $$instrument$$queue.length; i++) { |
| 288 |
entry = $$instrument$$queue[i]; |
| 289 |
$$rsvp$config$$config.trigger(entry.name, entry.payload); |
| 290 |
} |
| 291 |
$$instrument$$queue.length = 0; |
| 292 |
}, 50); |
| 293 |
} |
| 294 |
}; |
| 295 |
|
| 296 |
function $$$internal$$noop() {} |
| 297 |
var $$$internal$$PENDING = void 0; |
| 298 |
var $$$internal$$FULFILLED = 1; |
| 299 |
var $$$internal$$REJECTED = 2; |
| 300 |
var $$$internal$$GET_THEN_ERROR = new $$$internal$$ErrorObject(); |
| 301 |
|
| 302 |
function $$$internal$$getThen(promise) { |
| 303 |
try { |
| 304 |
return promise.then; |
| 305 |
} catch(error) { |
| 306 |
$$$internal$$GET_THEN_ERROR.error = error; |
| 307 |
return $$$internal$$GET_THEN_ERROR; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
function $$$internal$$tryThen(then, value, fulfillmentHandler, rejectionHandler) { |
| 312 |
try { |
| 313 |
then.call(value, fulfillmentHandler, rejectionHandler); |
| 314 |
} catch(e) { |
| 315 |
return e; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
function $$$internal$$handleForeignThenable(promise, thenable, then) { |
| 320 |
$$rsvp$config$$config.async(function(promise) { |
| 321 |
var sealed = false; |
| 322 |
var error = $$$internal$$tryThen(then, thenable, function(value) { |
| 323 |
if (sealed) { return; } |
| 324 |
sealed = true; |
| 325 |
if (thenable !== value) { |
| 326 |
$$$internal$$resolve(promise, value); |
| 327 |
} else { |
| 328 |
$$$internal$$fulfill(promise, value); |
| 329 |
} |
| 330 |
}, function(reason) { |
| 331 |
if (sealed) { return; } |
| 332 |
sealed = true; |
| 333 |
|
| 334 |
$$$internal$$reject(promise, reason); |
| 335 |
}, 'Settle: ' + (promise._label || ' unknown promise')); |
| 336 |
|
| 337 |
if (!sealed && error) { |
| 338 |
sealed = true; |
| 339 |
$$$internal$$reject(promise, error); |
| 340 |
} |
| 341 |
}, promise); |
| 342 |
} |
| 343 |
|
| 344 |
function $$$internal$$handleOwnThenable(promise, thenable) { |
| 345 |
if (thenable._state === $$$internal$$FULFILLED) { |
| 346 |
$$$internal$$fulfill(promise, thenable._result); |
| 347 |
} else if (promise._state === $$$internal$$REJECTED) { |
| 348 |
$$$internal$$reject(promise, thenable._result); |
| 349 |
} else { |
| 350 |
$$$internal$$subscribe(thenable, undefined, function(value) { |
| 351 |
if (thenable !== value) { |
| 352 |
$$$internal$$resolve(promise, value); |
| 353 |
} else { |
| 354 |
$$$internal$$fulfill(promise, value); |
| 355 |
} |
| 356 |
}, function(reason) { |
| 357 |
$$$internal$$reject(promise, reason); |
| 358 |
}); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
function $$$internal$$handleMaybeThenable(promise, maybeThenable) { |
| 363 |
if (maybeThenable.constructor === promise.constructor) { |
| 364 |
$$$internal$$handleOwnThenable(promise, maybeThenable); |
| 365 |
} else { |
| 366 |
var then = $$$internal$$getThen(maybeThenable); |
| 367 |
|
| 368 |
if (then === $$$internal$$GET_THEN_ERROR) { |
| 369 |
$$$internal$$reject(promise, $$$internal$$GET_THEN_ERROR.error); |
| 370 |
} else if (then === undefined) { |
| 371 |
$$$internal$$fulfill(promise, maybeThenable); |
| 372 |
} else if ($$utils$$isFunction(then)) { |
| 373 |
$$$internal$$handleForeignThenable(promise, maybeThenable, then); |
| 374 |
} else { |
| 375 |
$$$internal$$fulfill(promise, maybeThenable); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
function $$$internal$$resolve(promise, value) { |
| 381 |
if (promise === value) { |
| 382 |
$$$internal$$fulfill(promise, value); |
| 383 |
} else if ($$utils$$objectOrFunction(value)) { |
| 384 |
$$$internal$$handleMaybeThenable(promise, value); |
| 385 |
} else { |
| 386 |
$$$internal$$fulfill(promise, value); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
function $$$internal$$publishRejection(promise) { |
| 391 |
if (promise._onerror) { |
| 392 |
promise._onerror(promise._result); |
| 393 |
} |
| 394 |
|
| 395 |
$$$internal$$publish(promise); |
| 396 |
} |
| 397 |
|
| 398 |
function $$$internal$$fulfill(promise, value) { |
| 399 |
if (promise._state !== $$$internal$$PENDING) { return; } |
| 400 |
|
| 401 |
promise._result = value; |
| 402 |
promise._state = $$$internal$$FULFILLED; |
| 403 |
|
| 404 |
if (promise._subscribers.length === 0) { |
| 405 |
if ($$rsvp$config$$config.instrument) { |
| 406 |
$$instrument$$default('fulfilled', promise); |
| 407 |
} |
| 408 |
} else { |
| 409 |
$$rsvp$config$$config.async($$$internal$$publish, promise); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
function $$$internal$$reject(promise, reason) { |
| 414 |
if (promise._state !== $$$internal$$PENDING) { return; } |
| 415 |
promise._state = $$$internal$$REJECTED; |
| 416 |
promise._result = reason; |
| 417 |
|
| 418 |
$$rsvp$config$$config.async($$$internal$$publishRejection, promise); |
| 419 |
} |
| 420 |
|
| 421 |
function $$$internal$$subscribe(parent, child, onFulfillment, onRejection) { |
| 422 |
var subscribers = parent._subscribers; |
| 423 |
var length = subscribers.length; |
| 424 |
|
| 425 |
parent._onerror = null; |
| 426 |
|
| 427 |
subscribers[length] = child; |
| 428 |
subscribers[length + $$$internal$$FULFILLED] = onFulfillment; |
| 429 |
subscribers[length + $$$internal$$REJECTED] = onRejection; |
| 430 |
|
| 431 |
if (length === 0 && parent._state) { |
| 432 |
$$rsvp$config$$config.async($$$internal$$publish, parent); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
function $$$internal$$publish(promise) { |
| 437 |
var subscribers = promise._subscribers; |
| 438 |
var settled = promise._state; |
| 439 |
|
| 440 |
if ($$rsvp$config$$config.instrument) { |
| 441 |
$$instrument$$default(settled === $$$internal$$FULFILLED ? 'fulfilled' : 'rejected', promise); |
| 442 |
} |
| 443 |
|
| 444 |
if (subscribers.length === 0) { return; } |
| 445 |
|
| 446 |
var child, callback, detail = promise._result; |
| 447 |
|
| 448 |
for (var i = 0; i < subscribers.length; i += 3) { |
| 449 |
child = subscribers[i]; |
| 450 |
callback = subscribers[i + settled]; |
| 451 |
|
| 452 |
if (child) { |
| 453 |
$$$internal$$invokeCallback(settled, child, callback, detail); |
| 454 |
} else { |
| 455 |
callback(detail); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
promise._subscribers.length = 0; |
| 460 |
} |
| 461 |
|
| 462 |
function $$$internal$$ErrorObject() { |
| 463 |
this.error = null; |
| 464 |
} |
| 465 |
|
| 466 |
var $$$internal$$TRY_CATCH_ERROR = new $$$internal$$ErrorObject(); |
| 467 |
|
| 468 |
function $$$internal$$tryCatch(callback, detail) { |
| 469 |
try { |
| 470 |
return callback(detail); |
| 471 |
} catch(e) { |
| 472 |
$$$internal$$TRY_CATCH_ERROR.error = e; |
| 473 |
return $$$internal$$TRY_CATCH_ERROR; |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
function $$$internal$$invokeCallback(settled, promise, callback, detail) { |
| 478 |
var hasCallback = $$utils$$isFunction(callback), |
| 479 |
value, error, succeeded, failed; |
| 480 |
|
| 481 |
if (hasCallback) { |
| 482 |
value = $$$internal$$tryCatch(callback, detail); |
| 483 |
|
| 484 |
if (value === $$$internal$$TRY_CATCH_ERROR) { |
| 485 |
failed = true; |
| 486 |
error = value.error; |
| 487 |
value = null; |
| 488 |
} else { |
| 489 |
succeeded = true; |
| 490 |
} |
| 491 |
|
| 492 |
if (promise === value) { |
| 493 |
$$$internal$$reject(promise, new TypeError('A promises callback cannot return that same promise.')); |
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
} else { |
| 498 |
value = detail; |
| 499 |
succeeded = true; |
| 500 |
} |
| 501 |
|
| 502 |
if (promise._state !== $$$internal$$PENDING) { |
| 503 |
// noop |
| 504 |
} else if (hasCallback && succeeded) { |
| 505 |
$$$internal$$resolve(promise, value); |
| 506 |
} else if (failed) { |
| 507 |
$$$internal$$reject(promise, error); |
| 508 |
} else if (settled === $$$internal$$FULFILLED) { |
| 509 |
$$$internal$$fulfill(promise, value); |
| 510 |
} else if (settled === $$$internal$$REJECTED) { |
| 511 |
$$$internal$$reject(promise, value); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
function $$$internal$$initializePromise(promise, resolver) { |
| 516 |
try { |
| 517 |
resolver(function resolvePromise(value){ |
| 518 |
$$$internal$$resolve(promise, value); |
| 519 |
}, function rejectPromise(reason) { |
| 520 |
$$$internal$$reject(promise, reason); |
| 521 |
}); |
| 522 |
} catch(e) { |
| 523 |
$$$internal$$reject(promise, e); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
function $$enumerator$$makeSettledResult(state, position, value) { |
| 528 |
if (state === $$$internal$$FULFILLED) { |
| 529 |
return { |
| 530 |
state: 'fulfilled', |
| 531 |
value: value |
| 532 |
}; |
| 533 |
} else { |
| 534 |
return { |
| 535 |
state: 'rejected', |
| 536 |
reason: value |
| 537 |
}; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
function $$enumerator$$Enumerator(Constructor, input, abortOnReject, label) { |
| 542 |
this._instanceConstructor = Constructor; |
| 543 |
this.promise = new Constructor($$$internal$$noop, label); |
| 544 |
this._abortOnReject = abortOnReject; |
| 545 |
|
| 546 |
if (this._validateInput(input)) { |
| 547 |
this._input = input; |
| 548 |
this.length = input.length; |
| 549 |
this._remaining = input.length; |
| 550 |
|
| 551 |
this._init(); |
| 552 |
|
| 553 |
if (this.length === 0) { |
| 554 |
$$$internal$$fulfill(this.promise, this._result); |
| 555 |
} else { |
| 556 |
this.length = this.length || 0; |
| 557 |
this._enumerate(); |
| 558 |
if (this._remaining === 0) { |
| 559 |
$$$internal$$fulfill(this.promise, this._result); |
| 560 |
} |
| 561 |
} |
| 562 |
} else { |
| 563 |
$$$internal$$reject(this.promise, this._validationError()); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
$$enumerator$$Enumerator.prototype._validateInput = function(input) { |
| 568 |
return $$utils$$isArray(input); |
| 569 |
}; |
| 570 |
|
| 571 |
$$enumerator$$Enumerator.prototype._validationError = function() { |
| 572 |
return new Error('Array Methods must be provided an Array'); |
| 573 |
}; |
| 574 |
|
| 575 |
$$enumerator$$Enumerator.prototype._init = function() { |
| 576 |
this._result = new Array(this.length); |
| 577 |
}; |
| 578 |
|
| 579 |
var $$enumerator$$default = $$enumerator$$Enumerator; |
| 580 |
|
| 581 |
$$enumerator$$Enumerator.prototype._enumerate = function() { |
| 582 |
var length = this.length; |
| 583 |
var promise = this.promise; |
| 584 |
var input = this._input; |
| 585 |
|
| 586 |
for (var i = 0; promise._state === $$$internal$$PENDING && i < length; i++) { |
| 587 |
this._eachEntry(input[i], i); |
| 588 |
} |
| 589 |
}; |
| 590 |
|
| 591 |
$$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) { |
| 592 |
var c = this._instanceConstructor; |
| 593 |
if ($$utils$$isMaybeThenable(entry)) { |
| 594 |
if (entry.constructor === c && entry._state !== $$$internal$$PENDING) { |
| 595 |
entry._onerror = null; |
| 596 |
this._settledAt(entry._state, i, entry._result); |
| 597 |
} else { |
| 598 |
this._willSettleAt(c.resolve(entry), i); |
| 599 |
} |
| 600 |
} else { |
| 601 |
this._remaining--; |
| 602 |
this._result[i] = this._makeResult($$$internal$$FULFILLED, i, entry); |
| 603 |
} |
| 604 |
}; |
| 605 |
|
| 606 |
$$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) { |
| 607 |
var promise = this.promise; |
| 608 |
|
| 609 |
if (promise._state === $$$internal$$PENDING) { |
| 610 |
this._remaining--; |
| 611 |
|
| 612 |
if (this._abortOnReject && state === $$$internal$$REJECTED) { |
| 613 |
$$$internal$$reject(promise, value); |
| 614 |
} else { |
| 615 |
this._result[i] = this._makeResult(state, i, value); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
if (this._remaining === 0) { |
| 620 |
$$$internal$$fulfill(promise, this._result); |
| 621 |
} |
| 622 |
}; |
| 623 |
|
| 624 |
$$enumerator$$Enumerator.prototype._makeResult = function(state, i, value) { |
| 625 |
return value; |
| 626 |
}; |
| 627 |
|
| 628 |
$$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) { |
| 629 |
var enumerator = this; |
| 630 |
|
| 631 |
$$$internal$$subscribe(promise, undefined, function(value) { |
| 632 |
enumerator._settledAt($$$internal$$FULFILLED, i, value); |
| 633 |
}, function(reason) { |
| 634 |
enumerator._settledAt($$$internal$$REJECTED, i, reason); |
| 635 |
}); |
| 636 |
}; |
| 637 |
|
| 638 |
var $$promise$all$$default = function all(entries, label) { |
| 639 |
return new $$enumerator$$default(this, entries, true /* abort on reject */, label).promise; |
| 640 |
}; |
| 641 |
|
| 642 |
var $$promise$race$$default = function race(entries, label) { |
| 643 |
/*jshint validthis:true */ |
| 644 |
var Constructor = this; |
| 645 |
|
| 646 |
var promise = new Constructor($$$internal$$noop, label); |
| 647 |
|
| 648 |
if (!$$utils$$isArray(entries)) { |
| 649 |
$$$internal$$reject(promise, new TypeError('You must pass an array to race.')); |
| 650 |
return promise; |
| 651 |
} |
| 652 |
|
| 653 |
var length = entries.length; |
| 654 |
|
| 655 |
function onFulfillment(value) { |
| 656 |
$$$internal$$resolve(promise, value); |
| 657 |
} |
| 658 |
|
| 659 |
function onRejection(reason) { |
| 660 |
$$$internal$$reject(promise, reason); |
| 661 |
} |
| 662 |
|
| 663 |
for (var i = 0; promise._state === $$$internal$$PENDING && i < length; i++) { |
| 664 |
$$$internal$$subscribe(Constructor.resolve(entries[i]), undefined, onFulfillment, onRejection); |
| 665 |
} |
| 666 |
|
| 667 |
return promise; |
| 668 |
}; |
| 669 |
|
| 670 |
var $$promise$resolve$$default = function resolve(object, label) { |
| 671 |
/*jshint validthis:true */ |
| 672 |
var Constructor = this; |
| 673 |
|
| 674 |
if (object && typeof object === 'object' && object.constructor === Constructor) { |
| 675 |
return object; |
| 676 |
} |
| 677 |
|
| 678 |
var promise = new Constructor($$$internal$$noop, label); |
| 679 |
$$$internal$$resolve(promise, object); |
| 680 |
return promise; |
| 681 |
}; |
| 682 |
|
| 683 |
var $$promise$reject$$default = function reject(reason, label) { |
| 684 |
/*jshint validthis:true */ |
| 685 |
var Constructor = this; |
| 686 |
var promise = new Constructor($$$internal$$noop, label); |
| 687 |
$$$internal$$reject(promise, reason); |
| 688 |
return promise; |
| 689 |
}; |
| 690 |
|
| 691 |
var $$rsvp$promise$$guidKey = 'rsvp_' + $$utils$$now() + '-'; |
| 692 |
var $$rsvp$promise$$counter = 0; |
| 693 |
|
| 694 |
function $$rsvp$promise$$needsResolver() { |
| 695 |
throw new TypeError('You must pass a resolver function as the first argument to the promise constructor'); |
| 696 |
} |
| 697 |
|
| 698 |
function $$rsvp$promise$$needsNew() { |
| 699 |
throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function."); |
| 700 |
} |
| 701 |
|
| 702 |
var $$rsvp$promise$$default = $$rsvp$promise$$Promise; |
| 703 |
|
| 704 |
/** |
| 705 |
Promise objects represent the eventual result of an asynchronous operation. The |
| 706 |
primary way of interacting with a promise is through its `then` method, which |
| 707 |
registers callbacks to receive either a promise’s eventual value or the reason |
| 708 |
why the promise cannot be fulfilled. |
| 709 |
|
| 710 |
Terminology |
| 711 |
----------- |
| 712 |
|
| 713 |
- `promise` is an object or function with a `then` method whose behavior conforms to this specification. |
| 714 |
- `thenable` is an object or function that defines a `then` method. |
| 715 |
- `value` is any legal JavaScript value (including undefined, a thenable, or a promise). |
| 716 |
- `exception` is a value that is thrown using the throw statement. |
| 717 |
- `reason` is a value that indicates why a promise was rejected. |
| 718 |
- `settled` the final resting state of a promise, fulfilled or rejected. |
| 719 |
|
| 720 |
A promise can be in one of three states: pending, fulfilled, or rejected. |
| 721 |
|
| 722 |
Promises that are fulfilled have a fulfillment value and are in the fulfilled |
| 723 |
state. Promises that are rejected have a rejection reason and are in the |
| 724 |
rejected state. A fulfillment value is never a thenable. |
| 725 |
|
| 726 |
Promises can also be said to *resolve* a value. If this value is also a |
| 727 |
promise, then the original promise's settled state will match the value's |
| 728 |
settled state. So a promise that *resolves* a promise that rejects will |
| 729 |
itself reject, and a promise that *resolves* a promise that fulfills will |
| 730 |
itself fulfill. |
| 731 |
|
| 732 |
|
| 733 |
Basic Usage: |
| 734 |
------------ |
| 735 |
|
| 736 |
```js |
| 737 |
var promise = new Promise(function(resolve, reject) { |
| 738 |
// on success |
| 739 |
resolve(value); |
| 740 |
|
| 741 |
// on failure |
| 742 |
reject(reason); |
| 743 |
}); |
| 744 |
|
| 745 |
promise.then(function(value) { |
| 746 |
// on fulfillment |
| 747 |
}, function(reason) { |
| 748 |
// on rejection |
| 749 |
}); |
| 750 |
``` |
| 751 |
|
| 752 |
Advanced Usage: |
| 753 |
--------------- |
| 754 |
|
| 755 |
Promises shine when abstracting away asynchronous interactions such as |
| 756 |
`XMLHttpRequest`s. |
| 757 |
|
| 758 |
```js |
| 759 |
function getJSON(url) { |
| 760 |
return new Promise(function(resolve, reject){ |
| 761 |
var xhr = new XMLHttpRequest(); |
| 762 |
|
| 763 |
xhr.open('GET', url); |
| 764 |
xhr.onreadystatechange = handler; |
| 765 |
xhr.responseType = 'json'; |
| 766 |
xhr.setRequestHeader('Accept', 'application/json'); |
| 767 |
xhr.send(); |
| 768 |
|
| 769 |
function handler() { |
| 770 |
if (this.readyState === this.DONE) { |
| 771 |
if (this.status === 200) { |
| 772 |
resolve(this.response); |
| 773 |
} else { |
| 774 |
reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']')); |
| 775 |
} |
| 776 |
} |
| 777 |
}; |
| 778 |
}); |
| 779 |
} |
| 780 |
|
| 781 |
getJSON('/posts.json').then(function(json) { |
| 782 |
// on fulfillment |
| 783 |
}, function(reason) { |
| 784 |
// on rejection |
| 785 |
}); |
| 786 |
``` |
| 787 |
|
| 788 |
Unlike callbacks, promises are great composable primitives. |
| 789 |
|
| 790 |
```js |
| 791 |
Promise.all([ |
| 792 |
getJSON('/posts'), |
| 793 |
getJSON('/comments') |
| 794 |
]).then(function(values){ |
| 795 |
values[0] // => postsJSON |
| 796 |
values[1] // => commentsJSON |
| 797 |
|
| 798 |
return values; |
| 799 |
}); |
| 800 |
``` |
| 801 |
|
| 802 |
@class RSVP.Promise |
| 803 |
@param {function} resolver |
| 804 |
@param {String} label optional string for labeling the promise. |
| 805 |
Useful for tooling. |
| 806 |
@constructor |
| 807 |
*/ |
| 808 |
function $$rsvp$promise$$Promise(resolver, label) { |
| 809 |
this._id = $$rsvp$promise$$counter++; |
| 810 |
this._label = label; |
| 811 |
this._state = undefined; |
| 812 |
this._result = undefined; |
| 813 |
this._subscribers = []; |
| 814 |
|
| 815 |
if ($$rsvp$config$$config.instrument) { |
| 816 |
$$instrument$$default('created', this); |
| 817 |
} |
| 818 |
|
| 819 |
if ($$$internal$$noop !== resolver) { |
| 820 |
if (!$$utils$$isFunction(resolver)) { |
| 821 |
$$rsvp$promise$$needsResolver(); |
| 822 |
} |
| 823 |
|
| 824 |
if (!(this instanceof $$rsvp$promise$$Promise)) { |
| 825 |
$$rsvp$promise$$needsNew(); |
| 826 |
} |
| 827 |
|
| 828 |
$$$internal$$initializePromise(this, resolver); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
// deprecated |
| 833 |
$$rsvp$promise$$Promise.cast = $$promise$resolve$$default; |
| 834 |
|
| 835 |
$$rsvp$promise$$Promise.all = $$promise$all$$default; |
| 836 |
$$rsvp$promise$$Promise.race = $$promise$race$$default; |
| 837 |
$$rsvp$promise$$Promise.resolve = $$promise$resolve$$default; |
| 838 |
$$rsvp$promise$$Promise.reject = $$promise$reject$$default; |
| 839 |
|
| 840 |
$$rsvp$promise$$Promise.prototype = { |
| 841 |
constructor: $$rsvp$promise$$Promise, |
| 842 |
|
| 843 |
_guidKey: $$rsvp$promise$$guidKey, |
| 844 |
|
| 845 |
_onerror: function (reason) { |
| 846 |
$$rsvp$config$$config.trigger('error', reason); |
| 847 |
}, |
| 848 |
|
| 849 |
/** |
| 850 |
The primary way of interacting with a promise is through its `then` method, |
| 851 |
which registers callbacks to receive either a promise's eventual value or the |
| 852 |
reason why the promise cannot be fulfilled. |
| 853 |
|
| 854 |
```js |
| 855 |
findUser().then(function(user){ |
| 856 |
// user is available |
| 857 |
}, function(reason){ |
| 858 |
// user is unavailable, and you are given the reason why |
| 859 |
}); |
| 860 |
``` |
| 861 |
|
| 862 |
Chaining |
| 863 |
-------- |
| 864 |
|
| 865 |
The return value of `then` is itself a promise. This second, 'downstream' |
| 866 |
promise is resolved with the return value of the first promise's fulfillment |
| 867 |
or rejection handler, or rejected if the handler throws an exception. |
| 868 |
|
| 869 |
```js |
| 870 |
findUser().then(function (user) { |
| 871 |
return user.name; |
| 872 |
}, function (reason) { |
| 873 |
return 'default name'; |
| 874 |
}).then(function (userName) { |
| 875 |
// If `findUser` fulfilled, `userName` will be the user's name, otherwise it |
| 876 |
// will be `'default name'` |
| 877 |
}); |
| 878 |
|
| 879 |
findUser().then(function (user) { |
| 880 |
throw new Error('Found user, but still unhappy'); |
| 881 |
}, function (reason) { |
| 882 |
throw new Error('`findUser` rejected and we're unhappy'); |
| 883 |
}).then(function (value) { |
| 884 |
// never reached |
| 885 |
}, function (reason) { |
| 886 |
// if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'. |
| 887 |
// If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'. |
| 888 |
}); |
| 889 |
``` |
| 890 |
If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream. |
| 891 |
|
| 892 |
```js |
| 893 |
findUser().then(function (user) { |
| 894 |
throw new PedagogicalException('Upstream error'); |
| 895 |
}).then(function (value) { |
| 896 |
// never reached |
| 897 |
}).then(function (value) { |
| 898 |
// never reached |
| 899 |
}, function (reason) { |
| 900 |
// The `PedgagocialException` is propagated all the way down to here |
| 901 |
}); |
| 902 |
``` |
| 903 |
|
| 904 |
Assimilation |
| 905 |
------------ |
| 906 |
|
| 907 |
Sometimes the value you want to propagate to a downstream promise can only be |
| 908 |
retrieved asynchronously. This can be achieved by returning a promise in the |
| 909 |
fulfillment or rejection handler. The downstream promise will then be pending |
| 910 |
until the returned promise is settled. This is called *assimilation*. |
| 911 |
|
| 912 |
```js |
| 913 |
findUser().then(function (user) { |
| 914 |
return findCommentsByAuthor(user); |
| 915 |
}).then(function (comments) { |
| 916 |
// The user's comments are now available |
| 917 |
}); |
| 918 |
``` |
| 919 |
|
| 920 |
If the assimliated promise rejects, then the downstream promise will also reject. |
| 921 |
|
| 922 |
```js |
| 923 |
findUser().then(function (user) { |
| 924 |
return findCommentsByAuthor(user); |
| 925 |
}).then(function (comments) { |
| 926 |
// If `findCommentsByAuthor` fulfills, we'll have the value here |
| 927 |
}, function (reason) { |
| 928 |
// If `findCommentsByAuthor` rejects, we'll have the reason here |
| 929 |
}); |
| 930 |
``` |
| 931 |
|
| 932 |
Simple Example |
| 933 |
-------------- |
| 934 |
|
| 935 |
Synchronous Example |
| 936 |
|
| 937 |
```javascript |
| 938 |
var result; |
| 939 |
|
| 940 |
try { |
| 941 |
result = findResult(); |
| 942 |
// success |
| 943 |
} catch(reason) { |
| 944 |
// failure |
| 945 |
} |
| 946 |
``` |
| 947 |
|
| 948 |
Errback Example |
| 949 |
|
| 950 |
```js |
| 951 |
findResult(function(result, err){ |
| 952 |
if (err) { |
| 953 |
// failure |
| 954 |
} else { |
| 955 |
// success |
| 956 |
} |
| 957 |
}); |
| 958 |
``` |
| 959 |
|
| 960 |
Promise Example; |
| 961 |
|
| 962 |
```javascript |
| 963 |
findResult().then(function(result){ |
| 964 |
// success |
| 965 |
}, function(reason){ |
| 966 |
// failure |
| 967 |
}); |
| 968 |
``` |
| 969 |
|
| 970 |
Advanced Example |
| 971 |
-------------- |
| 972 |
|
| 973 |
Synchronous Example |
| 974 |
|
| 975 |
```javascript |
| 976 |
var author, books; |
| 977 |
|
| 978 |
try { |
| 979 |
author = findAuthor(); |
| 980 |
books = findBooksByAuthor(author); |
| 981 |
// success |
| 982 |
} catch(reason) { |
| 983 |
// failure |
| 984 |
} |
| 985 |
``` |
| 986 |
|
| 987 |
Errback Example |
| 988 |
|
| 989 |
```js |
| 990 |
|
| 991 |
function foundBooks(books) { |
| 992 |
|
| 993 |
} |
| 994 |
|
| 995 |
function failure(reason) { |
| 996 |
|
| 997 |
} |
| 998 |
|
| 999 |
findAuthor(function(author, err){ |
| 1000 |
if (err) { |
| 1001 |
failure(err); |
| 1002 |
// failure |
| 1003 |
} else { |
| 1004 |
try { |
| 1005 |
findBoooksByAuthor(author, function(books, err) { |
| 1006 |
if (err) { |
| 1007 |
failure(err); |
| 1008 |
} else { |
| 1009 |
try { |
| 1010 |
foundBooks(books); |
| 1011 |
} catch(reason) { |
| 1012 |
failure(reason); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
}); |
| 1016 |
} catch(error) { |
| 1017 |
failure(err); |
| 1018 |
} |
| 1019 |
// success |
| 1020 |
} |
| 1021 |
}); |
| 1022 |
``` |
| 1023 |
|
| 1024 |
Promise Example; |
| 1025 |
|
| 1026 |
```javascript |
| 1027 |
findAuthor(). |
| 1028 |
then(findBooksByAuthor). |
| 1029 |
then(function(books){ |
| 1030 |
// found books |
| 1031 |
}).catch(function(reason){ |
| 1032 |
// something went wrong |
| 1033 |
}); |
| 1034 |
``` |
| 1035 |
|
| 1036 |
@method then |
| 1037 |
@param {Function} onFulfilled |
| 1038 |
@param {Function} onRejected |
| 1039 |
@param {String} label optional string for labeling the promise. |
| 1040 |
Useful for tooling. |
| 1041 |
@return {Promise} |
| 1042 |
*/ |
| 1043 |
then: function(onFulfillment, onRejection, label) { |
| 1044 |
var parent = this; |
| 1045 |
var state = parent._state; |
| 1046 |
|
| 1047 |
if (state === $$$internal$$FULFILLED && !onFulfillment || state === $$$internal$$REJECTED && !onRejection) { |
| 1048 |
if ($$rsvp$config$$config.instrument) { |
| 1049 |
$$instrument$$default('chained', this, this); |
| 1050 |
} |
| 1051 |
return this; |
| 1052 |
} |
| 1053 |
|
| 1054 |
parent._onerror = null; |
| 1055 |
|
| 1056 |
var child = new this.constructor($$$internal$$noop, label); |
| 1057 |
var result = parent._result; |
| 1058 |
|
| 1059 |
if ($$rsvp$config$$config.instrument) { |
| 1060 |
$$instrument$$default('chained', parent, child); |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (state) { |
| 1064 |
var callback = arguments[state - 1]; |
| 1065 |
$$rsvp$config$$config.async(function(){ |
| 1066 |
$$$internal$$invokeCallback(state, child, callback, result); |
| 1067 |
}); |
| 1068 |
} else { |
| 1069 |
$$$internal$$subscribe(parent, child, onFulfillment, onRejection); |
| 1070 |
} |
| 1071 |
|
| 1072 |
return child; |
| 1073 |
}, |
| 1074 |
|
| 1075 |
/** |
| 1076 |
`catch` is simply sugar for `then(undefined, onRejection)` which makes it the same |
| 1077 |
as the catch block of a try/catch statement. |
| 1078 |
|
| 1079 |
```js |
| 1080 |
function findAuthor(){ |
| 1081 |
throw new Error('couldn't find that author'); |
| 1082 |
} |
| 1083 |
|
| 1084 |
// synchronous |
| 1085 |
try { |
| 1086 |
findAuthor(); |
| 1087 |
} catch(reason) { |
| 1088 |
// something went wrong |
| 1089 |
} |
| 1090 |
|
| 1091 |
// async with promises |
| 1092 |
findAuthor().catch(function(reason){ |
| 1093 |
// something went wrong |
| 1094 |
}); |
| 1095 |
``` |
| 1096 |
|
| 1097 |
@method catch |
| 1098 |
@param {Function} onRejection |
| 1099 |
@param {String} label optional string for labeling the promise. |
| 1100 |
Useful for tooling. |
| 1101 |
@return {Promise} |
| 1102 |
*/ |
| 1103 |
'catch': function(onRejection, label) { |
| 1104 |
return this.then(null, onRejection, label); |
| 1105 |
}, |
| 1106 |
|
| 1107 |
/** |
| 1108 |
`finally` will be invoked regardless of the promise's fate just as native |
| 1109 |
try/catch/finally behaves |
| 1110 |
|
| 1111 |
Synchronous example: |
| 1112 |
|
| 1113 |
```js |
| 1114 |
findAuthor() { |
| 1115 |
if (Math.random() > 0.5) { |
| 1116 |
throw new Error(); |
| 1117 |
} |
| 1118 |
return new Author(); |
| 1119 |
} |
| 1120 |
|
| 1121 |
try { |
| 1122 |
return findAuthor(); // succeed or fail |
| 1123 |
} catch(error) { |
| 1124 |
return findOtherAuther(); |
| 1125 |
} finally { |
| 1126 |
// always runs |
| 1127 |
// doesn't affect the return value |
| 1128 |
} |
| 1129 |
``` |
| 1130 |
|
| 1131 |
Asynchronous example: |
| 1132 |
|
| 1133 |
```js |
| 1134 |
findAuthor().catch(function(reason){ |
| 1135 |
return findOtherAuther(); |
| 1136 |
}).finally(function(){ |
| 1137 |
// author was either found, or not |
| 1138 |
}); |
| 1139 |
``` |
| 1140 |
|
| 1141 |
@method finally |
| 1142 |
@param {Function} callback |
| 1143 |
@param {String} label optional string for labeling the promise. |
| 1144 |
Useful for tooling. |
| 1145 |
@return {Promise} |
| 1146 |
*/ |
| 1147 |
'finally': function(callback, label) { |
| 1148 |
var constructor = this.constructor; |
| 1149 |
|
| 1150 |
return this.then(function(value) { |
| 1151 |
return constructor.resolve(callback()).then(function(){ |
| 1152 |
return value; |
| 1153 |
}); |
| 1154 |
}, function(reason) { |
| 1155 |
return constructor.resolve(callback()).then(function(){ |
| 1156 |
throw reason; |
| 1157 |
}); |
| 1158 |
}, label); |
| 1159 |
} |
| 1160 |
}; |
| 1161 |
|
| 1162 |
function $$rsvp$node$$Result() { |
| 1163 |
this.value = undefined; |
| 1164 |
} |
| 1165 |
|
| 1166 |
var $$rsvp$node$$ERROR = new $$rsvp$node$$Result(); |
| 1167 |
var $$rsvp$node$$GET_THEN_ERROR = new $$rsvp$node$$Result(); |
| 1168 |
|
| 1169 |
function $$rsvp$node$$getThen(obj) { |
| 1170 |
try { |
| 1171 |
return obj.then; |
| 1172 |
} catch(error) { |
| 1173 |
$$rsvp$node$$ERROR.value= error; |
| 1174 |
return $$rsvp$node$$ERROR; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
function $$rsvp$node$$tryApply(f, s, a) { |
| 1179 |
try { |
| 1180 |
f.apply(s, a); |
| 1181 |
} catch(error) { |
| 1182 |
$$rsvp$node$$ERROR.value = error; |
| 1183 |
return $$rsvp$node$$ERROR; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
function $$rsvp$node$$makeObject(_, argumentNames) { |
| 1188 |
var obj = {}; |
| 1189 |
var name; |
| 1190 |
var i; |
| 1191 |
var length = _.length; |
| 1192 |
var args = new Array(length); |
| 1193 |
|
| 1194 |
for (var x = 0; x < length; x++) { |
| 1195 |
args[x] = _[x]; |
| 1196 |
} |
| 1197 |
|
| 1198 |
for (i = 0; i < argumentNames.length; i++) { |
| 1199 |
name = argumentNames[i]; |
| 1200 |
obj[name] = args[i + 1]; |
| 1201 |
} |
| 1202 |
|
| 1203 |
return obj; |
| 1204 |
} |
| 1205 |
|
| 1206 |
function $$rsvp$node$$arrayResult(_) { |
| 1207 |
var length = _.length; |
| 1208 |
var args = new Array(length - 1); |
| 1209 |
|
| 1210 |
for (var i = 1; i < length; i++) { |
| 1211 |
args[i - 1] = _[i]; |
| 1212 |
} |
| 1213 |
|
| 1214 |
return args; |
| 1215 |
} |
| 1216 |
|
| 1217 |
function $$rsvp$node$$wrapThenable(then, promise) { |
| 1218 |
return { |
| 1219 |
then: function(onFulFillment, onRejection) { |
| 1220 |
return then.call(promise, onFulFillment, onRejection); |
| 1221 |
} |
| 1222 |
}; |
| 1223 |
} |
| 1224 |
|
| 1225 |
var $$rsvp$node$$default = function denodeify(nodeFunc, options) { |
| 1226 |
var fn = function() { |
| 1227 |
var self = this; |
| 1228 |
var l = arguments.length; |
| 1229 |
var args = new Array(l + 1); |
| 1230 |
var arg; |
| 1231 |
var promiseInput = false; |
| 1232 |
|
| 1233 |
for (var i = 0; i < l; ++i) { |
| 1234 |
arg = arguments[i]; |
| 1235 |
|
| 1236 |
if (!promiseInput) { |
| 1237 |
// TODO: clean this up |
| 1238 |
promiseInput = $$rsvp$node$$needsPromiseInput(arg); |
| 1239 |
if (promiseInput === $$rsvp$node$$GET_THEN_ERROR) { |
| 1240 |
var p = new $$rsvp$promise$$default($$$internal$$noop); |
| 1241 |
$$$internal$$reject(p, $$rsvp$node$$GET_THEN_ERROR.value); |
| 1242 |
return p; |
| 1243 |
} else if (promiseInput && promiseInput !== true) { |
| 1244 |
arg = $$rsvp$node$$wrapThenable(promiseInput, arg); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
args[i] = arg; |
| 1248 |
} |
| 1249 |
|
| 1250 |
var promise = new $$rsvp$promise$$default($$$internal$$noop); |
| 1251 |
|
| 1252 |
args[l] = function(err, val) { |
| 1253 |
if (err) |
| 1254 |
$$$internal$$reject(promise, err); |
| 1255 |
else if (options === undefined) |
| 1256 |
$$$internal$$resolve(promise, val); |
| 1257 |
else if (options === true) |
| 1258 |
$$$internal$$resolve(promise, $$rsvp$node$$arrayResult(arguments)); |
| 1259 |
else if ($$utils$$isArray(options)) |
| 1260 |
$$$internal$$resolve(promise, $$rsvp$node$$makeObject(arguments, options)); |
| 1261 |
else |
| 1262 |
$$$internal$$resolve(promise, val); |
| 1263 |
}; |
| 1264 |
|
| 1265 |
if (promiseInput) { |
| 1266 |
return $$rsvp$node$$handlePromiseInput(promise, args, nodeFunc, self); |
| 1267 |
} else { |
| 1268 |
return $$rsvp$node$$handleValueInput(promise, args, nodeFunc, self); |
| 1269 |
} |
| 1270 |
}; |
| 1271 |
|
| 1272 |
fn.__proto__ = nodeFunc; |
| 1273 |
|
| 1274 |
return fn; |
| 1275 |
}; |
| 1276 |
|
| 1277 |
function $$rsvp$node$$handleValueInput(promise, args, nodeFunc, self) { |
| 1278 |
var result = $$rsvp$node$$tryApply(nodeFunc, self, args); |
| 1279 |
if (result === $$rsvp$node$$ERROR) { |
| 1280 |
$$$internal$$reject(promise, result.value); |
| 1281 |
} |
| 1282 |
return promise; |
| 1283 |
} |
| 1284 |
|
| 1285 |
function $$rsvp$node$$handlePromiseInput(promise, args, nodeFunc, self){ |
| 1286 |
return $$rsvp$promise$$default.all(args).then(function(args){ |
| 1287 |
var result = $$rsvp$node$$tryApply(nodeFunc, self, args); |
| 1288 |
if (result === $$rsvp$node$$ERROR) { |
| 1289 |
$$$internal$$reject(promise, result.value); |
| 1290 |
} |
| 1291 |
return promise; |
| 1292 |
}); |
| 1293 |
} |
| 1294 |
|
| 1295 |
function $$rsvp$node$$needsPromiseInput(arg) { |
| 1296 |
if (arg && typeof arg === 'object') { |
| 1297 |
if (arg.constructor === $$rsvp$promise$$default) { |
| 1298 |
return true; |
| 1299 |
} else { |
| 1300 |
return $$rsvp$node$$getThen(arg); |
| 1301 |
} |
| 1302 |
} else { |
| 1303 |
return false; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
var $$rsvp$all$$default = function all(array, label) { |
| 1308 |
return $$rsvp$promise$$default.all(array, label); |
| 1309 |
}; |
| 1310 |
|
| 1311 |
function $$rsvp$all$settled$$AllSettled(Constructor, entries, label) { |
| 1312 |
this._superConstructor(Constructor, entries, false /* don't abort on reject */, label); |
| 1313 |
} |
| 1314 |
|
| 1315 |
$$rsvp$all$settled$$AllSettled.prototype = $$utils$$o_create($$enumerator$$default.prototype); |
| 1316 |
$$rsvp$all$settled$$AllSettled.prototype._superConstructor = $$enumerator$$default; |
| 1317 |
$$rsvp$all$settled$$AllSettled.prototype._makeResult = $$enumerator$$makeSettledResult; |
| 1318 |
|
| 1319 |
$$rsvp$all$settled$$AllSettled.prototype._validationError = function() { |
| 1320 |
return new Error('allSettled must be called with an array'); |
| 1321 |
}; |
| 1322 |
|
| 1323 |
var $$rsvp$all$settled$$default = function allSettled(entries, label) { |
| 1324 |
return new $$rsvp$all$settled$$AllSettled($$rsvp$promise$$default, entries, label).promise; |
| 1325 |
}; |
| 1326 |
|
| 1327 |
var $$rsvp$race$$default = function race(array, label) { |
| 1328 |
return $$rsvp$promise$$default.race(array, label); |
| 1329 |
}; |
| 1330 |
|
| 1331 |
function $$promise$hash$$PromiseHash(Constructor, object, label) { |
| 1332 |
this._superConstructor(Constructor, object, true, label); |
| 1333 |
} |
| 1334 |
|
| 1335 |
var $$promise$hash$$default = $$promise$hash$$PromiseHash; |
| 1336 |
$$promise$hash$$PromiseHash.prototype = $$utils$$o_create($$enumerator$$default.prototype); |
| 1337 |
$$promise$hash$$PromiseHash.prototype._superConstructor = $$enumerator$$default; |
| 1338 |
|
| 1339 |
$$promise$hash$$PromiseHash.prototype._init = function() { |
| 1340 |
this._result = {}; |
| 1341 |
}; |
| 1342 |
|
| 1343 |
$$promise$hash$$PromiseHash.prototype._validateInput = function(input) { |
| 1344 |
return input && typeof input === 'object'; |
| 1345 |
}; |
| 1346 |
|
| 1347 |
$$promise$hash$$PromiseHash.prototype._validationError = function() { |
| 1348 |
return new Error('Promise.hash must be called with an object'); |
| 1349 |
}; |
| 1350 |
|
| 1351 |
$$promise$hash$$PromiseHash.prototype._enumerate = function() { |
| 1352 |
var promise = this.promise; |
| 1353 |
var input = this._input; |
| 1354 |
var results = []; |
| 1355 |
|
| 1356 |
for (var key in input) { |
| 1357 |
if (promise._state === $$$internal$$PENDING && input.hasOwnProperty(key)) { |
| 1358 |
results.push({ |
| 1359 |
position: key, |
| 1360 |
entry: input[key] |
| 1361 |
}); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
|
| 1365 |
var length = results.length; |
| 1366 |
this._remaining = length; |
| 1367 |
var result; |
| 1368 |
|
| 1369 |
for (var i = 0; promise._state === $$$internal$$PENDING && i < length; i++) { |
| 1370 |
result = results[i]; |
| 1371 |
this._eachEntry(result.entry, result.position); |
| 1372 |
} |
| 1373 |
}; |
| 1374 |
|
| 1375 |
var $$rsvp$hash$$default = function hash(object, label) { |
| 1376 |
return new $$promise$hash$$default($$rsvp$promise$$default, object, label).promise; |
| 1377 |
}; |
| 1378 |
|
| 1379 |
function $$rsvp$hash$settled$$HashSettled(Constructor, object, label) { |
| 1380 |
this._superConstructor(Constructor, object, false, label); |
| 1381 |
} |
| 1382 |
|
| 1383 |
$$rsvp$hash$settled$$HashSettled.prototype = $$utils$$o_create($$promise$hash$$default.prototype); |
| 1384 |
$$rsvp$hash$settled$$HashSettled.prototype._superConstructor = $$enumerator$$default; |
| 1385 |
$$rsvp$hash$settled$$HashSettled.prototype._makeResult = $$enumerator$$makeSettledResult; |
| 1386 |
|
| 1387 |
$$rsvp$hash$settled$$HashSettled.prototype._validationError = function() { |
| 1388 |
return new Error('hashSettled must be called with an object'); |
| 1389 |
}; |
| 1390 |
|
| 1391 |
var $$rsvp$hash$settled$$default = function hashSettled(object, label) { |
| 1392 |
return new $$rsvp$hash$settled$$HashSettled($$rsvp$promise$$default, object, label).promise; |
| 1393 |
}; |
| 1394 |
|
| 1395 |
var $$rsvp$rethrow$$default = function rethrow(reason) { |
| 1396 |
setTimeout(function() { |
| 1397 |
throw reason; |
| 1398 |
}); |
| 1399 |
throw reason; |
| 1400 |
}; |
| 1401 |
|
| 1402 |
var $$rsvp$defer$$default = function defer(label) { |
| 1403 |
var deferred = { }; |
| 1404 |
|
| 1405 |
deferred.promise = new $$rsvp$promise$$default(function(resolve, reject) { |
| 1406 |
deferred.resolve = resolve; |
| 1407 |
deferred.reject = reject; |
| 1408 |
}, label); |
| 1409 |
|
| 1410 |
return deferred; |
| 1411 |
}; |
| 1412 |
|
| 1413 |
var $$rsvp$map$$default = function map(promises, mapFn, label) { |
| 1414 |
return $$rsvp$promise$$default.all(promises, label).then(function(values) { |
| 1415 |
if (!$$utils$$isFunction(mapFn)) { |
| 1416 |
throw new TypeError("You must pass a function as map's second argument."); |
| 1417 |
} |
| 1418 |
|
| 1419 |
var length = values.length; |
| 1420 |
var results = new Array(length); |
| 1421 |
|
| 1422 |
for (var i = 0; i < length; i++) { |
| 1423 |
results[i] = mapFn(values[i]); |
| 1424 |
} |
| 1425 |
|
| 1426 |
return $$rsvp$promise$$default.all(results, label); |
| 1427 |
}); |
| 1428 |
}; |
| 1429 |
|
| 1430 |
var $$rsvp$resolve$$default = function resolve(value, label) { |
| 1431 |
return $$rsvp$promise$$default.resolve(value, label); |
| 1432 |
}; |
| 1433 |
|
| 1434 |
var $$rsvp$reject$$default = function reject(reason, label) { |
| 1435 |
return $$rsvp$promise$$default.reject(reason, label); |
| 1436 |
}; |
| 1437 |
|
| 1438 |
var $$rsvp$filter$$default = function filter(promises, filterFn, label) { |
| 1439 |
return $$rsvp$promise$$default.all(promises, label).then(function(values) { |
| 1440 |
if (!$$utils$$isFunction(filterFn)) { |
| 1441 |
throw new TypeError("You must pass a function as filter's second argument."); |
| 1442 |
} |
| 1443 |
|
| 1444 |
var length = values.length; |
| 1445 |
var filtered = new Array(length); |
| 1446 |
|
| 1447 |
for (var i = 0; i < length; i++) { |
| 1448 |
filtered[i] = filterFn(values[i]); |
| 1449 |
} |
| 1450 |
|
| 1451 |
return $$rsvp$promise$$default.all(filtered, label).then(function(filtered) { |
| 1452 |
var results = new Array(length); |
| 1453 |
var newLength = 0; |
| 1454 |
|
| 1455 |
for (var i = 0; i < length; i++) { |
| 1456 |
if (filtered[i]) { |
| 1457 |
results[newLength] = values[i]; |
| 1458 |
newLength++; |
| 1459 |
} |
| 1460 |
} |
| 1461 |
|
| 1462 |
results.length = newLength; |
| 1463 |
|
| 1464 |
return results; |
| 1465 |
}); |
| 1466 |
}); |
| 1467 |
}; |
| 1468 |
|
| 1469 |
var $$rsvp$asap$$len = 0; |
| 1470 |
|
| 1471 |
var $$rsvp$asap$$default = function asap(callback, arg) { |
| 1472 |
$$rsvp$asap$$queue[$$rsvp$asap$$len] = callback; |
| 1473 |
$$rsvp$asap$$queue[$$rsvp$asap$$len + 1] = arg; |
| 1474 |
$$rsvp$asap$$len += 2; |
| 1475 |
if ($$rsvp$asap$$len === 2) { |
| 1476 |
// If len is 1, that means that we need to schedule an async flush. |
| 1477 |
// If additional callbacks are queued before the queue is flushed, they |
| 1478 |
// will be processed by this flush that we are scheduling. |
| 1479 |
$$rsvp$asap$$scheduleFlush(); |
| 1480 |
} |
| 1481 |
}; |
| 1482 |
|
| 1483 |
var $$rsvp$asap$$browserGlobal = (typeof window !== 'undefined') ? window : {}; |
| 1484 |
var $$rsvp$asap$$BrowserMutationObserver = $$rsvp$asap$$browserGlobal.MutationObserver || $$rsvp$asap$$browserGlobal.WebKitMutationObserver; |
| 1485 |
|
| 1486 |
// test for web worker but not in IE10 |
| 1487 |
var $$rsvp$asap$$isWorker = typeof Uint8ClampedArray !== 'undefined' && |
| 1488 |
typeof importScripts !== 'undefined' && |
| 1489 |
typeof MessageChannel !== 'undefined'; |
| 1490 |
|
| 1491 |
// node |
| 1492 |
function $$rsvp$asap$$useNextTick() { |
| 1493 |
return function() { |
| 1494 |
process.nextTick($$rsvp$asap$$flush); |
| 1495 |
}; |
| 1496 |
} |
| 1497 |
|
| 1498 |
function $$rsvp$asap$$useMutationObserver() { |
| 1499 |
var iterations = 0; |
| 1500 |
var observer = new $$rsvp$asap$$BrowserMutationObserver($$rsvp$asap$$flush); |
| 1501 |
var node = document.createTextNode(''); |
| 1502 |
observer.observe(node, { characterData: true }); |
| 1503 |
|
| 1504 |
return function() { |
| 1505 |
node.data = (iterations = ++iterations % 2); |
| 1506 |
}; |
| 1507 |
} |
| 1508 |
|
| 1509 |
// web worker |
| 1510 |
function $$rsvp$asap$$useMessageChannel() { |
| 1511 |
var channel = new MessageChannel(); |
| 1512 |
channel.port1.onmessage = $$rsvp$asap$$flush; |
| 1513 |
return function () { |
| 1514 |
channel.port2.postMessage(0); |
| 1515 |
}; |
| 1516 |
} |
| 1517 |
|
| 1518 |
function $$rsvp$asap$$useSetTimeout() { |
| 1519 |
return function() { |
| 1520 |
setTimeout($$rsvp$asap$$flush, 1); |
| 1521 |
}; |
| 1522 |
} |
| 1523 |
|
| 1524 |
var $$rsvp$asap$$queue = new Array(1000); |
| 1525 |
|
| 1526 |
function $$rsvp$asap$$flush() { |
| 1527 |
for (var i = 0; i < $$rsvp$asap$$len; i+=2) { |
| 1528 |
var callback = $$rsvp$asap$$queue[i]; |
| 1529 |
var arg = $$rsvp$asap$$queue[i+1]; |
| 1530 |
|
| 1531 |
callback(arg); |
| 1532 |
|
| 1533 |
$$rsvp$asap$$queue[i] = undefined; |
| 1534 |
$$rsvp$asap$$queue[i+1] = undefined; |
| 1535 |
} |
| 1536 |
|
| 1537 |
$$rsvp$asap$$len = 0; |
| 1538 |
} |
| 1539 |
|
| 1540 |
var $$rsvp$asap$$scheduleFlush; |
| 1541 |
|
| 1542 |
// Decide what async method to use to triggering processing of queued callbacks: |
| 1543 |
if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') { |
| 1544 |
$$rsvp$asap$$scheduleFlush = $$rsvp$asap$$useNextTick(); |
| 1545 |
} else if ($$rsvp$asap$$BrowserMutationObserver) { |
| 1546 |
$$rsvp$asap$$scheduleFlush = $$rsvp$asap$$useMutationObserver(); |
| 1547 |
} else if ($$rsvp$asap$$isWorker) { |
| 1548 |
$$rsvp$asap$$scheduleFlush = $$rsvp$asap$$useMessageChannel(); |
| 1549 |
} else { |
| 1550 |
$$rsvp$asap$$scheduleFlush = $$rsvp$asap$$useSetTimeout(); |
| 1551 |
} |
| 1552 |
|
| 1553 |
// default async is asap; |
| 1554 |
$$rsvp$config$$config.async = $$rsvp$asap$$default; |
| 1555 |
|
| 1556 |
var $$rsvp$$cast = $$rsvp$resolve$$default; |
| 1557 |
|
| 1558 |
function $$rsvp$$async(callback, arg) { |
| 1559 |
$$rsvp$config$$config.async(callback, arg); |
| 1560 |
} |
| 1561 |
|
| 1562 |
function $$rsvp$$on() { |
| 1563 |
$$rsvp$config$$config.on.apply($$rsvp$config$$config, arguments); |
| 1564 |
} |
| 1565 |
|
| 1566 |
function $$rsvp$$off() { |
| 1567 |
$$rsvp$config$$config.off.apply($$rsvp$config$$config, arguments); |
| 1568 |
} |
| 1569 |
|
| 1570 |
// Set up instrumentation through `window.__PROMISE_INTRUMENTATION__` |
| 1571 |
if (typeof window !== 'undefined' && typeof window['__PROMISE_INSTRUMENTATION__'] === 'object') { |
| 1572 |
var $$rsvp$$callbacks = window['__PROMISE_INSTRUMENTATION__']; |
| 1573 |
$$rsvp$config$$configure('instrument', true); |
| 1574 |
for (var $$rsvp$$eventName in $$rsvp$$callbacks) { |
| 1575 |
if ($$rsvp$$callbacks.hasOwnProperty($$rsvp$$eventName)) { |
| 1576 |
$$rsvp$$on($$rsvp$$eventName, $$rsvp$$callbacks[$$rsvp$$eventName]); |
| 1577 |
} |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
var rsvp$umd$$RSVP = { |
| 1582 |
'race': $$rsvp$race$$default, |
| 1583 |
'Promise': $$rsvp$promise$$default, |
| 1584 |
'allSettled': $$rsvp$all$settled$$default, |
| 1585 |
'hash': $$rsvp$hash$$default, |
| 1586 |
'hashSettled': $$rsvp$hash$settled$$default, |
| 1587 |
'denodeify': $$rsvp$node$$default, |
| 1588 |
'on': $$rsvp$$on, |
| 1589 |
'off': $$rsvp$$off, |
| 1590 |
'map': $$rsvp$map$$default, |
| 1591 |
'filter': $$rsvp$filter$$default, |
| 1592 |
'resolve': $$rsvp$resolve$$default, |
| 1593 |
'reject': $$rsvp$reject$$default, |
| 1594 |
'all': $$rsvp$all$$default, |
| 1595 |
'rethrow': $$rsvp$rethrow$$default, |
| 1596 |
'defer': $$rsvp$defer$$default, |
| 1597 |
'EventTarget': $$rsvp$events$$default, |
| 1598 |
'configure': $$rsvp$config$$configure, |
| 1599 |
'async': $$rsvp$$async |
| 1600 |
}; |
| 1601 |
|
| 1602 |
/* global define:true module:true window: true */ |
| 1603 |
if (typeof define === 'function' && define.amd) { |
| 1604 |
define(function() { return rsvp$umd$$RSVP; }); |
| 1605 |
} else if (typeof module !== 'undefined' && module.exports) { |
| 1606 |
module.exports = rsvp$umd$$RSVP; |
| 1607 |
} else if (typeof this !== 'undefined') { |
| 1608 |
this['RSVP'] = rsvp$umd$$RSVP; |
| 1609 |
} |
| 1610 |
}).call(this); |