alepha@docs:~/docs/reference/primitives$
cat $hook.md
1 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    name: "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  3  interface Hooks { 4    "my:custom:hook": { 5      arg1: string; 6    } 7  } 8} 9 10await alepha.events.emit("my:custom:hook", { 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 Empty placeholder, not implemented yet
after object | Array<object> No Empty placeholder, not implemented yet