createRuntime
Stable · since 0.1.0
createRuntime returns an isolated Runtime — the container that holds triggers, conditions and actions for a tree of components. Most apps create one runtime in main.tsx and pass it down via <TriggerRuntimeProvider>. Skip the call entirely and Triggery uses a lazily created default runtime via getDefaultRuntime.
Import
Section titled “Import”import { createRuntime } from '@triggery/core';
Signature
Section titled “Signature”function createRuntime(options?: RuntimeOptions): Runtime;Options
Section titled “Options”| Field | Type | Default | Description |
|---|---|---|---|
inspector | boolean | { dev?: boolean; prod?: boolean } | DEV true, PROD false | Enable / disable the per-run inspector. |
inspectorBufferSize | number | 50 | Inspector ring-buffer size. Ignored when the inspector is disabled. |
middleware | readonly Middleware[] | [] | Middleware chain. |
maxCascadeDepth | number | 3 | Maximum cascade depth — action → fireEvent → …. Runs above this are skipped with 'overflow'. |
See RuntimeOptions for the full shape.
Returns
Section titled “Returns”A Runtime object. The methods most apps touch:
| Method | Description |
|---|---|
runtime.fire(event, payload) | Dispatch an event through the scheduler. Returns void. |
runtime.fireSync(event, payload) | Dispatch synchronously (bypass scheduler). For tests and bench. |
runtime.subscribe(listener) | Listen for every recorded run. Returns a RegistrationToken. |
runtime.getInspectorBuffer() | Most-recent N inspector snapshots. |
runtime.getTrigger(id) | Look up a registered trigger. |
runtime.graph() | JSON-friendly snapshot of the registry — used by triggery graph CLI. |
runtime.dispose() | Abort all in-flight runs and drop all registrations. |
register* methods (registerTrigger, registerCondition, registerAction) are also part of the surface, but bindings handle them for you.
Examples
Section titled “Examples”Default — one runtime at the root
Section titled “Default — one runtime at the root”import { createRuntime } from '@triggery/core';
import { TriggerRuntimeProvider } from '@triggery/react';
import type { ComponentType } from 'react';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
const runtime = createRuntime();
declare const App: ComponentType;
createRoot(document.getElementById('root')!).render(
<StrictMode>
<TriggerRuntimeProvider runtime={runtime}>
<App />
</TriggerRuntimeProvider>
</StrictMode>,
);Isolated runtime per test
Section titled “Isolated runtime per test”import { createRuntime function createRuntime(options?: RuntimeOptions): Runtime } from '@triggery/core';
import { afterEach function afterEach<ExtraContext = object>(fn: AfterEachListener<ExtraContext>, timeout?: number): voidRegisters a callback function to be executed after each test within the current suite has completed.
This hook is useful for scenarios where you need to clean up or reset the test environment after each test runs, such as deleting temporary files, clearing test-specific database entries, or resetting mocked functions.
**Note:** The `afterEach` hooks are running in reverse order of their registration. You can configure this by changing the `sequence.hooks` option in the config file. , beforeEach function beforeEach<ExtraContext = object>(fn: BeforeEachListener<ExtraContext>, timeout?: number): voidRegisters a callback function to be executed before each test within the current suite.
This hook is useful for scenarios where you need to reset or reinitialize the test environment before each test runs, such as resetting database states, clearing caches, or reinitializing variables.
**Note:** The `beforeEach` hooks are executed in the order they are defined one after another. You can configure this by changing the `sequence.hooks` option in the config file. } from 'vitest';
let runtime let runtime: Runtime = createRuntime function createRuntime(options?: RuntimeOptions): Runtime ();
beforeEach beforeEach<object>(fn: BeforeEachListener<object>, timeout?: number): voidRegisters a callback function to be executed before each test within the current suite.
This hook is useful for scenarios where you need to reset or reinitialize the test environment before each test runs, such as resetting database states, clearing caches, or reinitializing variables.
**Note:** The `beforeEach` hooks are executed in the order they are defined one after another. You can configure this by changing the `sequence.hooks` option in the config file. (() => {
runtime let runtime: Runtime = createRuntime function createRuntime(options?: RuntimeOptions): Runtime ({ inspectorBufferSize inspectorBufferSize?: number | undefinedInspector ring buffer size (default: 50). Ignored when the inspector is disabled. : 100 });
});
afterEach afterEach<object>(fn: AfterEachListener<object>, timeout?: number): voidRegisters a callback function to be executed after each test within the current suite has completed.
This hook is useful for scenarios where you need to clean up or reset the test environment after each test runs, such as deleting temporary files, clearing test-specific database entries, or resetting mocked functions.
**Note:** The `afterEach` hooks are running in reverse order of their registration. You can configure this by changing the `sequence.hooks` option in the config file. (() => {
runtime let runtime: Runtime .dispose function dispose(): voidTear down the runtime completely. ();
});Tight cascade limit
Section titled “Tight cascade limit”import { createRuntime function createRuntime(options?: RuntimeOptions): Runtime } from '@triggery/core';
const runtime const runtime: Runtime = createRuntime function createRuntime(options?: RuntimeOptions): Runtime ({
maxCascadeDepth maxCascadeDepth?: number | undefinedMaximum cascade depth (action → fireEvent → ...). Default: 3. : 1, // refuse any indirect chain — action→event→trigger is enough
});With middleware
Section titled “With middleware”import { createRuntime function createRuntime(options?: RuntimeOptions): Runtime , type Middleware type Middleware = {
readonly name: string;
onFire?(ctx: FireContext): void | {
cancel: true;
reason: string;
};
onBeforeMatch?(ctx: MatchContext): void;
onSkip?(ctx: SkipContext): void;
onActionStart?(ctx: ActionContext): void;
onActionEnd?(ctx: ActionContext & {
durationMs: number;
result?: unknown;
}): void;
onError?(ctx: ActionContext & {
error: unknown;
}): void;
onCascade?(ctx: CascadeContext): void;
}
} from '@triggery/core';
const logger const logger: Middleware : Middleware type Middleware = {
readonly name: string;
onFire?(ctx: FireContext): void | {
cancel: true;
reason: string;
};
onBeforeMatch?(ctx: MatchContext): void;
onSkip?(ctx: SkipContext): void;
onActionStart?(ctx: ActionContext): void;
onActionEnd?(ctx: ActionContext & {
durationMs: number;
result?: unknown;
}): void;
onError?(ctx: ActionContext & {
error: unknown;
}): void;
onCascade?(ctx: CascadeContext): void;
}
= {
name name: string : 'logger',
onActionEnd onActionEnd?(ctx: ActionContext & {
durationMs: number;
result?: unknown;
}): void
({ triggerId triggerId: string , actionName actionName: string , durationMs durationMs: 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) (`[${triggerId triggerId: string }] ${actionName actionName: string } ${durationMs durationMs: number .toFixed Number.toFixed(fractionDigits?: number): stringReturns a string representing a number in fixed-point notation. (2)}ms`);
},
};
const runtime const runtime: Runtime = createRuntime function createRuntime(options?: RuntimeOptions): Runtime ({ middleware middleware?: readonly Middleware[] | undefinedGlobal middleware applied to every trigger in this runtime. : [logger const logger: Middleware ] });Inspector explicitly disabled
Section titled “Inspector explicitly disabled”import { createRuntime function createRuntime(options?: RuntimeOptions): Runtime } 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
}
. : false });Or per-environment:
import { createRuntime function createRuntime(options?: RuntimeOptions): Runtime } 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 },
});Related
Section titled “Related” getDefaultRuntime Access (and lazily create) the global singleton.
setDefaultRuntime Replace the singleton — useful in tests.
Runtime type Full method surface.
Runtime guide Narrative walkthrough of scopes, isolation, lifecycle.