Skip to content

Commit 96ffffb

Browse files
authored
feat: add suppressDestroy to context (#124)
1 parent b4b4d40 commit 96ffffb

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

packages/runtime/src/Context.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class NodejsWaitingRequestCounter {
110110
export class Context {
111111
private _isStopping = false
112112
private _canCallIntoJs = true
113+
private _suppressDestroy = false
113114

114115
public envStore = new Store<Env>()
115116
public scopeStore = new ScopeStore()
@@ -137,11 +138,25 @@ export class Context {
137138
if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
138139
this.refCounter = new NodejsWaitingRequestCounter()
139140
process.once('beforeExit', () => {
140-
this.destroy()
141+
if (!this._suppressDestroy) {
142+
this.destroy()
143+
}
141144
})
142145
}
143146
}
144147

148+
/**
149+
* Suppress the destroy on `beforeExit` event in Node.js.
150+
* Call this method if you want to keep the context and
151+
* all associated {@link Env | Env} alive,
152+
* this also means that cleanup hooks will not be called.
153+
* After call this method, you should call
154+
* {@link Context.destroy | `Context.prototype.destroy`} method manually.
155+
*/
156+
public suppressDestroy (): void {
157+
this._suppressDestroy = true
158+
}
159+
145160
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
146161
getRuntimeVersions () {
147162
return {
@@ -267,6 +282,10 @@ export class Context {
267282
return this._canCallIntoJs && !this._isStopping
268283
}
269284

285+
/**
286+
* Destroy the context and call cleanup hooks.
287+
* Associated {@link Env | Env} will be destroyed.
288+
*/
270289
public destroy (): void {
271290
this.setStopping(true)
272291
this.setCanCallIntoJs(false)

0 commit comments

Comments
 (0)