@@ -6,51 +6,57 @@ const id = <T = any>(_: T) => _;
66
77type Awaitable < T > = T | PromiseLike < T > ;
88
9- // FIXME: Ugh...
9+ export type TaskState = 'idle' | 'pending' | 'fulfilled' | 'rejected' ;
10+
1011class Task < T > {
1112 #task;
1213 #promise;
13- #state = 'idle'
14+ #state: TaskState = 'idle'
1415
15- constructor ( task : ( ) => Awaitable < T > , promise = new ResolvablePromise < T > ( ) ) {
16+ constructor ( task : ( ) => Awaitable < T > ) {
1617 this . #task = task ;
17- this . #promise = promise ;
18+ this . #promise = new ResolvablePromise < T > ( ) ;
1819 }
1920
2021 execute ( ) {
2122 if ( this . #state === 'idle' ) {
2223 this . #state = 'pending'
2324 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+ ) ;
2529 }
2630 }
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 }
2933}
3034
3135const lock = Symbol ( 'key' ) ;
3236
3337// TODO: Make own module?
3438// 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 > {
3642 #task: Task < TT > ;
3743 #mapFn;
3844 #mappedPromise;
3945
4046 static from < T > ( task : ( ) => Awaitable < T > ) {
41- return new JSONParseLazyPromise < T > ( lock , new Task ( task ) )
47+ return new TaskPromise < T > ( lock , new Task ( task ) )
4248 }
4349
4450 private constructor (
4551 key : symbol ,
4652 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 ,
4854 thisArg ?: any ,
4955 ) {
50- if ( key !== lock ) throw Error ( 'Illegal constructor invocation ' ) ;
56+ if ( key !== lock ) throw Error ( 'Illegal constructor' ) ;
5157 this . #task = task ;
5258 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 ) ) ) ;
5460 }
5561
5662 get state ( ) {
@@ -74,11 +80,11 @@ export class JSONParseLazyPromise<T, TT = T> implements Promise<T> {
7480 * Returns another lazy promise that triggers execution via `.then`
7581 */
7682 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 ,
7884 thisArg ?: any
79- ) : JSONParseLazyPromise < U , TT > {
85+ ) : TaskPromise < U , TT > {
8086 // @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 ) ;
8288 }
8389
8490 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> {
9197 return this . #mappedPromise. finally ( onfinally )
9298 }
9399
94- [ Symbol . toStringTag ] = 'JSONParseLazyPromise '
100+ [ Symbol . toStringTag ] = 'LazyPromise '
95101}
0 commit comments