-
Notifications
You must be signed in to change notification settings - Fork 2
/
deferred.ts
175 lines (154 loc) · 4.51 KB
/
deferred.ts
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
// A library for creating Deferred objects. Compatible with the Promises A+
// specification. #promise #rewrite
type PendingState = { readonly type: "pending" }
type FulfilledState<T> = { readonly type: "fulfilled"; readonly value: T }
type RejectedState = { readonly type: "rejected"; readonly reason: any }
type ResolvedState<T> = FulfilledState<T> | RejectedState
type PromiseState<T> = ResolvedState<T> | PendingState
const microtask = globalThis.queueMicrotask || setTimeout
export class Deferred<T> {
private state: PromiseState<T> = { type: "pending" }
private queue: ((state: ResolvedState<T>) => void)[] = []
promise = this
private serve() {
if (this.state.type == "pending") return
while (this.queue.length) {
let item = this.queue.shift()
item?.(this.state)
}
}
resolve(x: T | PromiseLike<T>) {
if ((this as any) === x) {
this.reject(
new TypeError("The promise and value refer to the same object"),
)
} else if (x && (typeof x == "object" || typeof x == "function")) {
let called = false
try {
let then = (x as any).then
if (then && typeof then == "function") {
then.call(
x,
(val: T | PromiseLike<T>) => {
if (!called) {
this.resolve(val)
called = true
}
},
(reason: any) => {
if (!called) {
this.reject(reason)
called = true
}
},
)
} else {
this.fulfill(x as T)
}
} catch (err) {
if (!called) {
this.reject(err)
}
}
} else {
this.fulfill(x as T)
}
}
private fulfill(value: T) {
if (this.state.type != "pending") return
this.state = { type: "fulfilled", value }
microtask(() => this.serve())
}
reject(reason?: any) {
if (this.state.type != "pending") return
this.state = { type: "rejected", reason }
microtask(() => this.serve())
}
then<TResult1 = T, TResult2 = never>(
onfulfilled?:
| ((value: T) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?:
| ((reason: any) => TResult2 | PromiseLike<TResult2>)
| undefined
| null,
): Deferred<TResult1 | TResult2> {
let deferred = new Deferred<TResult1 | TResult2>()
this.queue.push((state) => {
if (state.type == "fulfilled") {
if (typeof onfulfilled == "function") {
try {
let result = onfulfilled(state.value)
deferred.resolve(result)
} catch (err) {
deferred.reject(err)
}
} else {
deferred.resolve(state.value as any)
}
} else if (state.type == "rejected") {
if (typeof onrejected == "function") {
try {
let result = onrejected(state.reason)
deferred.resolve(result)
} catch (err) {
deferred.reject(err)
}
} else {
deferred.reject(state.reason)
}
}
})
if (this.state.type != "pending") {
microtask(() => this.serve())
}
return deferred
}
catch<TResult = never>(
onrejected?:
| ((reason: any) => TResult | PromiseLike<TResult>)
| undefined
| null,
): Deferred<T | TResult> {
return this.then(null, onrejected)
}
finally(onfinally?: (() => void) | undefined | null) {
return this.then<T, never>(
(value) => {
if (typeof onfinally == "function") onfinally()
return value
},
(reason) => {
if (typeof onfinally == "function") onfinally()
throw reason
},
)
}
static resolve<T>(value: T | PromiseLike<T>): Deferred<T> {
let deferred = new Deferred<T>()
deferred.resolve(value)
return deferred
}
static reject(reason?: any): Deferred<never> {
let deferred = new Deferred<never>()
deferred.reject(reason)
return deferred
}
static wait(ms: number): Deferred<void> {
let deferred = new Deferred<void>()
setTimeout(() => deferred.resolve(), ms)
return deferred
}
wait(ms: number): Deferred<T> {
return this.finally(() => Deferred.wait(ms))
}
spread<TResult = void>(
onfulfilled: (...value: T & any[]) => TResult | PromiseLike<T>,
): Deferred<T extends any[] ? TResult : T> {
return this.then<T extends any[] ? TResult : T>((value) => {
if (Array.isArray(value)) return onfulfilled(...(value as any)) as any
else return value as any
})
}
}