@@ -110,6 +110,7 @@ class NodejsWaitingRequestCounter {
110
110
export class Context {
111
111
private _isStopping = false
112
112
private _canCallIntoJs = true
113
+ private _suppressDestroy = false
113
114
114
115
public envStore = new Store < Env > ( )
115
116
public scopeStore = new ScopeStore ( )
@@ -137,11 +138,25 @@ export class Context {
137
138
if ( typeof process === 'object' && process !== null && typeof process . once === 'function' ) {
138
139
this . refCounter = new NodejsWaitingRequestCounter ( )
139
140
process . once ( 'beforeExit' , ( ) => {
140
- this . destroy ( )
141
+ if ( ! this . _suppressDestroy ) {
142
+ this . destroy ( )
143
+ }
141
144
} )
142
145
}
143
146
}
144
147
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
+
145
160
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
146
161
getRuntimeVersions ( ) {
147
162
return {
@@ -267,6 +282,10 @@ export class Context {
267
282
return this . _canCallIntoJs && ! this . _isStopping
268
283
}
269
284
285
+ /**
286
+ * Destroy the context and call cleanup hooks.
287
+ * Associated {@link Env | Env} will be destroyed.
288
+ */
270
289
public destroy ( ) : void {
271
290
this . setStopping ( true )
272
291
this . setCanCallIntoJs ( false )
0 commit comments