forked from Alusus/Promises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromises.alusus
356 lines (324 loc) · 15.1 KB
/
Promises.alusus
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import "Srl/errors";
import "closure";
module Promises {
use Srl;
def unresolvedPromises: SrdRef[Promise[Int]];
def unresolvedPromisesLocker: ptr[func](func {});
def unresolvedPromisesUnlocker: ptr[func](func {});
func addUnresolvedPromise[ResultType: type](p: SrdRef[Promise[ResultType]]) {
unresolvedPromisesLocker();
p.prevUnresolvedPromise.release();
p.nextUnresolvedPromise = unresolvedPromises;
if not unresolvedPromises.isNull() {
unresolvedPromises.prevUnresolvedPromise = castSrdRef[p, Promise[Int]];
}
unresolvedPromises = castSrdRef[p, Promise[Int]];
unresolvedPromisesUnlocker();
}
func removeUnresolvedPromise[ResultType: type](p: SrdRef[Promise[ResultType]]) {
unresolvedPromisesLocker();
if not p.nextUnresolvedPromise.isNull() p.nextUnresolvedPromise.prevUnresolvedPromise = p.prevUnresolvedPromise;
if not p.prevUnresolvedPromise.isNull() p.prevUnresolvedPromise.nextUnresolvedPromise = p.nextUnresolvedPromise
else unresolvedPromises = p.nextUnresolvedPromise;
p.prevUnresolvedPromise.release();
p.nextUnresolvedPromise.release();
unresolvedPromisesUnlocker();
}
macro castClosure [c, T] T().{ this.data = c.data; this.fp = c.fp~cast[T.fp~type]; };
class Promise [ResultType: type] {
def wkThis: WkRef[Promise[ResultType]];
def status: Int = Status.NEW;
def resolver: SrdRef[Resolver[ResultType]];
def child: ref[ChainNode[ResultType]](nullRef[ChainNode[ResultType]]);
def prevUnresolvedPromise: SrdRef[Promise[Int]];
def nextUnresolvedPromise: SrdRef[Promise[Int]];
def error: SrdRef[Error];
def result: ResultType;
handler this.resolve(res: ResultType) {
if this.status != Status.NEW return;
removeUnresolvedPromise[ResultType](SrdRef[Promise[ResultType]](this.wkThis));
this.result = res;
this.status = Status.RESOLVED;
def chain: ref[ChainNode[ResultType]](this.child);
while chain~ptr != 0 {
// The child may get released during onResolved, so we'll capture its info
// before the call to avoid access to invalid memory.
def next: ref[ChainNode[ResultType]](chain.next);
chain.onResolved(res);
chain~no_deref = next;
}
}
handler this.resolve(p: SrdRef[Promise[ResultType]]) {
if this.status != Status.NEW return;
this.resolver = SrdRef[DirectResolver[ResultType]].construct()~use_in (self){
promise~no_deref = this;
chainNode.setPromise(p);
};
if p.status == Status.RESOLVED {
this.resolver.obj~cast[ref[DirectResolver[ResultType]]].chainNode.onResolved(p.result);
} else if p.status == Status.REJECTED {
this.resolver.obj~cast[ref[DirectResolver[ResultType]]].chainNode.onRejected(p.error);
}
}
handler this.reject(err: SrdRef[Error]) {
if this.status != Status.NEW return;
removeUnresolvedPromise[ResultType](SrdRef[Promise[ResultType]](this.wkThis));
this.error = err;
this.status = Status.REJECTED;
def chain: ref[ChainNode[ResultType]](this.child);
while chain~ptr != 0 {
// The child may get released during onRejected, so we'll capture its info
// before the call to avoid access to invalid memory.
def next: ref[ChainNode[ResultType]](chain.next);
chain.onRejected(err);
chain~no_deref = next;
}
}
function new (): SrdRef[Promise[ResultType]] {
return SrdRef[Promise[ResultType]]().{
construct();
this.wkThis = this;
addUnresolvedPromise[ResultType](this);
};
}
handler [ThenType: type] this.then(
cb: closure (input: ResultType, promise: ref[Promise[ThenType]])
): SrdRef[Promise[ThenType]] {
return SrdRef[Promise[ThenType]].construct()~use_in(thenPromise) {
wkThis = thenPromise;
resolver = SrdRef[ThenResolver[ThenType, ResultType]].construct()~use_in (self){
promise~no_deref = thenPromise.obj;
chainNode.setPromise(SrdRef[Promise[ResultType]](this.wkThis));
callback = cb;
};
if this.status == Status.RESOLVED {
resolver.obj~cast[ref[ThenResolver[ThenType, ResultType]]].chainNode.onResolved(this.result);
} else if this.status == Status.REJECTED {
resolver.obj~cast[ref[ThenResolver[ThenType, ResultType]]].chainNode.onRejected(this.error);
}
addUnresolvedPromise[ThenType](thenPromise);
};
}
handler [ThenType: type] this.then(
cb: closure (input: ResultType, promise: ref[ThenPromise[ThenType, ResultType]])
): SrdRef[Promise[ThenType]] {
return this.then[ThenType](castClosure[cb, closure (input: ResultType, promise: ref[Promise[ThenType]])]);
}
handler this.catch(
cb: closure (err: SrdRef[Error], promise: ref[Promise[ResultType]])
): SrdRef[Promise[ResultType]] {
return SrdRef[Promise[ResultType]].construct()~use_in(catchPromise) {
wkThis = catchPromise;
resolver = SrdRef[CatchResolver[ResultType]].construct()~use_in(self) {
promise~no_deref = catchPromise.obj;
chainNode.setPromise(SrdRef[Promise[ResultType]](this.wkThis));
callback = cb;
};
if this.status == Status.RESOLVED {
resolver.obj~cast[ref[CatchResolver[ResultType]]].chainNode.onResolved(this.result);
} else if this.status == Status.REJECTED {
resolver.obj~cast[ref[CatchResolver[ResultType]]].chainNode.onRejected(this.error);
}
addUnresolvedPromise[ResultType](catchPromise);
};
}
handler this.catch(
cb: closure (err: SrdRef[Error], promise: ref[CatchPromise[ResultType]])
): SrdRef[Promise[ResultType]] {
return this.catch(castClosure[cb, closure (err: SrdRef[Error], promise: ref[Promise[ResultType]])]);
}
handler this.ignoreResult(): SrdRef[Promise[Int]] {
return this.then[Int](
closure (input: ResultType, p: ref[Promise[Int]]) {
p.resolve(Int(0));
}
);
}
function all (inputs: Array[SrdRef[Promise[ResultType]]]): SrdRef[Promise[Array[ResultType]]] {
return SrdRef[Promise[Array[ResultType]]].construct()~use_in(allPromise) {
wkThis = allPromise;
resolver = SrdRef[AllResolver[ResultType]].construct()~use_in(__this) {
promise~no_deref = allPromise.obj;
def allResolved: Bool = true;
def i: Int;
for i = 0, i < inputs.getLength(), ++i {
chainNodes.add(SrdRef[AllChainNode[ResultType]].construct()~use_in(self){
allResolver~no_deref = __this.obj;
setPromise(inputs(i));
});
if chainNodes(i).depPromise.status == Status.RESOLVED {
allPromise.result.add(chainNodes(i).depPromise.result);
} else {
allResolved = false;
allPromise.result.add(ResultType());
if chainNodes(i).depPromise.status == Status.REJECTED and status == Status.NEW {
allPromise.reject(chainNodes(i).depPromise.error);
}
}
}
if allResolved allPromise.resolve(result);
};
addUnresolvedPromise[Array[ResultType]](allPromise);
};
}
function all (count: Int, inputs: ...SrdRef[Promise[ResultType]]): SrdRef[Promise[Array[ResultType]]] {
def ar: Array[SrdRef[Promise[ResultType]]];
while count-- > 0 ar.add(inputs~next_arg[SrdRef[Promise[ResultType]]]);
return all(ar);
}
}
class ThenPromise [ResultType: type, InputType: type] {
@injection def promise: Promise[ResultType];
handler this.retry(p: SrdRef[Promise[InputType]]) {
if this.resolver.getResolverType() != ResolverType.THEN_RESOLVER return;
this.resolver.obj~cast[ref[ThenResolver[ResultType, InputType]]].chainNode.setPromise(p);
if p.status == Status.RESOLVED {
this.resolver.obj~cast[ref[ThenResolver[ResultType, InputType]]].chainNode.onResolved(p.result);
} else if p.status == Status.REJECTED {
this.resolver.obj~cast[ref[ThenResolver[ResultType, InputType]]].chainNode.onRejected(p.error);
}
}
}
class CatchPromise [ResultType: type] {
@injection def promise: Promise[ResultType];
handler this.retry(p: SrdRef[Promise[ResultType]]) {
if this.resolver.getResolverType() != ResolverType.CATCH_RESOLVER return;
this.resolver.obj~cast[ref[CatchResolver[ResultType]]].chainNode.setPromise(p);
if p.status == Status.RESOLVED {
this.resolver.obj~cast[ref[CatchResolver[ResultType]]].chainNode.onResolved(p.result);
} else if p.status == Status.REJECTED {
this.resolver.obj~cast[ref[CatchResolver[ResultType]]].chainNode.onRejected(p.error);
}
}
}
class ChainNode [InputType: type] {
def next: ref[ChainNode[InputType]](nullRef[ChainNode[InputType]]);
def depPromise: SrdRef[Promise[InputType]];
handler this.setPromise(p: SrdRef[Promise[InputType]]) {
this.releasePromise();
this.next~no_deref = p.child;
p.child~no_deref = this;
this.depPromise = p;
}
handler this.releasePromise() {
if this.depPromise.isNull() return;
if this.depPromise.child~ptr == this~ptr {
this.depPromise.child~no_deref = this.next;
} else {
def c: ref[ChainNode[InputType]](this.depPromise.child);
while c.next~ptr != 0 {
if c.next~ptr == this~ptr {
c.next~no_deref = this.next;
break;
}
c~no_deref = c.next;
}
}
}
handler this~terminate() {
this.releasePromise();
}
handler this.onResolved(result: InputType) as_ptr;
handler this.onRejected(err: SrdRef[Error]) as_ptr;
}
def ResolverType: {
def DIRECT_RESOLVER: 1;
def THEN_RESOLVER: 2;
def CATCH_RESOLVER: 3;
def ALL_RESOLVER: 4;
};
class Resolver [ResultType: type] {
handler this.getResolverType(): Int as_ptr;
def promise: ref[Promise[ResultType]];
}
class DirectResolver [ResultType: type] {
@injection def resolver: Resolver[ResultType];
@injection def chainNode: ChainNode[ResultType];
handler (this: ChainNode[ResultType]).onResolved(result: ResultType) set_ptr {
unshiftThis[chainNode];
this.promise.resolve(result);
}
handler (this: ChainNode[ResultType]).onRejected(err: SrdRef[Error]) set_ptr {
unshiftThis[chainNode];
this.promise.reject(err);
}
handler (this:Resolver[ResultType]).getResolverType(): Int set_ptr {
return ResolverType.DIRECT_RESOLVER;
}
}
class ThenResolver [ResultType: type, InputType: type] {
@injection def resolver: Resolver[ResultType];
@injection def chainNode: ChainNode[InputType];
def callback: closure (input: InputType, promise: ref[Promise[ResultType]]);
handler (this: ChainNode[InputType]).onResolved(result: InputType) set_ptr {
unshiftThis[chainNode];
this.callback(result, this.promise);
}
handler (this: ChainNode[InputType]).onRejected(err: SrdRef[Error]) set_ptr {
unshiftThis[chainNode];
this.promise.reject(err);
}
handler (this:Resolver[ResultType]).getResolverType(): Int set_ptr {
return ResolverType.THEN_RESOLVER;
}
}
class CatchResolver [InputType: type] {
@injection def resolver: Resolver[InputType];
@injection def chainNode: ChainNode[InputType];
def callback: closure (err: SrdRef[Error], promise: ref[Promise[InputType]]);
handler (this: ChainNode[InputType]).onResolved(result: InputType) set_ptr {
unshiftThis[chainNode];
this.promise.resolve(result);
}
handler (this: ChainNode[InputType]).onRejected(err: SrdRef[Error]) set_ptr {
unshiftThis[chainNode];
this.callback(err, this.promise);
}
handler (this:Resolver[InputType]).getResolverType(): Int set_ptr {
return ResolverType.CATCH_RESOLVER;
}
}
class AllChainNode [InputType: type] {
@injection def chainNode: ChainNode[InputType];
def allResolver: ref[AllResolver[InputType]];
handler (this: ChainNode[InputType]).onResolved(result: InputType) set_ptr {
this.allResolver.onResolved(result, this);
}
handler (this: ChainNode[InputType]).onRejected(err: SrdRef[Error]) set_ptr {
this.allResolver.onRejected(err, this);
}
}
class AllResolver [InputType: type] {
@injection def resolver: Resolver[Array[InputType]];
def chainNodes: Array[SrdRef[AllChainNode[InputType]]];
handler this.onResolved(r: InputType, p: ref[AllChainNode[InputType]]) {
if this.promise.status != Status.NEW return;
def i: Int;
def allResolved: Bool = true;
for i = 0, i < this.chainNodes.getLength(), ++i {
if this.chainNodes(i).obj~ptr == p~ptr {
this.promise.result(i) = r;
}
if this.chainNodes(i).depPromise.status != Status.RESOLVED {
allResolved = false;
}
}
if allResolved this.promise.resolve(this.promise.result);
}
handler this.onRejected(err: SrdRef[Error], p: ref[AllChainNode[InputType]]) {
if this.promise.status != Status.NEW return;
this.promise.reject(err);
}
handler (this:Resolver[Array[InputType]]).getResolverType(): Int set_ptr {
return ResolverType.ALL_RESOLVER;
}
}
def Status: {
def NEW: 0;
def RESOLVED: 1;
def REJECTED: 2;
}
macro unshiftThis[member] {
this~ptr = (this~ptr~cast[ArchWord] - ptr[this_type](0)~cnt.member~ptr~cast[ArchWord])~cast[ptr[this_type]];
}
}