promise-1.0.0.hacked.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /**
  2. * This is a derivation of https://github.com/jakearchibald/es6-promise,
  3. * hacked horribly to get it through the Closure compiler.
  4. *
  5. * @suppress{undefinedVars}
  6. */
  7. (function(self) {
  8. var define, requireModule, require, requirejs;
  9. (function() {
  10. var registry = {}, seen = {};
  11. define = function(name, deps, callback) {
  12. registry[name] = { deps: deps, callback: callback };
  13. };
  14. requirejs = require = requireModule = function(name) {
  15. requirejs._eak_seen = registry;
  16. if (seen[name]) { return seen[name]; }
  17. seen[name] = {};
  18. if (!registry[name]) {
  19. throw new Error("Could not find module " + name);
  20. }
  21. var mod = registry[name],
  22. deps = mod.deps,
  23. callback = mod.callback,
  24. reified = [],
  25. exports;
  26. for (var i=0, l=deps.length; i<l; i++) {
  27. if (deps[i] === 'exports') {
  28. reified.push(exports = {});
  29. } else {
  30. reified.push(requireModule(resolve(deps[i])));
  31. }
  32. }
  33. var value = callback.apply(this, reified);
  34. return seen[name] = exports || value;
  35. function resolve(child) {
  36. if (child.charAt(0) !== '.') { return child; }
  37. var parts = child.split("/");
  38. var parentBase = name.split("/").slice(0, -1);
  39. for (var i=0, l=parts.length; i<l; i++) {
  40. var part = parts[i];
  41. if (part === '..') { parentBase.pop(); }
  42. else if (part === '.') { continue; }
  43. else { parentBase.push(part); }
  44. }
  45. return parentBase.join("/");
  46. }
  47. };
  48. })();
  49. define("promise/all",
  50. ["./utils","exports"],
  51. function(__dependency1__, __exports__) {
  52. "use strict";
  53. /* global toString */
  54. var isArray = __dependency1__.isArray;
  55. var isFunction = __dependency1__.isFunction;
  56. /**
  57. Returns a promise that is fulfilled when all the given promises have been
  58. fulfilled, or rejected if any of them become rejected. The return promise
  59. is fulfilled with an array that gives all the values in the order they were
  60. passed in the `promises` array argument.
  61. Example:
  62. ```javascript
  63. var promise1 = RSVP.resolve(1);
  64. var promise2 = RSVP.resolve(2);
  65. var promise3 = RSVP.resolve(3);
  66. var promises = [ promise1, promise2, promise3 ];
  67. RSVP.all(promises).then(function(array){
  68. // The array here would be [ 1, 2, 3 ];
  69. });
  70. ```
  71. If any of the `promises` given to `RSVP.all` are rejected, the first promise
  72. that is rejected will be given as an argument to the returned promises's
  73. rejection handler. For example:
  74. Example:
  75. ```javascript
  76. var promise1 = RSVP.resolve(1);
  77. var promise2 = RSVP.reject(new Error("2"));
  78. var promise3 = RSVP.reject(new Error("3"));
  79. var promises = [ promise1, promise2, promise3 ];
  80. RSVP.all(promises).then(function(array){
  81. // Code here never runs because there are rejected promises!
  82. }, function(error) {
  83. // error.message === "2"
  84. });
  85. ```
  86. @method all
  87. @param {Array} promises
  88. @return {Promise} promise that is fulfilled when all `promises` have been
  89. fulfilled, or rejected if any of them become rejected.
  90. */
  91. function all(promises) {
  92. /*jshint validthis:true */
  93. var Promise = this;
  94. if (!isArray(promises)) {
  95. throw new TypeError('You must pass an array to all.');
  96. }
  97. return new Promise(function(resolve, reject) {
  98. var results = [], remaining = promises.length,
  99. promise;
  100. if (remaining === 0) {
  101. resolve([]);
  102. }
  103. function resolver(index) {
  104. return function(value) {
  105. resolveAll(index, value);
  106. };
  107. }
  108. function resolveAll(index, value) {
  109. results[index] = value;
  110. if (--remaining === 0) {
  111. resolve(results);
  112. }
  113. }
  114. for (var i = 0; i < promises.length; i++) {
  115. promise = promises[i];
  116. if (promise && isFunction(promise.then)) {
  117. promise.then(resolver(i), reject);
  118. } else {
  119. resolveAll(i, promise);
  120. }
  121. }
  122. });
  123. }
  124. __exports__.all = all;
  125. });
  126. define("promise/asap",
  127. ["exports"],
  128. function(__exports__) {
  129. "use strict";
  130. var browserGlobal = (typeof window !== 'undefined') ? window : {};
  131. var local = (typeof global !== 'undefined') ? global : (this === undefined? window:this);
  132. // node
  133. function useNextTick() {
  134. return function() {
  135. process.nextTick(flush);
  136. };
  137. }
  138. function useSetTimeout() {
  139. return function() {
  140. local.setTimeout(flush, 1);
  141. };
  142. }
  143. var queue = [];
  144. function flush() {
  145. for (var i = 0; i < queue.length; i++) {
  146. var tuple = queue[i];
  147. var callback = tuple[0], arg = tuple[1];
  148. callback(arg);
  149. }
  150. queue = [];
  151. }
  152. var scheduleFlush;
  153. // Decide what async method to use to triggering processing of queued callbacks:
  154. if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
  155. scheduleFlush = useNextTick();
  156. } else {
  157. scheduleFlush = useSetTimeout();
  158. }
  159. function asap(callback, arg) {
  160. var length = queue.push([callback, arg]);
  161. if (length === 1) {
  162. // If length is 1, that means that we need to schedule an async flush.
  163. // If additional callbacks are queued before the queue is flushed, they
  164. // will be processed by this flush that we are scheduling.
  165. scheduleFlush();
  166. }
  167. }
  168. __exports__.asap = asap;
  169. });
  170. define("promise/config",
  171. ["exports"],
  172. function(__exports__) {
  173. "use strict";
  174. var config = {
  175. instrument: false
  176. };
  177. function configure(name, value) {
  178. if (arguments.length === 2) {
  179. config[name] = value;
  180. } else {
  181. return config[name];
  182. }
  183. }
  184. __exports__.config = config;
  185. __exports__.configure = configure;
  186. });
  187. define("promise/polyfill",
  188. ["./promise","./utils","exports"],
  189. function(__dependency1__, __dependency2__, __exports__) {
  190. "use strict";
  191. /*global self*/
  192. var RSVPPromise = __dependency1__.Promise;
  193. var isFunction = __dependency2__.isFunction;
  194. function polyfill() {
  195. var local;
  196. if (typeof global !== 'undefined') {
  197. local = global;
  198. } else if (typeof window !== 'undefined' && window.document) {
  199. local = window;
  200. } else {
  201. local = self;
  202. }
  203. var es6PromiseSupport =
  204. "Promise" in local &&
  205. // Some of these methods are missing from
  206. // Firefox/Chrome experimental implementations
  207. "resolve" in local.Promise &&
  208. "reject" in local.Promise &&
  209. "all" in local.Promise &&
  210. "race" in local.Promise &&
  211. // Older version of the spec had a resolver object
  212. // as the arg rather than a function
  213. (function() {
  214. var resolve;
  215. new local.Promise(function(r) { resolve = r; });
  216. return isFunction(resolve);
  217. }());
  218. if (!es6PromiseSupport) {
  219. local.Promise = RSVPPromise;
  220. }
  221. }
  222. __exports__.polyfill = polyfill;
  223. });
  224. define("promise/promise",
  225. ["./config","./utils","./all","./race","./resolve","./reject","./asap","exports"],
  226. function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __exports__) {
  227. "use strict";
  228. var config = __dependency1__.config;
  229. var configure = __dependency1__.configure;
  230. var objectOrFunction = __dependency2__.objectOrFunction;
  231. var isFunction = __dependency2__.isFunction;
  232. var now = __dependency2__.now;
  233. var all = __dependency3__.all;
  234. var race = __dependency4__.race;
  235. var staticResolve = __dependency5__.resolve;
  236. var staticReject = __dependency6__.reject;
  237. var asap = __dependency7__.asap;
  238. var counter = 0;
  239. config.async = asap; // default async is asap;
  240. function Promise(resolver) {
  241. if (!isFunction(resolver)) {
  242. throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
  243. }
  244. if (!(this instanceof Promise)) {
  245. throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
  246. }
  247. this._subscribers = [];
  248. invokeResolver(resolver, this);
  249. }
  250. function invokeResolver(resolver, promise) {
  251. function resolvePromise(value) {
  252. resolve(promise, value);
  253. }
  254. function rejectPromise(reason) {
  255. reject(promise, reason);
  256. }
  257. try {
  258. resolver(resolvePromise, rejectPromise);
  259. } catch(e) {
  260. rejectPromise(e);
  261. }
  262. }
  263. function invokeCallback(settled, promise, callback, detail) {
  264. var hasCallback = isFunction(callback),
  265. value, error, succeeded, failed;
  266. if (hasCallback) {
  267. try {
  268. value = callback(detail);
  269. succeeded = true;
  270. } catch(e) {
  271. failed = true;
  272. error = e;
  273. }
  274. } else {
  275. value = detail;
  276. succeeded = true;
  277. }
  278. if (handleThenable(promise, value)) {
  279. return;
  280. } else if (hasCallback && succeeded) {
  281. resolve(promise, value);
  282. } else if (failed) {
  283. reject(promise, error);
  284. } else if (settled === FULFILLED) {
  285. resolve(promise, value);
  286. } else if (settled === REJECTED) {
  287. reject(promise, value);
  288. }
  289. }
  290. var PENDING = void 0;
  291. var SEALED = 0;
  292. var FULFILLED = 1;
  293. var REJECTED = 2;
  294. function subscribe(parent, child, onFulfillment, onRejection) {
  295. var subscribers = parent._subscribers;
  296. var length = subscribers.length;
  297. subscribers[length] = child;
  298. subscribers[length + FULFILLED] = onFulfillment;
  299. subscribers[length + REJECTED] = onRejection;
  300. }
  301. function publish(promise, settled) {
  302. var child, callback, subscribers = promise._subscribers, detail = promise._detail;
  303. for (var i = 0; i < subscribers.length; i += 3) {
  304. child = subscribers[i];
  305. callback = subscribers[i + settled];
  306. invokeCallback(settled, child, callback, detail);
  307. }
  308. promise._subscribers = null;
  309. }
  310. Promise.prototype = {
  311. constructor: Promise,
  312. _state: undefined,
  313. _detail: undefined,
  314. _subscribers: undefined,
  315. then: function(onFulfillment, onRejection) {
  316. var promise = this;
  317. var thenPromise = new this.constructor(function() {});
  318. if (this._state) {
  319. var callbacks = arguments;
  320. config.async(function invokePromiseCallback() {
  321. invokeCallback(promise._state, thenPromise, callbacks[promise._state - 1], promise._detail);
  322. });
  323. } else {
  324. subscribe(this, thenPromise, onFulfillment, onRejection);
  325. }
  326. return thenPromise;
  327. },
  328. 'catch': function(onRejection) {
  329. return this.then(null, onRejection);
  330. }
  331. };
  332. Promise.all = all;
  333. Promise.race = race;
  334. Promise.resolve = staticResolve;
  335. Promise.reject = staticReject;
  336. function handleThenable(promise, value) {
  337. var then = null,
  338. resolved;
  339. try {
  340. if (promise === value) {
  341. throw new TypeError("A promises callback cannot return that same promise.");
  342. }
  343. if (objectOrFunction(value)) {
  344. then = value.then;
  345. if (isFunction(then)) {
  346. then.call(value, function(val) {
  347. if (resolved) { return true; }
  348. resolved = true;
  349. if (value !== val) {
  350. resolve(promise, val);
  351. } else {
  352. fulfill(promise, val);
  353. }
  354. }, function(val) {
  355. if (resolved) { return true; }
  356. resolved = true;
  357. reject(promise, val);
  358. });
  359. return true;
  360. }
  361. }
  362. } catch (error) {
  363. if (resolved) { return true; }
  364. reject(promise, error);
  365. return true;
  366. }
  367. return false;
  368. }
  369. function resolve(promise, value) {
  370. if (promise === value) {
  371. fulfill(promise, value);
  372. } else if (!handleThenable(promise, value)) {
  373. fulfill(promise, value);
  374. }
  375. }
  376. function fulfill(promise, value) {
  377. if (promise._state !== PENDING) { return; }
  378. promise._state = SEALED;
  379. promise._detail = value;
  380. config.async(publishFulfillment, promise);
  381. }
  382. function reject(promise, reason) {
  383. if (promise._state !== PENDING) { return; }
  384. promise._state = SEALED;
  385. promise._detail = reason;
  386. config.async(publishRejection, promise);
  387. }
  388. function publishFulfillment(promise) {
  389. publish(promise, promise._state = FULFILLED);
  390. }
  391. function publishRejection(promise) {
  392. publish(promise, promise._state = REJECTED);
  393. }
  394. __exports__.Promise = Promise;
  395. });
  396. define("promise/race",
  397. ["./utils","exports"],
  398. function(__dependency1__, __exports__) {
  399. "use strict";
  400. /* global toString */
  401. var isArray = __dependency1__.isArray;
  402. /**
  403. `RSVP.race` allows you to watch a series of promises and act as soon as the
  404. first promise given to the `promises` argument fulfills or rejects.
  405. Example:
  406. ```javascript
  407. var promise1 = new RSVP.Promise(function(resolve, reject){
  408. setTimeout(function(){
  409. resolve("promise 1");
  410. }, 200);
  411. });
  412. var promise2 = new RSVP.Promise(function(resolve, reject){
  413. setTimeout(function(){
  414. resolve("promise 2");
  415. }, 100);
  416. });
  417. RSVP.race([promise1, promise2]).then(function(result){
  418. // result === "promise 2" because it was resolved before promise1
  419. // was resolved.
  420. });
  421. ```
  422. `RSVP.race` is deterministic in that only the state of the first completed
  423. promise matters. For example, even if other promises given to the `promises`
  424. array argument are resolved, but the first completed promise has become
  425. rejected before the other promises became fulfilled, the returned promise
  426. will become rejected:
  427. ```javascript
  428. var promise1 = new RSVP.Promise(function(resolve, reject){
  429. setTimeout(function(){
  430. resolve("promise 1");
  431. }, 200);
  432. });
  433. var promise2 = new RSVP.Promise(function(resolve, reject){
  434. setTimeout(function(){
  435. reject(new Error("promise 2"));
  436. }, 100);
  437. });
  438. RSVP.race([promise1, promise2]).then(function(result){
  439. // Code here never runs because there are rejected promises!
  440. }, function(reason){
  441. // reason.message === "promise2" because promise 2 became rejected before
  442. // promise 1 became fulfilled
  443. });
  444. ```
  445. @method race
  446. @param {Array} promises array of promises to observe
  447. @return {Promise} a promise that becomes fulfilled with the value the first
  448. completed promises is resolved with if the first completed promise was
  449. fulfilled, or rejected with the reason that the first completed promise
  450. was rejected with.
  451. */
  452. function race(promises) {
  453. /*jshint validthis:true */
  454. var Promise = this;
  455. if (!isArray(promises)) {
  456. throw new TypeError('You must pass an array to race.');
  457. }
  458. return new Promise(function(resolve, reject) {
  459. var results = [], promise;
  460. for (var i = 0; i < promises.length; i++) {
  461. promise = promises[i];
  462. if (promise && typeof promise.then === 'function') {
  463. promise.then(resolve, reject);
  464. } else {
  465. resolve(promise);
  466. }
  467. }
  468. });
  469. }
  470. __exports__.race = race;
  471. });
  472. define("promise/reject",
  473. ["exports"],
  474. function(__exports__) {
  475. "use strict";
  476. /**
  477. `RSVP.reject` returns a promise that will become rejected with the passed
  478. `reason`. `RSVP.reject` is essentially shorthand for the following:
  479. ```javascript
  480. var promise = new RSVP.Promise(function(resolve, reject){
  481. reject(new Error('WHOOPS'));
  482. });
  483. promise.then(function(value){
  484. // Code here doesn't run because the promise is rejected!
  485. }, function(reason){
  486. // reason.message === 'WHOOPS'
  487. });
  488. ```
  489. Instead of writing the above, your code now simply becomes the following:
  490. ```javascript
  491. var promise = RSVP.reject(new Error('WHOOPS'));
  492. promise.then(function(value){
  493. // Code here doesn't run because the promise is rejected!
  494. }, function(reason){
  495. // reason.message === 'WHOOPS'
  496. });
  497. ```
  498. @method reject
  499. @param {?} reason value that the returned promise will be rejected with.
  500. @return {Promise} a promise that will become rejected with the given
  501. `reason`.
  502. */
  503. function reject(reason) {
  504. /*jshint validthis:true */
  505. var Promise = this;
  506. return new Promise(function (resolve, reject) {
  507. reject(reason);
  508. });
  509. }
  510. __exports__.reject = reject;
  511. });
  512. define("promise/resolve",
  513. ["exports"],
  514. function(__exports__) {
  515. "use strict";
  516. function resolve(value) {
  517. /*jshint validthis:true */
  518. if (value && typeof value === 'object' && value.constructor === this) {
  519. return value;
  520. }
  521. var Promise = this;
  522. return new Promise(function(resolve) {
  523. resolve(value);
  524. });
  525. }
  526. __exports__.resolve = resolve;
  527. });
  528. define("promise/utils",
  529. ["exports"],
  530. function(__exports__) {
  531. "use strict";
  532. function objectOrFunction(x) {
  533. return isFunction(x) || (typeof x === "object" && x !== null);
  534. }
  535. function isFunction(x) {
  536. return typeof x === "function";
  537. }
  538. function isArray(x) {
  539. return Object.prototype.toString.call(x) === "[object Array]";
  540. }
  541. // Date.now is not available in browsers < IE9
  542. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now#Compatibility
  543. var now = Date.now || function() { return new Date().getTime(); };
  544. __exports__.objectOrFunction = objectOrFunction;
  545. __exports__.isFunction = isFunction;
  546. __exports__.isArray = isArray;
  547. __exports__.now = now;
  548. });
  549. requireModule('promise/polyfill').polyfill();
  550. }(this));