1
1
namespace $ {
2
2
3
- const handled = new WeakSet < Promise < unknown > > ( )
3
+ const wrappers = new WeakMap < Promise < unknown > , Promise < any > > ( )
4
4
5
5
/**
6
6
* Suspendable task with support both sync/async api.
@@ -179,14 +179,27 @@ namespace $ {
179
179
default : result = ( this . task as any ) . call ( this . host ! , ... this . args ) ; break
180
180
}
181
181
182
- if ( $mol_promise_like ( result ) && ! handled . has ( result ) ) {
183
-
184
- const put = ( res : Result ) => {
185
- if ( this . cache === result ) this . put ( res )
186
- return res
187
- }
188
- result = result . then ( put , put )
189
-
182
+ if ( $mol_promise_like ( result ) ) {
183
+
184
+ // if( wrappers.has( result ) ) {
185
+ // result = wrappers.get( result )!
186
+ // } else {
187
+
188
+ const put = ( res : Result ) => {
189
+ if ( this . cache === result ) this . put ( res )
190
+ return res
191
+ }
192
+
193
+ wrappers . set ( result , result = Object . assign (
194
+ result . then ( put , put ) ,
195
+ { destructor : ( result as any ) . destructor || ( ( ) => { } ) }
196
+ ) )
197
+ wrappers . set ( result , result )
198
+
199
+ const error = new Error ( `Promise in ${ this } ` )
200
+ Object . defineProperty ( result , 'stack' , { get : ( ) => error . stack } )
201
+
202
+ // }
190
203
}
191
204
192
205
} catch ( error : any ) {
@@ -197,28 +210,28 @@ namespace $ {
197
210
result = new Error ( String ( error ) , { cause : error } )
198
211
}
199
212
200
- if ( $mol_promise_like ( result ) && ! handled . has ( result ) ) {
213
+ if ( $mol_promise_like ( result ) ) {
201
214
202
- result = result . finally ( ( ) => {
203
- if ( this . cache === result ) this . absorb ( )
204
- } )
215
+ // if( wrappers.has( result ) ) {
216
+ // result = wrappers.get( result )!
217
+ // } else {
218
+
219
+ wrappers . set ( result , result = Object . assign (
220
+ result . finally ( ( ) => {
221
+ if ( this . cache === result ) this . absorb ( )
222
+ } ) ,
223
+ { destructor : ( result as any ) . destructor || ( ( ) => { } ) }
224
+ ) )
225
+
226
+ const error = new Error ( `Promise in ${ this } ` )
227
+ Object . defineProperty ( result , 'stack' , { get : ( ) => error . stack } )
228
+
229
+ // }
205
230
206
231
}
207
232
208
233
}
209
234
210
- if ( $mol_promise_like ( result ) && ! handled . has ( result ) ) {
211
-
212
- result = Object . assign ( result , {
213
- destructor : ( result as any ) [ 'destructor' ] ?? ( ( ) => { } )
214
- } )
215
- handled . add ( result )
216
-
217
- const error = new Error ( `Promise in ${ this } ` )
218
- Object . defineProperty ( result , 'stack' , { get : ( ) => error . stack } )
219
-
220
- }
221
-
222
235
if ( ! $mol_promise_like ( result ) ) {
223
236
this . track_cut ( )
224
237
}
0 commit comments