Skip to content

Commit

Permalink
Reapply "$mol_wire_fiber: fixed promise destructor propagation"
Browse files Browse the repository at this point in the history
This reverts commit 850fb75.
  • Loading branch information
jin committed Dec 15, 2024
1 parent a396caa commit a4ef62f
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions wire/fiber/fiber.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace $ {

const handled = new WeakSet< Promise< unknown > >()
const wrappers = new WeakMap< Promise< unknown >, Promise< any > >()

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

if( $mol_promise_like( result ) && !handled.has( result ) ) {

const put = ( res: Result )=> {
if( this.cache === result ) this.put( res )
return res
}
result = result.then( put, put )

if( $mol_promise_like( result ) ) {

// if( wrappers.has( result ) ) {
// result = wrappers.get( result )!
// } else {

const put = ( res: Result )=> {
if( this.cache === result ) this.put( res )
return res
}

wrappers.set( result, result = Object.assign(
result.then( put, put ),
{ destructor: ( result as any ).destructor || (()=> {}) }
) )
wrappers.set( result, result )

const error = new Error( `Promise in ${ this }` )
Object.defineProperty( result, 'stack', { get: ()=> error.stack } )

// }
}

} catch( error: any ) {
Expand All @@ -197,28 +210,28 @@ namespace $ {
result = new Error( String( error ), { cause: error } )
}

if( $mol_promise_like( result ) && !handled.has( result ) ) {
if( $mol_promise_like( result ) ) {

result = result.finally( ()=> {
if( this.cache === result ) this.absorb()
} )
// if( wrappers.has( result ) ) {
// result = wrappers.get( result )!
// } else {

wrappers.set( result, result = Object.assign(
result.finally( ()=> {
if( this.cache === result ) this.absorb()
} ),
{ destructor: ( result as any ).destructor || (()=> {}) }
) )

const error = new Error( `Promise in ${ this }` )
Object.defineProperty( result, 'stack', { get: ()=> error.stack } )

// }

}

}

if( $mol_promise_like( result ) && !handled.has( result ) ) {

result = Object.assign( result, {
destructor: (result as any)['destructor'] ?? (()=> {})
} )
handled.add( result )

const error = new Error( `Promise in ${ this }` )
Object.defineProperty( result, 'stack', { get: ()=> error.stack } )

}

if( ! $mol_promise_like( result ) ) {
this.track_cut()
}
Expand Down

0 comments on commit a4ef62f

Please sign in to comment.