-
Notifications
You must be signed in to change notification settings - Fork 0
/
FakePromise.ts
422 lines (362 loc) · 11.9 KB
/
FakePromise.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
let nextId = 0;
const SPACES = ' ';
/**
* @author Maciej Chałapuk ([email protected])
*/
export class FakePromise<T> implements Promise<T> {
/**
* @param result with which returned promise will be resolved
* @return new resolved promise
* @post .setError(), .setResult(), .resolve() and .reject() can not be called on returned promise
*/
static resolve<T = void>(result ?: Promise<T> | T) : FakePromise<T> {
const promise = new FakePromise<T>();
promise.resolve(result);
return promise;
}
/**
* @param error error with which returned promise will be rejected
* @return new rejected promise
* @post .setError(), .setResult(), .resolve() and .reject() can not be called on returned promise
*/
static reject<T = void>(error : any) : FakePromise<T> {
const promise = new FakePromise<T>();
promise.reject(error);
return promise;
}
private onfulfilled ?: ((value: T) => any) | null;
private onrejected ?: ((reason: any) => any) | null;
private nextPromise ?: FakePromise<any>;
private result : T | Promise<T>;
private error : any;
private id = nextId++;
private isChained = false;
private resultPromised = false;
private resolveChain = false;
private resultSet = false;
private errorSet = false;
private specified = false;
private resolved = false;
private rejected = false;
private _promiseTrace : string;
private _resultTrace : string;
private _errorTrace : string;
private _specifyTrace : string;
private _resolveTrace : string;
private _rejectTrace : string;
// intended for printing with console.log
get promiseTrace() : string { return this._promiseTrace; }
get resultTrace() : string { return this._resultTrace; }
get errorTrace() : string { return this._errorTrace; }
get specifyTrace() : string { return this._specifyTrace; }
get resolveTrace() : string { return this._resolveTrace; }
get rejectTrace() : string { return this._rejectTrace; }
readonly [Symbol.toStringTag]: "promise";
then<TResult1 = T, TResult2 = never>(
onfulfilled ?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onrejected ?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
this.check(!this.specified, 'promise already specified', this._specifyTrace);
this.onfulfilled = onfulfilled;
this.onrejected = onrejected;
this.specified = true;
this._specifyTrace = this.trace('specification');
return this.maybeFinishResolving();
}
catch<TResult = never>(
onrejected ?: ((reason: any) => TResult | PromiseLike<TResult>) | null
): Promise<T | TResult> {
return this.then<T, TResult>(undefined, onrejected);
}
finally(onfinally ?: (() => void) | null): Promise<T> {
const callback = onfinally || noop;
// Declaration in TypeScript library file is wrong. This method should
// return Promise<void>. Casting to any in order to satisfy the interface.
return this.then(callback, callback) as any;
}
/**
* @param result with which promise will be resolved
* @pre promise is not rejected or resolved
* @post promise is resolved
*/
resolve(result ?: T | Promise<T>) : void {
this.markResolveChain();
this.resolveOne(result);
}
/**
* @param error error with which promise will be rejected
* @pre promise is not rejected or resolved
* @pre given error is not undefined nor null or .setError(error) was called before
* @post promise is rejected
*/
reject(error ?: any) : void {
this.markResolveChain();
this.rejectOne(error);
}
/**
* @param result with which promise will be resolved
* @pre promise is not rejected or resolved
* @pre .setResult() was not called
* @post .setResult() can not be called
* @post .resolve() and .resolveOne() can not be called with result argument
*/
setResult(result : T | Promise<T>) : void {
this.check(
!this.isChained,
"result musn't be programmatically set in a chained promise",
);
this._setResult(result);
}
/**
* @param error error with which promise will be rejected
* @pre given error is not null nor undefined
* @pre .setError(error) was not called before
* @pre promise is not already resolved (or rejected)
* @post .reject() and .rejectOne() can be called without argument
* @post .setError(), .setResult(), .resolve() and .resolveOne() can not be called
*/
setError(error : any) : void {
this.check(
!this.isChained,
"error musn't be programmatically set in a chained promise",
);
this._setError(error);
}
/**
* @param result with which promise will be resolved
* @return next promise in the chain (not resolved nor rejected)
* @pre promise is not rejected or resolved
* @post promise is resolved
*/
resolveOne<TResult = never>(result ?: T | Promise<T>) : FakePromise<TResult> {
this.check(
!this.errorSet,
'trying to resolve a promise containing error',
this._errorTrace,
);
if (this.isChained) {
this.check(
result === undefined,
"result musn't be programmatically set in a chained promise",
);
} else if (result !== undefined || !this.resultSet) {
this._setResult(result as T);
}
this.markResolved();
return this.maybeFinishResolving();
}
/**
* @param error error with which promise will be rejected
* @return next promise in the chain (not resolved nor rejected)
* @pre promise is not rejected or resolved
* @pre given error is not undefined nor null or .setError(error) was called before
* @post promise is rejected
*/
rejectOne<TResult = never>(error ?: any) : FakePromise<TResult> {
this.check(
!this.resultSet,
'trying to reject a promise containing result',
this._resultTrace,
);
if (this.isChained) {
this.check(
error === undefined,
"error musn't be programmatically set in a chained promise",
);
} else if (error !== undefined || !this.errorSet) {
this._setError(error);
}
this.markRejected();
return this.maybeFinishResolving();
}
toJSON() : any {
const { id, resultPromised, resolveChain, resultSet, errorSet, specified, resolved, rejected } = this;
return { id, resultPromised, resolveChain, resultSet, errorSet, specified, resolved, rejected } as any;
}
toString(indent ?: number) : string {
const flags = this.toJSON();
const keys = Object.keys(flags)
.filter(key => key !== 'id')
.filter(key => flags[key])
;
if (!indent) {
const stringifiedFlags = keys.map(key => `${key}: ${flags[key]}`);
return `FakePromise#${this.id}{${stringifiedFlags.join(',')}}`;
}
const prefix = SPACES.substring(0, indent);
const stringifiedFlags = keys.map(key => `${prefix} ${key}: ${flags[key]},\n`);
return `${prefix}FakePromise#${this.id} {\n${stringifiedFlags.join('')}${prefix}}`;
}
private _setResult(result : T | Promise<T>) : void {
this.check(
!this.errorSet,
'trying to set result on a promise with error already set',
this._errorTrace,
);
this.check(
!this.resultSet,
'result already set',
this._resultTrace,
);
this.check(
!this.resultPromised,
'result already set (waiting for promise)',
this._promiseTrace,
);
if (isPromise(result)) {
this.resultPromised = true;
this._promiseTrace = this.trace('setting promise as a result');
result.then(
result => {
this.resultPromised = false;
this._setResult(result);
},
error => {
this.resultPromised = false;
this._setError(error);
},
);
return;
}
this.resultSet = true;
this.result = result;
this._resultTrace = this.trace('setting result');
this.maybeFinishResolving();
}
private _setError(error : any) : void {
this.check(
!this.resultSet,
'trying to set error on a promise with result already set',
this._resultTrace,
);
this.check(
!this.errorSet,
'error already set',
this._errorTrace,
);
this.check(
!this.resultPromised,
'result already set (waiting for promise)',
this._promiseTrace,
);
this.check(
hasValue(error),
'error must not be undefined nor null',
);
this.errorSet = true;
this.error = error;
this._errorTrace = this.trace('setting error');
this.maybeFinishResolving();
}
private markResolveChain() {
this.resolveChain = true;
}
private markResolved() {
this.check(!this.resolved, 'promise already resolved', this._resolveTrace);
this.check(!this.rejected, 'promise already rejected', this._rejectTrace);
this.resolved = true;
this._resolveTrace = this.trace('resolve');
}
private markRejected() {
this.check(!this.resolved, 'promise already resolved', this._resolveTrace);
this.check(!this.rejected, 'promise already rejected', this._rejectTrace);
this.rejected = true;
this._rejectTrace = this.trace('reject');
}
private maybeFinishResolving() {
if (
!this.specified
|| !(this.resolved || this.rejected)
|| this.resultPromised
) {
return this.getNextPromise();
}
if (this.errorSet) {
return this.doReject();
}
if (this.resultSet) {
return this.doResolve();
}
return this.getNextPromise();
}
private doResolve() {
if (!hasValue(this.onfulfilled)) {
// just forward
return this.setNextResult(this.result);
}
const callback = this.onfulfilled as (arg : T) => any;
return this.executeAndSetNextResult(callback, this.result);
}
private doReject() {
if (!hasValue(this.onrejected)) {
// just forward
return this.setNextError(this.error);
}
const callback = this.onrejected as (arg : any) => any;
return this.executeAndSetNextResult(callback, this.error);
}
private executeAndSetNextResult(callback : (arg : any) => any, arg : any) {
try {
return this.setNextResult(callback(arg as any));
} catch (e) {
if (e.name === 'FakePromiseError') {
throw e;
}
return this.setNextError(e);
}
}
private setNextResult(result : any) {
const next = this.getNextPromise();
next._setResult(result);
if (this.resolveChain) {
next.resolve();
}
return next;
}
private setNextError(error : any) {
const next = this.getNextPromise();
next._setError(error);
if (this.resolveChain) {
next.reject();
}
return next;
}
private getNextPromise() {
if (!this.nextPromise) {
this.nextPromise = new FakePromise<any>();
this.nextPromise.isChained = true;
}
return this.nextPromise;
}
private check(condition : boolean, message : string, stacktrace ?: string) {
if (!condition) {
const formattedState = `\n CURRENT STATE:\n${this.toString(6)}`;
const error = new Error(`${message}${formattedState}${stacktrace ? stacktrace : ''}`);
error.name = 'FakePromiseError';
throw error;
}
}
private trace(name : string) {
const stateTitle = `state after ${name}`.toUpperCase();
const formattedState = ` ${stateTitle}:\n${this.toString(6)}`;
const error = new Error('error');
const stack = error.stack as string;
const traceTitle = `stacktrace of ${name}`.toUpperCase();
const firstNewline = stack.indexOf('\n');
const secondNewline = stack.indexOf('\n', firstNewline + 1);
const formattedStack = ` ${traceTitle}:${stack.substring(secondNewline)}\n EOS`
.split('\n')
.map(line => ` ${line}`)
.join('\n')
;
return `\n${formattedState}\n${formattedStack}`;
}
}
export default FakePromise;
function hasValue(arg : any | null | undefined) {
return (arg !== null && arg !== undefined);
}
function isPromise<T>(arg : T | Promise<T>): arg is Promise<T> {
return hasValue(arg) && typeof (arg as any).then === 'function';
}
function noop() {}