Stone.jsDocs
Paradigm

Essentials

Hooks & lifecycle

Some work has to happen at a specific moment, not in response to a request: open a pool at startup, flush it at shutdown, observe every event as it passes. Hooks give you those moments by name, so that code lives where it belongs instead of leaking into handlers.

#Declaring a hook

Mark a method with @Hook(name). The kernel calls it at the matching moment, with the app's dependencies available. Startup and shutdown hooks fire once; per-event hooks fire inside each ephemeral container.

app/Application.tsts
import { Hook, StoneApp } from '@stone-js/core'

@StoneApp({ name: 'tasks' })
export class Application {
  @Hook('onStart')
  async warmUp ({ container }) {
    await container.resolve('db').connect()   // once, at startup
  }

  @Hook('onTerminate')
  async drain ({ container }) {
    await container.resolve('db').close()      // once, at shutdown
  }
}

#The lifecycle moments

HookTypeDescription
onInitprocessThe app is being initialised, before it starts.
onStartprocessThe app has started; warm long-lived resources here.
onHandlingEventper eventAn event is about to be handled.
onExecutingEventHandlerper eventThe handler is about to run.
onExecutingErrorHandlerper eventAn error handler is about to run.
onStopprocessThe app is stopping.
onTerminateprocessFinal teardown; flush and close.

#Imperative hooks

app/hooks.tsts
import { defineHookListener } from '@stone-js/core'

export const hooks = [
  defineHookListener('onStart', ({ container }) => container.resolve('db').connect())
]

Stone.js

Your app exists in every runtime. Until you run it.

An open-source project by Stone Foundation
Created by Mr. Stone (Evens Pierre)