Skip to content

Framework hooks

Lifecycle slots a plugin can observe with on(hookName, fn). One primitive for everything reactive in the framework's machinery.

Why this exists

A framework hook is a named point in the app's life — booting, ready, shutting down — that a plugin can react to. Hooks are how cross-cutting behavior wires into the lifecycle without being copy-pasted into every handler.

Hooks are keyed by name, not by an imported value. You subscribe with on("AppBooted", fn) and the payload is typed from the name. This is the framework's machinery; a domain event is a different thing, and you react to it with when in a listener. on for hooks, when for events — one rule, no overlap.

App lifecycle hooks

HookWhen
AppBootedAfter every initializer, provider, and plugin has booted.
AppReadyOnce a transport (HTTP/queue) is listening.
AppShuttingDownAt the top of app.stop().
PluginBootedAfter a single plugin finishes its boot step.
WireMountedAfter a wire is mounted on an adapter.

Subscribing from a plugin

A plugin's context carries on. The payload is typed from the hook name:

ts
import { definePlugin } from "@nwire/app";

const readyLog = definePlugin("ready-log", ({ on }) => {
  on("AppBooted", ({ appName, bootedAt }) => {
    console.log(`${appName} booted at ${bootedAt}`);
  });
});

Declaring your own hook slot

A plugin can declare a new slot that other plugins tap:

ts
import { definePlugin } from "@nwire/app";

const cachePlugin = definePlugin("cache", ({ defineHook, on }) => {
  defineHook("CacheInvalidated");
  on("AppShuttingDown", () => {
    // flush before exit
  });
});

Other plugins then observe on("CacheInvalidated", fn) once the slot exists.

Dispatch-stage hooks — in flux

The per-dispatch hooks (around action dispatch and event recording) are being reshaped by the forge → runtime convergence. The names and the subscribe surface for that layer are not settled yet, so they are left undocumented here on purpose. Revisit once the convergence lands. For the stable lifecycle hooks, use the table above.

See also

MIT licensed.