forked from intel/appframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathayepromise.js
176 lines (149 loc) · 4.87 KB
/
ayepromise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* ayepromise.js
* @license BSD - https://github.com/cburgmer/ayepromise/commit/299eb65b5ce227873b2f1724c8f5b2bfa723680a
* https://github.com/cburgmer/ayepromise
*/
// UMD header
(function (root, factory) {
//remove AMD code
root.ayepromise = factory();
}(this, function () {
'use strict';
var ayepromise = {};
/* Wrap an arbitrary number of functions and allow only one of them to be
executed and only once */
var once = function () {
var wasCalled = false;
return function wrapper(wrappedFunction) {
return function () {
if (wasCalled) {
return;
}
wasCalled = true;
wrappedFunction.apply(null, arguments);
};
};
};
var getThenableIfExists = function (obj) {
// Make sure we only access the accessor once as required by the spec
var then = obj && obj.then;
if (obj !== null &&
typeof obj === "object" &&
typeof then === "function") {
return function() { return then.apply(obj, arguments); };
}
};
var aThenHandler = function (onFulfilled, onRejected) {
var defer = ayepromise.defer();
var doHandlerCall = function (func, value) {
setTimeout(function () {
var returnValue;
try {
returnValue = func(value);
} catch (e) {
defer.reject(e);
return;
}
if (returnValue === defer.promise) {
defer.reject(new TypeError('Cannot resolve promise with itself'));
} else {
defer.resolve(returnValue);
}
}, 1);
};
return {
promise: defer.promise,
callFulfilled: function (value) {
if (onFulfilled && onFulfilled.call) {
doHandlerCall(onFulfilled, value);
} else {
defer.resolve(value);
}
},
callRejected: function (value) {
if (onRejected && onRejected.call) {
doHandlerCall(onRejected, value);
} else {
defer.reject(value);
}
}
};
};
// States
var PENDING = 0,
FULFILLED = 1,
REJECTED = 2;
ayepromise.defer = function () {
var state = PENDING,
outcome,
thenHandlers = [];
var doFulfill = function (value) {
state = FULFILLED;
outcome = value;
thenHandlers.forEach(function (then) {
then.callFulfilled(outcome);
});
};
var doReject = function (error) {
state = REJECTED;
outcome = error;
thenHandlers.forEach(function (then) {
then.callRejected(outcome);
});
};
var executeThenHandlerDirectlyIfStateNotPendingAnymore = function (then) {
if (state === FULFILLED) {
then.callFulfilled(outcome);
} else if (state === REJECTED) {
then.callRejected(outcome);
}
};
var registerThenHandler = function (onFulfilled, onRejected) {
var thenHandler = aThenHandler(onFulfilled, onRejected);
thenHandlers.push(thenHandler);
executeThenHandlerDirectlyIfStateNotPendingAnymore(thenHandler);
return thenHandler.promise;
};
var safelyResolveThenable = function (thenable) {
// Either fulfill, reject or reject with error
var onceWrapper = once();
try {
thenable(
onceWrapper(transparentlyResolveThenablesAndFulfill),
onceWrapper(doReject)
);
} catch (e) {
onceWrapper(doReject)(e);
}
};
var transparentlyResolveThenablesAndFulfill = function (value) {
var thenable;
try {
thenable = getThenableIfExists(value);
} catch (e) {
doReject(e);
return;
}
if (thenable) {
safelyResolveThenable(thenable);
} else {
doFulfill(value);
}
};
var onceWrapper = once();
return {
resolve: onceWrapper(transparentlyResolveThenablesAndFulfill),
reject: onceWrapper(doReject),
promise: {
then: registerThenHandler,
fail: function (onRejected) {
return registerThenHandler(null, onRejected);
}
}
};
};
return ayepromise;
}));
(function($){
$.Deferred=ayepromise.defer;
})(af);