|
1 | | -(function() { |
2 | | - |
3 | | - 'use strict'; |
4 | | - |
5 | | - var Promise = function() { |
6 | | - this.thenTargets = []; |
7 | | - this.pending = true; |
8 | | - }; |
9 | | - |
10 | | - var isPromise = function(promise) { |
11 | | - return promise && promise instanceof Promise; |
12 | | - } |
13 | | - |
14 | | - var isPseudoPromise = function(promise) { |
15 | | - return promise && typeof promise.then == 'function'; |
16 | | - } |
17 | | - |
18 | | - Promise.prototype.resolve = function(promise, value) { |
19 | | - if (promise === value) { |
20 | | - throw new TypeError('resolve: arguments cannot be the same object') |
21 | | - } |
22 | | - if (promise === value) { |
23 | | - throw new TypeError('resolve: arguments cannot be the same object') |
24 | | - } |
25 | | - if (isPromise(value) || isPseudoPromise(value)) { |
26 | | - value.then(promise.fulfil.bind(promise), promise.reject.bind(promise)) |
27 | | - } else { |
28 | | - promise.fulfil(value); |
29 | | - } |
30 | | - }; |
31 | | - |
32 | | - Promise.prototype.handleThenTargets = function() { |
33 | | - var callbackResult; |
34 | | - var callback; |
35 | | - var value; |
36 | | - var i; |
37 | | - |
38 | | - for (i=0;i<this.thenTargets.length;++i) { |
39 | | - if (this.fulfilled) { |
40 | | - callback = this.thenTargets[i].onFulfilled; |
41 | | - value = this.value; |
42 | | - } |
43 | | - if (this.rejected) { |
44 | | - callback = this.thenTargets[i].onRejected; |
45 | | - value = this.reason; |
46 | | - } |
47 | | - try { |
48 | | - if (callback && typeof callback === 'function') { |
49 | | - callbackResult = callback.apply(undefined, value); |
50 | | - } else { |
51 | | - callbackResult = this; |
52 | | - } |
53 | | - this.resolve(this.thenTargets[i], callbackResult); |
54 | | - } |
55 | | - catch(err) { |
56 | | - this.thenTargets[i].reject(err); |
57 | | - } |
58 | | - } |
59 | | - this.thenTargets = []; |
60 | | - }; |
61 | | - |
62 | | - Promise.prototype.handleThen = function() { |
63 | | - if (!this.pending) { |
64 | | - this.handleThenTargets(); |
65 | | - } |
66 | | - }; |
67 | | - |
68 | | - Promise.prototype.then = function(onFulfilled, onRejected) { |
69 | | - var thenResult = new Promise(); |
70 | | - // The execution of then is asynchronous so we need to have this info available later. |
71 | | - thenResult.onFulfilled = onFulfilled; |
72 | | - thenResult.onRejected = onRejected; |
73 | | - this.thenTargets.push(thenResult); |
74 | | - setTimeout(this.handleThen.bind(this),0); |
75 | | - return thenResult; |
76 | | - }; |
77 | | - |
78 | | - Promise.prototype.fulfil = function() { |
79 | | - var i; |
80 | | - var linkedPromise; |
81 | | - if (this.rejected) { |
82 | | - return; |
83 | | - } |
84 | | - this.fulfilled = true; |
85 | | - this.pending = false; |
86 | | - this.value = arguments; |
87 | | - |
88 | | - this.handleThenTargets(); |
89 | | - }; |
90 | | - |
91 | | - Promise.prototype.reject = function() { |
92 | | - var i; |
93 | | - var linkedPromise; |
94 | | - if (this.fulfilled) { |
95 | | - return; |
96 | | - } |
97 | | - this.reason = arguments; |
98 | | - this.rejected = true; |
99 | | - this.pending = false; |
100 | | - this.handleThenTargets(); |
101 | | - } |
102 | | - |
103 | | - this.Promise = Promise; |
104 | | - |
105 | | -}).call(this); |
106 | 1 | /** |
107 | 2 | * |
108 | 3 | * FULL TILT |
@@ -1579,9 +1474,9 @@ window.FULLTILT = FULLTILT; |
1579 | 1474 | /** |
1580 | 1475 | * JavaScript project for accessing and normalizing the accelerometer and gyroscope data on mobile devices |
1581 | 1476 | * |
1582 | | -* @author Doruk Eker <dorukeker@gmail.com> |
| 1477 | +* @author Doruk Eker <doruk@dorukeker.com> |
1583 | 1478 | * @copyright Doruk Eker <http://dorukeker.com> |
1584 | | -* @version 2.0.4 |
| 1479 | +* @version 2.0.5 |
1585 | 1480 | * @license MIT License | http://opensource.org/licenses/MIT |
1586 | 1481 | */ |
1587 | 1482 |
|
|
0 commit comments