From 96ffffb93f8f15ea8502b32b6102c453472e5bb8 Mon Sep 17 00:00:00 2001 From: Toyo Li Date: Sun, 28 Jul 2024 17:48:16 +0800 Subject: [PATCH] feat: add suppressDestroy to context (#124) --- packages/runtime/src/Context.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/runtime/src/Context.ts b/packages/runtime/src/Context.ts index dc56ae09..0c28c092 100644 --- a/packages/runtime/src/Context.ts +++ b/packages/runtime/src/Context.ts @@ -110,6 +110,7 @@ class NodejsWaitingRequestCounter { export class Context { private _isStopping = false private _canCallIntoJs = true + private _suppressDestroy = false public envStore = new Store() public scopeStore = new ScopeStore() @@ -137,11 +138,25 @@ export class Context { if (typeof process === 'object' && process !== null && typeof process.once === 'function') { this.refCounter = new NodejsWaitingRequestCounter() process.once('beforeExit', () => { - this.destroy() + if (!this._suppressDestroy) { + this.destroy() + } }) } } + /** + * Suppress the destroy on `beforeExit` event in Node.js. + * Call this method if you want to keep the context and + * all associated {@link Env | Env} alive, + * this also means that cleanup hooks will not be called. + * After call this method, you should call + * {@link Context.destroy | `Context.prototype.destroy`} method manually. + */ + public suppressDestroy (): void { + this._suppressDestroy = true + } + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type getRuntimeVersions () { return { @@ -267,6 +282,10 @@ export class Context { return this._canCallIntoJs && !this._isStopping } + /** + * Destroy the context and call cleanup hooks. + * Associated {@link Env | Env} will be destroyed. + */ public destroy (): void { this.setStopping(true) this.setCanCallIntoJs(false)