-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.js
218 lines (176 loc) · 5.84 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { isFulfilled, isRejected, isSettled, isPending, isNever, isHandled, getValue, getReason } from './inspect'
import { Future, resolve, reject, future, never, fulfill, all, race, iterablePromise, taskQueue } from './Promise'
import _delay from './delay'
import _timeout from './timeout'
import Any from './Any'
import Merge from './Merge'
import Settle from './Settle'
import { resultsArray } from './iterable'
import _runPromise from './runPromise'
import _runNode from './node'
import _runCoroutine from './coroutine.js'
import { isDebug } from './env'
import { swapContext, pushContext, enableAsyncTraces, disableAsyncTraces } from './trace'
export { enableAsyncTraces, disableAsyncTraces }
/* istanbul ignore next */
if (isDebug) {
enableAsyncTraces()
}
// -------------------------------------------------------------
// ## Core promise methods
// -------------------------------------------------------------
export {
resolve, reject, future, never, fulfill, all, race,
isFulfilled, isRejected, isSettled, isPending, isNever, isHandled,
getValue, getReason
}
// -------------------------------------------------------------
// ## Coroutine
// -------------------------------------------------------------
// coroutine :: Generator e a -> (...* -> Promise e a)
// Make a coroutine from a promise-yielding generator
export function coroutine (generator) {
return function coroutinified (...args) {
return runGenerator(generator, this, args)
}
}
function runGenerator (generator, thisArg, args) {
const iterator = generator.apply(thisArg, args)
return _runCoroutine(resolve, iterator, new Future())
}
// -------------------------------------------------------------
// ## Node-style async
// -------------------------------------------------------------
// type Nodeback e a = e -> a -> ()
// type NodeApi e a = ...* -> Nodeback e a -> ()
// fromNode :: NodeApi e a -> (...args -> Promise e a)
// Turn a Node API into a promise API
export function fromNode (f) {
return function promisified (...args) {
return runResolver(_runNode, f, this, args, new Future())
}
}
// runNode :: NodeApi e a -> ...* -> Promise e a
// Run a Node API, returning a promise for the outcome
export function runNode (f, ...args) {
return runResolver(_runNode, f, this, args, new Future())
}
// -------------------------------------------------------------
// ## Make a promise
// -------------------------------------------------------------
// type Resolve e a = a|Thenable e a -> ()
// type Reject e = e -> ()
// type Producer e a = (...* -> Resolve e a -> Reject e -> ())
// runPromise :: Producer e a -> ...* -> Promise e a
export function runPromise (f, ...args) {
return runResolver(_runPromise, f, this, args, new Future())
}
function runResolver (run, f, thisArg, args, p) {
checkFunction(f)
try {
run(f, thisArg, args, p)
} catch (e) {
p._reject(e)
}
return p
}
// -------------------------------------------------------------
// ## Time
// -------------------------------------------------------------
// delay :: number -> Promise e a -> Promise e a
export function delay (ms, x) {
/* eslint complexity:[2,4] */
const p = resolve(x)
return ms <= 0 || isRejected(p) || isNever(p) ? p
: _delay(ms, p, new Future())
}
// timeout :: number -> Promise e a -> Promise (e|TimeoutError) a
export function timeout (ms, x) {
const p = resolve(x)
return isSettled(p) ? p : _timeout(ms, p, new Future())
}
// -------------------------------------------------------------
// ## Iterables
// -------------------------------------------------------------
// any :: Iterable (Promise e a) -> Promise e a
export function any (promises) {
return iterablePromise(new Any(), promises)
}
// settle :: Iterable (Promise e a) -> Promise e [Promise e a]
export function settle (promises) {
const handler = new Settle(resolve, resultsArray(promises))
return iterablePromise(handler, promises)
}
// -------------------------------------------------------------
// ## Lifting
// -------------------------------------------------------------
// merge :: (...* -> b) -> ...Promise e a -> Promise e b
export function merge (f, ...args) {
return runMerge(f, this, args)
}
function runMerge (f, thisArg, args) {
const handler = new Merge(new MergeHandler(f, thisArg), resultsArray(args))
return iterablePromise(handler, args)
}
class MergeHandler {
constructor (f, c) {
this.context = pushContext(this.constructor, Merge.name)
this.f = f
this.c = c
this.promise = void 0
this.args = void 0
}
merge (promise, args) {
this.promise = promise
this.args = args
taskQueue.add(this)
}
run () {
const c = swapContext(this.context)
try {
this.promise._resolve(this.f.apply(this.c, this.args))
} catch (e) {
this.promise._reject(e)
}
swapContext(c)
}
}
function checkFunction (f) {
if (typeof f !== 'function') {
throw new TypeError('must provide a resolver function')
}
}
// -------------------------------------------------------------
// ## ES6 Promise polyfill
// -------------------------------------------------------------
const NOARGS = []
// type Resolve a = a -> ()
// type Reject e = e -> ()
// Promise :: (Resolve a -> Reject e) -> Promise e a
class CreedPromise extends Future {
constructor (f) {
super()
runResolver(_runPromise, f, void 0, NOARGS, this)
}
}
CreedPromise.resolve = resolve
CreedPromise.reject = reject
CreedPromise.all = all
CreedPromise.race = race
export function shim () {
/* global self */
const orig = typeof Promise === 'function' && Promise
/* istanbul ignore if */
if (typeof self !== 'undefined') {
self.Promise = CreedPromise
/* istanbul ignore else */
} else if (typeof global !== 'undefined') {
global.Promise = CreedPromise
}
return orig
}
export { CreedPromise as Promise }
/* istanbul ignore if */
if (typeof Promise !== 'function') {
shim()
}