/
Promistix.js
131 lines (117 loc) · 2.92 KB
/
Promistix.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
module.exports = (function () {
function empty_list(list) {
list = list || {};
list.next = null;
list.tail = list;
return list;
}
function append_node(list, node) {
list.tail.next = node;
list.tail = node;
}
var WAIT = 0,
DONE = 1,
FAIL = 2,
last_pipe_state_id = 3,
queued_thunks = empty_list();
function run_queued() {
var thunk = queued_thunks.next;
empty_list(queued_thunks);
while (thunk) {
var state = thunk.state,
value = thunk.value,
then = thunk.thens,
thunk = thunk.next;
while (then) {
var cfn = then[state],
next_value = value,
next_state = state;
if (typeof cfn === "function") {
try {
next_value = cfn(next_value);
next_state = DONE;
} catch (error) {
next_value = error;
next_state = FAIL;
}
}
transit(then.deferred, next_state, next_value);
then = then.next;
}
}
}
function deferred() {
var def = empty_list({
state: WAIT,
value: "",
promise: { then: function (res, rej) { return then(def, res, rej); } },
resolve: function (value) { transit(def, DONE, value); },
reject: function (value) { transit(def, FAIL, value); }
});
return def;
};
function schedule(def) {
var thunk = { state: def.state, value: def.value, thens: def.next, next: null };
empty_list(def);
append_node(queued_thunks, thunk);
if (queued_thunks.next === thunk)
Promistix.schedule(run_queued);
}
function then(def, done, fail) {
var then = { 1: done, 2: fail, deferred: deferred(), next: null };
append_node(def, then);
if (def.state !== WAIT)
schedule(def);
return then.deferred.promise;
}
function pipe(def, id, state, value) {
if (def.state === id) {
def.state = WAIT;
transit(def, state, value);
}
}
function fill(def, state, value) {
def.value = value;
def.state = state;
schedule(def);
}
function transit(def, state, value) {
if (def.state !== WAIT)
return;
if (typeof value === "function" || (typeof value === "object" && value !== null)) {
try {
if (value === def.promise)
throw new TypeError("A promise cannot return itself");
var then = value.then;
if (state === DONE && typeof then === "function") {
def.promise.then = then.bind(value);
var id = def.state = last_pipe_state_id++;
try {
then.call(value,
function (next) {
pipe(def, id, DONE, next);
}, function (next) {
pipe(def, id, FAIL, next);
});
} catch (error1) {
if (def.state === id)
fill(def, FAIL, error1);
}
return;
}
} catch (error2) {
value = error2;
state = FAIL;
}
}
fill(def, state, value);
}
var Promistix = {
name: "Promistix",
pending: deferred,
schedule: setImmediate || (process && process.nextTick) || function () {
throw new Error("Promistix.schedule must be set to setImmediate in a nodejs environment, or a similar function");
}
};
return Promistix;
})();