-
Notifications
You must be signed in to change notification settings - Fork 0
/
lamb.js
157 lines (145 loc) · 4.09 KB
/
lamb.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
/* eslint no-continue: 0, no-use-before-define: 0 */
// The promise/A+ specification: https://promisesaplus.com/
const VALIDATE_STATE = {
PENDING: 0,
FULFILLED: 1,
REJECTED: 2,
};
const Utils = {
runAsync: (fn) => {
process.nextTick(fn);
},
isPromise: p => p && p instanceof Lamb,
isObject: value => value && typeof value === 'object',
isFunction: value => value && typeof value === 'function',
defaultFulfillCallback: value => value,
defaultRejectCallback: (reason) => { throw reason; },
isValidateState: state => (state === VALIDATE_STATE.PENDING) || (state === VALIDATE_STATE.FULFILLED) ||
(state || VALIDATE_STATE.REJECTED),
};
class Lamb {
constructor(fn) {
this.queue = [];
this.state = VALIDATE_STATE.PENDING;
this.handlers = {
onFulfilled: null,
onRejected: null,
};
if (typeof fn === 'function') {
fn((value) => {
this.resolve.call(this, value);
}, (reason) => {
this.reject(reason);
});
}
}
then(onFulfilled, onRejected) {
const queuedPromise = new Lamb();
if (Utils.isFunction(onFulfilled)) {
queuedPromise.handlers.onFulfilled = onFulfilled;
}
if (Utils.isFunction(onRejected)) {
queuedPromise.handlers.onRejected = onRejected;
}
this.queue.push(queuedPromise);
this.process();
return queuedPromise;
}
process() {
if (this.state === VALIDATE_STATE.PENDING) {
return;
}
// 2.2.4 onFulfilled or onRejected must not be called until the execution context stack contains only platform code.
Utils.runAsync(() => {
while (this.queue && this.queue.length) {
const queuedPromise = this.queue.shift();
let handler = null;
let value = null;
if (this.state === VALIDATE_STATE.FULFILLED) {
handler = queuedPromise.handlers.onFulfilled || Utils.defaultFulfillCallback;
} else if (this.state === VALIDATE_STATE.REJECTED) {
handler = queuedPromise.handlers.onRejected || Utils.defaultRejectCallback;
}
try {
value = handler(this.value);
} catch (e) {
queuedPromise.reject(e);
continue;
}
this.resolve.call(queuedPromise, value);
}
});
}
resolve(x) {
if (x === this) {
this.reject(new TypeError('The promise and its value refer to the same object.'));
} else if (Utils.isPromise(x)) {
if (x.state === VALIDATE_STATE.PENDING) {
x.then(value => this.resolve(value), reason => this.reject(reason));
} else {
this.transition(x.state, x.value);
}
} else if (Utils.isObject(x) || Utils.isFunction(x)) {
let then = null;
let isCalled = false;
try {
then = x.then;
if (Utils.isFunction(then)) {
then.call(x, (value) => {
if (!isCalled) {
this.resolve(value);
isCalled = true;
}
}, (reason) => {
if (!isCalled) {
this.reject(reason);
isCalled = true;
}
});
} else {
this.fulfill(x);
isCalled = true;
}
} catch (e) {
if (!isCalled) {
this.reject(e);
isCalled = true;
}
}
} else {
this.fulfill(x);
}
}
reject(reason) {
this.transition(VALIDATE_STATE.REJECTED, reason);
}
fulfill(value) {
this.transition(VALIDATE_STATE.FULFILLED, value);
}
transition(state, value) {
if (this.state === state || this.state !== VALIDATE_STATE.PENDING || !Utils.isValidateState(state) ||
arguments.length !== 2) {
return;
}
this.state = state;
this.value = value;
this.process();
}
}
module.exports = {
resolved: value => new Lamb(onFulfilled => onFulfilled(value)),
rejected: reason => new Lamb((onFulfilled, onRejected) => { onRejected(reason); }),
deferred: () => {
let resov = null;
let reject = null;
return {
promise: new Lamb((onFulfilled, onRejected) => {
resov = onFulfilled;
reject = onRejected;
}),
resolve: resov,
reject,
};
},
Lamb,
};