Skip to content

Commit

Permalink
add shutdown callbacks to the application
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuspoehls committed May 4, 2024
1 parent e0f7c0d commit 413d7d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/application/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class Application extends Container implements ApplicationContract {
this.meta = {
appRoot: this.resolveFileURLToPath(basePath),
bootingCallbacks: [],
shutdownCallbacks: [],
serviceProviders: Arr.from(),

packageJson: undefined,
Expand Down Expand Up @@ -351,6 +352,27 @@ export class Application extends Container implements ApplicationContract {
})
}

/**
* Returns the registered shutdown callbacks.
*/
shutdownCallbacks (): Callback[] {
return this.meta.shutdownCallbacks
}

/**
* Register a shutdown callback that runs after all service provides ran
* their `shutdown` function.
*/
onShutdown (callback: Callback): this {
if (typeof callback !== 'function') {
throw new Error(`You must pass a function to the application’s "onShutdown" function. Received [${String(callback)}]`)
}

return tap(this, () => {
this.meta.shutdownCallbacks.push(callback)
})
}

/**
* Register the configured user-land providers.
*/
Expand Down Expand Up @@ -457,6 +479,8 @@ export class Application extends Container implements ApplicationContract {
).forEach(async provider => {
await this.shutdownProvider(provider)
})

await this.runAppCallbacks(this.shutdownCallbacks())
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/application/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export interface ApplicationMeta {
*/
bootingCallbacks: Callback[]

/**
* Shutdown callbacks run when the application stops. We’re
* using this in the framework to u
*/
shutdownCallbacks: Callback[]

/**
* All registered service providers.
*/
Expand Down

0 comments on commit 413d7d5

Please sign in to comment.