Skip to content

Commit a4ef62f

Browse files
author
jin
committed
Reapply "$mol_wire_fiber: fixed promise destructor propagation"
This reverts commit 850fb75.
1 parent a396caa commit a4ef62f

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

wire/fiber/fiber.ts

+38-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace $ {
22

3-
const handled = new WeakSet< Promise< unknown > >()
3+
const wrappers = new WeakMap< Promise< unknown >, Promise< any > >()
44

55
/**
66
* Suspendable task with support both sync/async api.
@@ -179,14 +179,27 @@ namespace $ {
179179
default: result = (this.task as any).call( this.host!, ... this.args ); break
180180
}
181181

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+
// }
190203
}
191204

192205
} catch( error: any ) {
@@ -197,28 +210,28 @@ namespace $ {
197210
result = new Error( String( error ), { cause: error } )
198211
}
199212

200-
if( $mol_promise_like( result ) && !handled.has( result ) ) {
213+
if( $mol_promise_like( result ) ) {
201214

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+
// }
205230

206231
}
207232

208233
}
209234

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-
222235
if( ! $mol_promise_like( result ) ) {
223236
this.track_cut()
224237
}

0 commit comments

Comments
 (0)