createTrigger
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”Signature
Section titled “Signature”The generic S is the schema — three optional maps for events, conditions and actions. It is never inferred in normal use; always passed explicitly.
Both forms produce the same Trigger<S> object at runtime — the builder is purely a typing convenience that narrows required conditions automatically (no !, no if (!conditions.x) return;). See TriggerBuilder below.
Schema
Section titled “Schema”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. |
conditions | { [K in ConditionKey<S>]?: ConditionMap<S>[K] | null } | no | — | v0.10+ Initial values for conditions owned by this trigger. Updated via trigger.setCondition(name, value). |
required | readonly ConditionKey<S>[] | no | [] | Conditions that must be registered (and non-null/non-undefined) 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.setCondition(name, value) | v0.10+ Update an inline condition declared in conditions: config. No-op (DEV warn-once) for keys not in config. |
trigger.action(name) | v0.10+ Get the typed multi-subscriber channel for an action. Cached per (trigger, name). See ActionChannel. |
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”Full schema with all three maps
Section titled “Full schema with all three maps”Async handler with AbortSignal
Section titled “Async handler with AbortSignal”Registered in a specific runtime
Section titled “Registered in a specific runtime”Trigger builder (v0.10+)
Section titled “Trigger builder (v0.10+)”Calling createTrigger<S>() with no arguments returns a TriggerBuilder<S>. The builder accumulates configuration through chainable methods and finalizes with .handle(handler) — at which point the handler sees required conditions narrowed to NonNullable<...>. Import from @triggery/core/builder — the subpath keeps the builder machinery out of the main bundle.
Methods:
| Method | Description |
|---|---|
.id(string) | Required. Same as the imperative id field. |
.events([...]) | Required. Same as the imperative events field. |
.conditions({ ... }) | Optional inline values, same shape as the imperative conditions: field. |
.require(...keys) | Add required condition keys. Calling .require('a').require('b') ≡ .require('a', 'b'). Narrows conditions.<key> to NonNullable<...> in .handle. |
.schedule('sync' | 'microtask') | Override the scheduler. |
.concurrency(strategy) | Override the concurrency strategy. |
.scope(string) | Set the scope id (for <TriggerScope> isolation). |
.handle(handler) | Finalize and register the trigger with the default runtime. Returns Trigger<S>. |
The chained order doesn’t matter; .handle validates that id and events were set.
ActionChannel
Section titled “ActionChannel”Returned by trigger.action(name). A typed multi-subscriber channel for one action of the trigger — the v0.10 replacement for the v0.9 Set<callback> + for-of fan-out pattern.
Channel subscribers and any runtime.registerAction(...) handler for the same key both fire on every emit — the channel path is additive, not a replacement. The channel itself is cached per (trigger, name) so repeat calls return the same object.