Skip to content

Commit

Permalink
feat: add suppressDestroy to context (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi authored Jul 28, 2024
1 parent b4b4d40 commit 96ffffb
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/runtime/src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class NodejsWaitingRequestCounter {
export class Context {
private _isStopping = false
private _canCallIntoJs = true
private _suppressDestroy = false

public envStore = new Store<Env>()
public scopeStore = new ScopeStore()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 96ffffb

Please sign in to comment.