Skip to content
@tde.io/plexis  ·  npm install @tde.io/plexis  ·  npm  ·  GitHub

Domain Instance

Domain — returned by defineDomain(). Represents a running flow with durable context, history, and graph introspection.

PropertyTypeDescription
idstringDomain identifier
phasestringCurrent phase ID
contextTContextCurrent context (immutable; updated via events)
currentCurrentPhaseNode<TContext, TEdges>Scoped view of the current phase; see below
graphDomainGraphGraph query API; see DomainGraph methods

CurrentPhaseNode fields:

FieldTypeDescription
phasestringCurrent phase ID
contextTContextCurrent 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

Follow a domain event.

event TEdges

Event name.

payload unknown (optional)

Optional event payload passed to guards, actions, and hooks.

Promise<DomainFollowResult<TContext>> — see DomainFollowResult.

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.

expectedPhase string

Expected current phase ID.

event TEdges

Event name.

payload unknown (optional)

Optional event payload.

Promise<DomainFollowResult<TContext>>

// Only proceeds if domain is currently in 'pending'
const result = await order.followFrom('pending', 'submit');

Check if an event is allowed without running any side effects.

event TEdges

Event name.

payload unknown (optional)

Optional event payload passed to the guard.

boolean | Promise<boolean>

const allowed = await order.can('submit');
if (allowed) {
await order.follow('submit');
}

Subscribe to phase changes. The listener is called after each successful event.

listener (snapshot: DomainSnapshot<TContext>) => void

Called after each successful event.

() => void — call to unsubscribe.

const unsubscribe = order.subscribe((snapshot) => {
console.log('moved to:', snapshot.phase);
console.log('context:', snapshot.context);
});
await order.follow('submit');
// listener fires here
unsubscribe();

Capture the current phase, context, and history length.

DomainSnapshot<TContext> — see DomainSnapshot.

const snap = order.snapshot();
// { phase: 'pending', context: { orderId: 'o1' }, historyLength: 0 }

Restore from a previously captured snapshot.

snapshot DomainSnapshot<TContext>

Snapshot captured via domain.snapshot().

None (void).

const snap = order.snapshot();
await order.follow('submit');
// Roll back:
order.restore(snap);
console.log(order.phase); // 'pending'

Return all recorded events.

DomainHistoryEntry<TContext>[] — see DomainHistoryEntry.

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'

Return a static graph descriptor.

GraphDescriptor — see GraphDescriptor.

const descriptor = order.describe();
console.log(descriptor.nodes.map((n) => n.id)); // ['pending', 'processing']

Export trace data for this domain.

format 'json' | 'text' | 'tree' (optional)

Export format.

unknown

const json = order.trace('json');
const tree = order.trace('tree');

Inspect a node’s paths and attached pipelines.

node string | GraphNodeRef

Node ID or graph node reference.

options PathQueryOptions (optional)

FieldTypeRequiredDescription
maxDepthnumberNoMaximum path depth to traverse
includeCyclesbooleanNoWhen true, cyclic paths are included in results
includeCrossBoundarybooleanNoWhen true, paths crossing domain/pipeline boundaries are included
direction'inbound' | 'outbound'NoTraversal direction for reachability queries

NodeInspection — see NodeInspection.

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'

Accessed via domain.graph.

MethodParametersReturnsDescription
describe()GraphDescriptorStatic graph descriptor for this domain
node(id)id: stringGraphNode | undefinedLook up a graph node by ID
inbound(id)id: stringGraphEdge[]All edges pointing to the given node
outbound(id)id: stringGraphEdge[]All edges pointing from the given node
pathsTo(id, options?)id: string, options?: PathQueryOptionsGraphPath[]All paths leading to the given node
pathsFrom(id, options?)id: string, options?: PathQueryOptionsGraphPath[]All paths starting from the given node
reachableFrom(id, options?)id: string, options?: PathQueryOptionsGraphNode[]All nodes reachable from the given node
observedPathsTo(id)id: stringGraphPath[]Paths to the given node observed at runtime
observedPathsFrom(id)id: stringGraphPath[]Paths from the given node observed at runtime
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');