Domain Instance
Domain — returned by defineDomain(). Represents a running flow with durable context, history, and graph introspection.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
id | string | Domain identifier |
phase | string | Current phase ID |
context | TContext | Current context (immutable; updated via events) |
current | CurrentPhaseNode<TContext, TEdges> | Scoped view of the current phase; see below |
graph | DomainGraph | Graph query API; see DomainGraph methods |
CurrentPhaseNode fields:
| Field | Type | Description |
|---|---|---|
phase | string | Current phase ID |
context | TContext | Current context |
can(event, payload?) | (event: TEdges, payload?: unknown) => boolean | Promise<boolean> | Check if an event is allowed |
follow(event, payload?) | (event: TEdges, payload?: unknown) => Promise<DomainFollowResult> | Follow an event |
domain.follow(event, payload?)
Section titled “domain.follow(event, payload?)”Follow a domain event.
Parameters
Section titled “Parameters”event TEdges
Event name.
payload unknown (optional)
Optional event payload passed to guards, actions, and hooks.
Return value
Section titled “Return value”Promise<DomainFollowResult<TContext>> — see DomainFollowResult.
Example
Section titled “Example”import { defineDomain, when, on, target, terminal } from '@tde.io/plexis';
const order = defineDomain('order', () => { when('pending', () => { on('submit', target('processing')); }); when('processing', terminal()); return { context: { orderId: 'o1' }, initial: 'pending' };});
const result = await order.follow('submit', { userId: 'u1' });console.log(result.status); // 'followed'console.log(order.phase); // 'processing'domain.followFrom(expectedPhase, event, payload?)
Section titled “domain.followFrom(expectedPhase, event, payload?)”Follow a domain event from a specific phase. Throws PHASE_MISMATCH if the current phase does not match.
Parameters
Section titled “Parameters”expectedPhase string
Expected current phase ID.
event TEdges
Event name.
payload unknown (optional)
Optional event payload.
Return value
Section titled “Return value”Promise<DomainFollowResult<TContext>>
Example
Section titled “Example”// Only proceeds if domain is currently in 'pending'const result = await order.followFrom('pending', 'submit');domain.can(event, payload?)
Section titled “domain.can(event, payload?)”Check if an event is allowed without running any side effects.
Parameters
Section titled “Parameters”event TEdges
Event name.
payload unknown (optional)
Optional event payload passed to the guard.
Return value
Section titled “Return value”boolean | Promise<boolean>
Example
Section titled “Example”const allowed = await order.can('submit');if (allowed) { await order.follow('submit');}domain.subscribe(listener)
Section titled “domain.subscribe(listener)”Subscribe to phase changes. The listener is called after each successful event.
Parameters
Section titled “Parameters”listener (snapshot: DomainSnapshot<TContext>) => void
Called after each successful event.
Return value
Section titled “Return value”() => void — call to unsubscribe.
Example
Section titled “Example”const unsubscribe = order.subscribe((snapshot) => { console.log('moved to:', snapshot.phase); console.log('context:', snapshot.context);});
await order.follow('submit');// listener fires here
unsubscribe();domain.snapshot()
Section titled “domain.snapshot()”Capture the current phase, context, and history length.
Return value
Section titled “Return value”DomainSnapshot<TContext> — see DomainSnapshot.
Example
Section titled “Example”const snap = order.snapshot();// { phase: 'pending', context: { orderId: 'o1' }, historyLength: 0 }domain.restore(snapshot)
Section titled “domain.restore(snapshot)”Restore from a previously captured snapshot.
Parameters
Section titled “Parameters”snapshot DomainSnapshot<TContext>
Snapshot captured via domain.snapshot().
Return value
Section titled “Return value”None (void).
Example
Section titled “Example”const snap = order.snapshot();await order.follow('submit');
// Roll back:order.restore(snap);console.log(order.phase); // 'pending'domain.history()
Section titled “domain.history()”Return all recorded events.
Return value
Section titled “Return value”DomainHistoryEntry<TContext>[] — see DomainHistoryEntry.
Example
Section titled “Example”await order.follow('submit');const history = order.history();console.log(history[0].from); // 'pending'console.log(history[0].to); // 'processing'console.log(history[0].event); // 'submit'domain.describe()
Section titled “domain.describe()”Return a static graph descriptor.
Return value
Section titled “Return value”GraphDescriptor — see GraphDescriptor.
Example
Section titled “Example”const descriptor = order.describe();console.log(descriptor.nodes.map((n) => n.id)); // ['pending', 'processing']domain.trace(format?)
Section titled “domain.trace(format?)”Export trace data for this domain.
Parameters
Section titled “Parameters”format 'json' | 'text' | 'tree' (optional)
Export format.
Return value
Section titled “Return value”unknown
Example
Section titled “Example”const json = order.trace('json');const tree = order.trace('tree');domain.inspectNode(node, options?)
Section titled “domain.inspectNode(node, options?)”Inspect a node’s paths and attached pipelines.
Parameters
Section titled “Parameters”node string | GraphNodeRef
Node ID or graph node reference.
options PathQueryOptions (optional)
| Field | Type | Required | Description |
|---|---|---|---|
maxDepth | number | No | Maximum path depth to traverse |
includeCycles | boolean | No | When true, cyclic paths are included in results |
includeCrossBoundary | boolean | No | When true, paths crossing domain/pipeline boundaries are included |
direction | 'inbound' | 'outbound' | No | Traversal direction for reachability queries |
Return value
Section titled “Return value”NodeInspection — see NodeInspection.
Example
Section titled “Example”const info = order.inspectNode('pending');console.log(info.outbound.map((e) => e.label)); // outbound edges from 'pending'console.log(info.pathsFrom.length); // number of paths from 'pending'DomainGraph methods
Section titled “DomainGraph methods”Accessed via domain.graph.
| Method | Parameters | Returns | Description |
|---|---|---|---|
describe() | — | GraphDescriptor | Static graph descriptor for this domain |
node(id) | id: string | GraphNode | undefined | Look up a graph node by ID |
inbound(id) | id: string | GraphEdge[] | All edges pointing to the given node |
outbound(id) | id: string | GraphEdge[] | All edges pointing from the given node |
pathsTo(id, options?) | id: string, options?: PathQueryOptions | GraphPath[] | All paths leading to the given node |
pathsFrom(id, options?) | id: string, options?: PathQueryOptions | GraphPath[] | All paths starting from the given node |
reachableFrom(id, options?) | id: string, options?: PathQueryOptions | GraphNode[] | All nodes reachable from the given node |
observedPathsTo(id) | id: string | GraphPath[] | Paths to the given node observed at runtime |
observedPathsFrom(id) | id: string | GraphPath[] | Paths from the given node observed at runtime |
Example
Section titled “Example”const graph = order.graph;
// Static queries:const node = graph.node('pending');const edges = graph.outbound('pending');const paths = graph.pathsFrom('pending', { maxDepth: 3 });const reachable = graph.reachableFrom('pending');
// Runtime-observed queries (populated after events run):const observedPaths = graph.observedPathsFrom('pending');