2
2
import { ResolvablePromise } from 'https://ghuc.cc/worker-tools/resolvable-promise/index.ts' ;
3
3
import { pipe } from 'https://cdn.skypack.dev/[email protected] ' ;
4
4
5
- const id = ( _ : any ) => _ ;
5
+ const id = < T = any > ( _ : T ) => _ ;
6
6
7
7
type Awaitable < T > = T | PromiseLike < T > ;
8
8
9
- // TODO: Make own module?
10
- // TODO: Add abort signal?
11
- export class JSONParseLazyPromise < T , TTask = T > implements Promise < T > {
9
+ // FIXME: Ugh...
10
+ class Task < T > {
12
11
#task;
13
12
#promise;
13
+ #state = 'idle'
14
+
15
+ constructor ( task : ( ) => Awaitable < T > , promise = new ResolvablePromise < T > ( ) ) {
16
+ this . #task = task ;
17
+ this . #promise = promise ;
18
+ }
19
+
20
+ execute ( ) {
21
+ if ( this . #state === 'idle' ) {
22
+ this . #state = 'pending'
23
+ this . #promise. resolve ( this . #task( ) )
24
+ this . #promise. then ( ( ) => { this . #state = 'fulfilled' } , ( ) => { this . #state = 'rejected' } )
25
+ }
26
+ }
27
+ get state ( ) { return this . #state }
28
+ get promise ( ) { return this . #promise }
29
+ }
30
+
31
+ const lock = Symbol ( 'key' ) ;
32
+
33
+ // TODO: Make own module?
34
+ // TODO: Add abort signal?
35
+ export class JSONParseLazyPromise < T , TT = T > implements Promise < T > {
36
+ #task: Task < TT > ;
14
37
#mapFn;
15
38
#mappedPromise;
16
39
17
- constructor (
18
- task : ( ) => Awaitable < TTask > ,
19
- promise = new ResolvablePromise < TTask > ( ) ,
20
- mapFn ?: ( ( value : TTask , i ?: 0 ) => Awaitable < T > ) | undefined | null ,
40
+ static from < T > ( task : ( ) => Awaitable < T > ) {
41
+ return new JSONParseLazyPromise < T > ( lock , new Task ( task ) )
42
+ }
43
+
44
+ private constructor (
45
+ key : symbol ,
46
+ task : Task < TT > ,
47
+ mapFn ?: ( ( value : TT , i ?: 0 ) => Awaitable < T > ) | undefined | null ,
21
48
thisArg ?: any ,
22
49
) {
50
+ if ( key !== lock ) throw Error ( 'Illegal constructor invocation' ) ;
23
51
this . #task = task ;
24
- this . #promise = promise ;
25
52
this . #mapFn = mapFn ;
26
- this . #mappedPromise = promise . then ( mapFn && ( x => mapFn . call ( thisArg , x , 0 ) ) )
53
+ this . #mappedPromise = this . #task . promise . then ( mapFn && ( x => mapFn . call ( thisArg , x , 0 ) ) )
27
54
}
28
55
29
- #execute ( ) {
30
- this . #promise . resolve ( this . # task( ) ) ;
56
+ get state ( ) {
57
+ return this . #task. state ;
31
58
}
32
59
33
60
/**
@@ -38,7 +65,7 @@ export class JSONParseLazyPromise<T, TTask = T> implements Promise<T> {
38
65
onfulfilled ?: ( ( value : T ) => Awaitable < U > ) | undefined | null ,
39
66
onrejected ?: ( ( reason : any ) => Awaitable < V > ) | undefined | null
40
67
) : Promise < U | V > {
41
- this . #execute( ) ;
68
+ this . #task . execute ( ) ;
42
69
return this . #mappedPromise. then ( onfulfilled , onrejected )
43
70
}
44
71
@@ -49,8 +76,8 @@ export class JSONParseLazyPromise<T, TTask = T> implements Promise<T> {
49
76
map < U = T > (
50
77
mapFn ?: ( ( value : T , i ?: 0 ) => Awaitable < U > ) | undefined | null ,
51
78
thisArg ?: any
52
- ) : JSONParseLazyPromise < U , TTask > {
53
- return new JSONParseLazyPromise ( this . #task , this . #promise , pipe ( this . #mapFn ?? id , mapFn ?? id ) , thisArg ) ;
79
+ ) : JSONParseLazyPromise < U , TT > {
80
+ return new JSONParseLazyPromise ( lock , this . #task , pipe ( this . #mapFn?? id , mapFn ?? id ) , thisArg ) ;
54
81
}
55
82
56
83
catch < V = never > ( onrejected ?: ( ( reason : any ) => Awaitable < V > ) | null ) : Promise < T | V > {
0 commit comments