@@ -6,51 +6,57 @@ const id = <T = any>(_: T) => _;
6
6
7
7
type Awaitable < T > = T | PromiseLike < T > ;
8
8
9
- // FIXME: Ugh...
9
+ export type TaskState = 'idle' | 'pending' | 'fulfilled' | 'rejected' ;
10
+
10
11
class Task < T > {
11
12
#task;
12
13
#promise;
13
- #state = 'idle'
14
+ #state: TaskState = 'idle'
14
15
15
- constructor ( task : ( ) => Awaitable < T > , promise = new ResolvablePromise < T > ( ) ) {
16
+ constructor ( task : ( ) => Awaitable < T > ) {
16
17
this . #task = task ;
17
- this . #promise = promise ;
18
+ this . #promise = new ResolvablePromise < T > ( ) ;
18
19
}
19
20
20
21
execute ( ) {
21
22
if ( this . #state === 'idle' ) {
22
23
this . #state = 'pending'
23
24
this . #promise. resolve ( this . #task( ) )
24
- this . #promise. then ( ( ) => { this . #state = 'fulfilled' } , ( ) => { this . #state = 'rejected' } )
25
+ this . #promise. then (
26
+ ( ) => { this . #state = 'fulfilled' } ,
27
+ ( ) => { this . #state = 'rejected' } ,
28
+ ) ;
25
29
}
26
30
}
27
- get state ( ) { return this . #state }
28
- get promise ( ) { return this . #promise }
31
+ get state ( ) : TaskState { return this . #state }
32
+ get promise ( ) : Promise < T > { return this . #promise }
29
33
}
30
34
31
35
const lock = Symbol ( 'key' ) ;
32
36
33
37
// TODO: Make own module?
34
38
// TODO: Add abort signal?
35
- export class JSONParseLazyPromise < T , TT = T > implements Promise < T > {
39
+ // FIXME: use executor instead of task functions?
40
+ // Remove extra type??
41
+ export class TaskPromise < T , TT = T > implements Promise < T > {
36
42
#task: Task < TT > ;
37
43
#mapFn;
38
44
#mappedPromise;
39
45
40
46
static from < T > ( task : ( ) => Awaitable < T > ) {
41
- return new JSONParseLazyPromise < T > ( lock , new Task ( task ) )
47
+ return new TaskPromise < T > ( lock , new Task ( task ) )
42
48
}
43
49
44
50
private constructor (
45
51
key : symbol ,
46
52
task : Task < TT > ,
47
- mapFn ?: ( ( value : TT , i ?: 0 ) => Awaitable < T > ) | undefined | null ,
53
+ mapFn ?: ( ( value : TT , i ?: 0 , p ?: TaskPromise < T , TT > ) => Awaitable < T > ) | undefined | null ,
48
54
thisArg ?: any ,
49
55
) {
50
- if ( key !== lock ) throw Error ( 'Illegal constructor invocation ' ) ;
56
+ if ( key !== lock ) throw Error ( 'Illegal constructor' ) ;
51
57
this . #task = task ;
52
58
this . #mapFn = mapFn ;
53
- this . #mappedPromise = this . #task. promise . then ( mapFn && ( x => mapFn . call ( thisArg , x , 0 ) ) )
59
+ this . #mappedPromise = this . #task. promise . then ( mapFn && ( x => mapFn . call ( thisArg , x , 0 , this ) ) ) ;
54
60
}
55
61
56
62
get state ( ) {
@@ -74,11 +80,11 @@ export class JSONParseLazyPromise<T, TT = T> implements Promise<T> {
74
80
* Returns another lazy promise that triggers execution via `.then`
75
81
*/
76
82
map < U = T > (
77
- mapFn ?: ( ( value : T , i ?: 0 ) => Awaitable < U > ) | undefined | null ,
83
+ mapFn ?: ( ( value : T , i ?: 0 , p ?: TaskPromise < T , TT > ) => Awaitable < U > ) | undefined | null ,
78
84
thisArg ?: any
79
- ) : JSONParseLazyPromise < U , TT > {
85
+ ) : TaskPromise < U , TT > {
80
86
// @ts -ignore: types of id function (x => x) not correctly inferred...
81
- return new JSONParseLazyPromise ( lock , this . #task, pipe ( this . #mapFn?? id , mapFn ?? id ) , thisArg ) ;
87
+ return new TaskPromise ( lock , this . #task, pipe ( this . #mapFn?? id , mapFn ?? id ) , thisArg ) ;
82
88
}
83
89
84
90
catch < V = never > ( onrejected ?: ( ( reason : any ) => Awaitable < V > ) | null ) : Promise < T | V > {
@@ -91,5 +97,5 @@ export class JSONParseLazyPromise<T, TT = T> implements Promise<T> {
91
97
return this . #mappedPromise. finally ( onfinally )
92
98
}
93
99
94
- [ Symbol . toStringTag ] = 'JSONParseLazyPromise '
100
+ [ Symbol . toStringTag ] = 'LazyPromise '
95
101
}
0 commit comments