createTrigger
Stable · since 0.1.0
The only constructor in Triggery. Returns a Trigger<Schema> registered in the given runtime (or the default runtime if omitted). Most apps create one trigger per scenario, one file per trigger.
Import
Section titled “Import”import { createTrigger } from '@triggery/core';
Signature
Section titled “Signature”function createTrigger<S extends TriggerSchema>(
config: CreateTriggerConfig<S>,
runtime?: Runtime,
): Trigger<S>;The generic S is the schema — three optional maps for events, conditions and actions. It is never inferred in normal use; always passed explicitly.
Schema
Section titled “Schema”type TriggerSchema = {
events?: Record<string, unknown>; // payload type per event name
conditions?: Record<string, unknown>; // value type per condition name
actions?: Record<string, unknown>; // payload type per action name
};For a void payload, use void (events) or undefined (actions). Names can be any valid string; convention is kebab-case verbs for events, camelCase for conditions and actions.
Config
Section titled “Config”| Field | Type | Required | Default | Description |
|---|---|---|---|---|
id | string (literal) | yes | — | Unique runtime registry key. Must be a string literal — see no-dynamic-id. |
events | readonly EventKey<S>[] | yes | — | Event names from S['events'] this trigger reacts to. |
required | readonly ConditionKey<S>[] | no | [] | Conditions that must be registered for the handler to run. |
schedule | 'microtask' | 'sync' | no | 'microtask' | When to dispatch matching events. |
concurrency | ConcurrencyStrategy | no | 'take-latest' | How async handler runs interact. |
scope | string | no | '' | Restrict to a <TriggerScope id="…"> subtree. |
handler | TriggerHandler<S> | yes | — | Function called when an event matches and required are satisfied. |
Returns
Section titled “Returns”A Trigger<S> object — a stable identity you can pass to React/Solid/Vue hooks.
| Method | Description |
|---|---|
trigger.id | Same string you passed in. |
trigger.enable() | Re-enable a disabled trigger. |
trigger.disable() | Skip future events (records 'disabled' in inspector). |
trigger.isEnabled() | Boolean. |
trigger.inspect() | Latest TriggerInspectSnapshot or undefined. |
trigger.dispose() | Unregister from the runtime. Rare — use in tests. |
trigger.namedHooks() | Throws on @triggery/core directly — use createNamedHooks from a binding. |
Examples
Section titled “Examples”Minimal trigger
Section titled “Minimal trigger”import { createTrigger function createTrigger<S extends TriggerSchema>(config: CreateTriggerConfig<S>, runtime?: Runtime): Trigger<S>Create a trigger and register it in a runtime (the default runtime if none is passed). } from '@triggery/core';
const ping const ping: Trigger<{
events: {
tick: number;
};
}>
= createTrigger createTrigger<{
events: {
tick: number;
};
}>(config: CreateTriggerConfig<{
events: {
tick: number;
};
}>, runtime?: Runtime): Trigger<{
events: {
tick: number;
};
}>
Create a trigger and register it in a runtime (the default runtime if none is passed). <{
events events: {
tick: number;
}
: { tick tick: number : number };
}>({
id id: string : 'demo:ping',
events events: readonly "tick"[] : ['tick'],
handler handler: TriggerHandler<{
events: {
tick: number;
};
}, never>
({ event event: {
readonly name: "tick";
readonly payload: number;
}
}) {
console var console: Console .log Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) ('tick', event event: {
readonly name: "tick";
readonly payload: number;
}
.payload payload: number );
},
});Full schema with all three maps
Section titled “Full schema with all three maps”import { createTrigger function createTrigger<S extends TriggerSchema>(config: CreateTriggerConfig<S>, runtime?: Runtime): Trigger<S>Create a trigger and register it in a runtime (the default runtime if none is passed). } from '@triggery/core';
type Settings type Settings = {
sound: boolean;
notifications: boolean;
}
= { sound sound: boolean : boolean; notifications notifications: boolean : boolean };
type Message type Message = {
author: string;
text: string;
channelId: string;
}
= { author author: string : string; text text: string : string; channelId channelId: string : string };
const messageTrigger const messageTrigger: Trigger<{
events: {
"new-message": Message;
};
conditions: {
settings: Settings;
activeChannelId: string | null;
};
actions: {
showToast: {
title: string;
body: string;
};
playSound: "beep";
};
}>
= createTrigger createTrigger<{
events: {
"new-message": Message;
};
conditions: {
settings: Settings;
activeChannelId: string | null;
};
actions: {
showToast: {
title: string;
body: string;
};
playSound: "beep";
};
}>(config: CreateTriggerConfig<{
events: {
"new-message": Message;
};
conditions: {
settings: Settings;
activeChannelId: string | null;
};
actions: {
showToast: {
title: string;
body: string;
};
playSound: "beep";
};
}>, runtime?: Runtime): Trigger<{
events: {
"new-message": Message;
};
conditions: {
settings: Settings;
activeChannelId: string | null;
};
actions: {
showToast: {
title: string;
body: string;
};
playSound: "beep";
};
}>
Create a trigger and register it in a runtime (the default runtime if none is passed). <{
events events: {
'new-message': Message;
}
: { 'new-message': Message type Message = {
author: string;
text: string;
channelId: string;
}
};
conditions conditions: {
settings: Settings;
activeChannelId: string | null;
}
: { settings settings: Settings : Settings type Settings = {
sound: boolean;
notifications: boolean;
}
; activeChannelId activeChannelId: string | null : string | null };
actions actions: {
showToast: {
title: string;
body: string;
};
playSound: "beep";
}
: { showToast showToast: {
title: string;
body: string;
}
: { title title: string : string; body body: string : string }; playSound playSound: "beep" : 'beep' };
}>({
id id: string : 'message-received',
events events: readonly "new-message"[] : ['new-message'],
required required?: readonly ("settings" | "activeChannelId")[] | undefinedRequired condition keys. The trigger will not run unless all of them are registered. : ['settings'],
handler handler: TriggerHandler<{
events: {
"new-message": Message;
};
conditions: {
settings: Settings;
activeChannelId: string | null;
};
actions: {
showToast: {
title: string;
body: string;
};
playSound: "beep";
};
}, never>
({ event event: {
readonly name: "new-message";
readonly payload: Message;
}
, conditions conditions: ConditionsCtx<{
settings: Settings;
activeChannelId: string | null;
}, never>
, actions actions: ActionsCtx<{
showToast: {
title: string;
body: string;
};
playSound: "beep";
}>
, check check: CheckCtx<{
settings: Settings;
activeChannelId: string | null;
}>
}) {
if (event event: {
readonly name: "new-message";
readonly payload: Message;
}
.payload payload: Message .channelId channelId: string === conditions conditions: ConditionsCtx<{
settings: Settings;
activeChannelId: string | null;
}, never>
.activeChannelId activeChannelId?: string | null | undefined ) return;
if (!check check: CheckCtx<{
settings: Settings;
activeChannelId: string | null;
}>
.is is<"settings">(key: "settings", predicate: (value: Settings) => boolean): boolean ('settings', s s: Settings => s s: Settings .notifications notifications: boolean )) return;
actions actions: ActionsCtx<{
showToast: {
title: string;
body: string;
};
playSound: "beep";
}>
.debounce function debounce(ms: number): ActionsCtx<{
showToast: {
title: string;
body: string;
};
playSound: "beep";
}>
(800).playSound playSound?: ((payload: "beep") => void) | undefined ?.('beep');
actions actions: ActionsCtx<{
showToast: {
title: string;
body: string;
};
playSound: "beep";
}>
.showToast showToast?: ((payload: {
title: string;
body: string;
}) => void) | undefined
?.({
title title: string : event event: {
readonly name: "new-message";
readonly payload: Message;
}
.payload payload: Message .author author: string ,
body body: string : event event: {
readonly name: "new-message";
readonly payload: Message;
}
.payload payload: Message .text text: string ,
});
},
});Async handler with AbortSignal
Section titled “Async handler with AbortSignal”import { createTrigger function createTrigger<S extends TriggerSchema>(config: CreateTriggerConfig<S>, runtime?: Runtime): Trigger<S>Create a trigger and register it in a runtime (the default runtime if none is passed). } from '@triggery/core';
const fetchTrigger const fetchTrigger: Trigger<{
events: {
"fetch-user": string;
};
actions: {
setUser: {
id: string;
name: string;
};
};
}>
= createTrigger createTrigger<{
events: {
"fetch-user": string;
};
actions: {
setUser: {
id: string;
name: string;
};
};
}>(config: CreateTriggerConfig<{
events: {
"fetch-user": string;
};
actions: {
setUser: {
id: string;
name: string;
};
};
}>, runtime?: Runtime): Trigger<{
events: {
"fetch-user": string;
};
actions: {
setUser: {
id: string;
name: string;
};
};
}>
Create a trigger and register it in a runtime (the default runtime if none is passed). <{
events events: {
'fetch-user': string;
}
: { 'fetch-user': string };
actions actions: {
setUser: {
id: string;
name: string;
};
}
: { setUser setUser: {
id: string;
name: string;
}
: { id id: string : string; name name: string : string } };
}>({
id id: string : 'fetch-user',
events events: readonly "fetch-user"[] : ['fetch-user'],
concurrency concurrency?: ConcurrencyStrategy | undefinedConcurrency strategy applied across handler runs (default: `'take-latest'`).
- `take-latest` — new run aborts the previous (`signal.aborted` becomes true).
- `take-every` — every run proceeds independently, no aborts.
- `take-first` / `exhaust` — new runs are skipped while one is still in flight.
- `queue` — new runs wait for the previous to finish (serialized).
- `sync` — like `take-every`; provided as a documentation marker. : 'take-latest',
async handler handler: TriggerHandler<{
events: {
"fetch-user": string;
};
actions: {
setUser: {
id: string;
name: string;
};
};
}, never>
({ event event: {
readonly name: "fetch-user";
readonly payload: string;
}
, actions actions: ActionsCtx<{
setUser: {
id: string;
name: string;
};
}>
, signal signal: AbortSignal }) {
const res const res: Response = await fetch function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)[MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/fetch) (`/api/users/${event event: {
readonly name: "fetch-user";
readonly payload: string;
}
.payload payload: string }`, { signal RequestInit.signal?: AbortSignal | null | undefinedAn AbortSignal to set request's signal. });
if (signal signal: AbortSignal .aborted AbortSignal.aborted: booleanThe **`aborted`** read-only property returns a value that indicates whether the asynchronous operations the signal is communicating with are aborted (`true`) or not (`false`).
[MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/aborted) ) return;
actions actions: ActionsCtx<{
setUser: {
id: string;
name: string;
};
}>
.setUser setUser?: ((payload: {
id: string;
name: string;
}) => void) | undefined
?.(await res const res: Response .json Body.json(): Promise<any>[MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) ());
},
});Registered in a specific runtime
Section titled “Registered in a specific runtime”import { createRuntime function createRuntime(options?: RuntimeOptions): Runtime , createTrigger function createTrigger<S extends TriggerSchema>(config: CreateTriggerConfig<S>, runtime?: Runtime): Trigger<S>Create a trigger and register it in a runtime (the default runtime if none is passed). } from '@triggery/core';
const runtime const runtime: Runtime = createRuntime function createRuntime(options?: RuntimeOptions): Runtime ({ inspector inspector?: InspectorOption | undefinedEnable / disable the per-run inspector. See
{@link
InspectorOption
}
. : { dev dev?: boolean | undefined : true, prod prod?: boolean | undefined : false } });
const trigger const trigger: Trigger<{
events: {
hi: void;
};
}>
= createTrigger createTrigger<{
events: {
hi: void;
};
}>(config: CreateTriggerConfig<{
events: {
hi: void;
};
}>, runtime?: Runtime): Trigger<{
events: {
hi: void;
};
}>
Create a trigger and register it in a runtime (the default runtime if none is passed). <{ events events: {
hi: void;
}
: { hi hi: void : void } }>(
{
id id: string : 'demo:hi',
events events: readonly "hi"[] : ['hi'],
handler handler: TriggerHandler<{
events: {
hi: void;
};
}, never>
() {
console var console: Console .log Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) ('hi');
},
},
runtime const runtime: Runtime ,
);Related
Section titled “Related” createRuntime The container that holds triggers, conditions and actions.
TriggerSchema The generic type — events / conditions / actions maps.
TriggerHandler The function type of the handler argument.
Trigger anatomy guide Field-by-field walkthrough, in narrative form.