Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.2k
Description
- Version: 8.7
- Platform:
- Subsystem: async_hooks
Let's say I make a super simple AsyncResource, that lets me bind a function to always run in a particular executionId.
classBoundFunctionextendsasyncHooks.AsyncResource{constructor(f,bindTimeAsyncId){super('CONTEXT_BIND',bindTimeAsyncId);this.f=f;}run(){this.emitBefore();constret=this.f();this.emitAfter();returnret;}}I can use it with the following wrapper:
functionbind(f){constbindTimeAsyncId=asyncHooks.executionAsyncId();constboundF=newBoundHook(f,bindTimeAsyncId);return()=>boundF.run();}However, it seems I have no way to schedule cleanup. Let's say I do
constf=()=>console.log(`hello from ${asyncHooks.executionAsyncId()}`);constboundF=bind(f);I want BoundFunction#emitDestroy() to run once the GC grabs boundF. The docs seem to say that this is a normal approach to async resources, by
Note: Some resources depend on garbage collection for cleanup, so if a reference is made to the resource object passed to init it is possible that destroy will never be called, causing a memory leak in the application. If the resource does not depend on garbage collection, then this will not be an issue.
However, as far as I can see the JS Embedder API gives me no way to achieve this behaviour.