-
Notifications
You must be signed in to change notification settings - Fork 0
/
future.js
156 lines (142 loc) · 3.51 KB
/
future.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
'use strict';
(function(root, factory){
if (typeof define === 'function' && define.amd) {
define('future', function(){
return factory();
});
} else if (typeof exports !== 'undefined') {
module.exports = factory();
} else {
root.future = factory();
}
})(this, function(){
var future,
Future = function() {
this.status = 'pending';
};
function process(_future, val, status) {
_future.status = status;
_future.x = val;
var _queue, next,
onHandleType = status === 'fullfiled' ? 'onFullFilled' : 'onRejected',
isReject = status === 'rejected';
setTimeout(function(){
_queue = _future.queue.slice();
_future.queue = [];
while(_queue.length) {
next = _queue.shift();
if (typeof next[onHandleType] !== 'function') {
makeThenable(val, _future, next.future, isReject);
return;
}
var handler = next[onHandleType];
try {
makeThenable(handler(val), _future, next.future, isReject && next.type === 1);
} catch(e) {
reject(next.future)(e);
}
}
}, 0);
}
function resolve(_future) {
return function(data) {
process(_future, data, 'fullfiled');
};
}
function reject(_future) {
return function(reason) {
process(_future, reason, 'rejected');
};
}
var then = function(_future){
return function(onFullFilled, onRejected) {
var next = future(function() {
});
_future.queue.push({
onFullFilled: onFullFilled,
onRejected: onRejected,
future: next
});
if (_future.status === 'fullfiled' && onFullFilled) {
resolve(_future)(_future.x);
} else if (_future.status === 'rejected' && onRejected){
reject(_future)(_future.x);
}
return next;
};
};
function handle(isReject, promise, x) {
if (isReject) {
reject(promise)(x);
} else {
resolve(promise)(x);
}
}
function makeThenable(x, _future, promise, isReject) {
if (x instanceof Future) {
if (promise === x) {
throw new TypeError('Should not return the same promise');
}
x.queue.push({
onFullFilled: function(value){
return value;
},
onRejected: function(reason) {
return reason;
},
future: promise,
type: 1
});
if (x.status === 'pending') return;
if (x.status === 'rejected') {
reject(promise)(x.x);
return;
}
promise = x;
x = promise.x;
} else if ((x && typeof x === 'object') || (x && typeof x === 'function')){
var _then;
try {
_then = x.then;
x.status = 'pending';
if(typeof _then === 'function') {
var resolvePromise = function(value){
if (x.status === 'pending'){
x.status = 'fullfiled';
makeThenable(value, _future, promise);
}
};
var rejectPromise = function(value) {
if (x.status === 'pending'){
x.status = 'rejected';
reject(promise)(value);
}
};
try{
_then.call(x, resolvePromise, rejectPromise);
} catch(e) {
if (x.status === 'pending')
reject(promise)(e);
}
} else {
handle(isReject, promise, x);
}
} catch(e) {
reject(promise)(e);
}
return;
}
handle(isReject, promise, x);
}
future = function(resolver) {
var _future = new Future();
_future.queue = [];
_future.then = then(_future);
try {
resolver(resolve(_future), reject(_future), _future);
} catch(e) {
}
return _future;
};
return future;
});