alepha@docs:~/framework/docs/reference/primitives$
cat $hook.md | pretty1 min read
#$hook
#Import
typescript
1import { $hook } from "alepha";
#Overview
Registers a new hook.
ts
1import { $hook } from "alepha"; 2 3class MyProvider { 4 onStart = $hook({ 5 on: "start", // or "configure", "ready", "stop", ... 6 handler: async (app) => { 7 // await db.connect(); ... 8 }, 9 });10}
Hooks are used to run async functions from all registered providers/services.
You can't register a hook after the App has started.
It's used under the hood by the configure, start, and stop methods.
Some modules also use hooks to run their own logic. (e.g. alepha/server).
You can create your own hooks by using module augmentation:
ts
1declare module "alepha" {2 interface Hooks {3 "my:task:completed": {4 arg1: string;5 };6 }7}8 9await alepha.events.emit("my:task:completed", { arg1: "value" });
#Options
| Option | Type | Required | Description |
|---|---|---|---|
on |
T |
Yes | The name of the hook |
handler |
Object |
Yes | The handler to run when the hook is triggered. |
priority |
"first" | "last" |
No | Force the hook to run first or last on the list of hooks. |
before |
object | Array<object> |
No | Run this hook before the hooks owned by the specified services. |
after |
object | Array<object> |
No | Run this hook after the hooks owned by the specified services. |