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

Definition Functions

These functions create Domain and Pipeline instances from a synchronous setup function.

Creates and returns a Domain instance. The setup function runs synchronously; when() and on() registrations are collected and used to build the domain.

function defineDomain<TContext, TEdges>(
id: string,
setup: () => DomainSetupResult<TContext>,
options?: DefineDomainOptions<TContext>
): Domain<TContext, TEdges>

id string

Unique domain identifier.

setup () => DomainSetupResult<TContext>

Synchronous setup function. when() calls inside register states. Must return a DomainSetupResult:

FieldTypeRequiredDefaultDescription
contextTContextYesInitial context object
initialstringYesID of the initial state
strictbooleanNofalseWhen true, follow() throws on unknown events instead of returning status: 'ignored'
errorPolicyErrorPolicyNoDefault error policy for handlers in this domain

options DefineDomainOptions<TContext> (optional)

Additional configuration:

FieldTypeRequiredDescription
tracerTracerNoTracer instance to attach
merge(prev: TContext, patch: Partial<TContext>, metadata: MergeMetadata) => TContextNoCustom context merge function; called instead of the default shallow spread

Domain<TContext, TEdges>

import { defineDomain, when, on, terminal } from '@tde.io/plexis';
type OrderContext = { orderId: string | null; total: number };
const order = defineDomain<OrderContext>('order', () => {
when('pending', () => {
on('submit', target('processing'));
});
when('processing', () => {
on('fulfill', target('fulfilled'));
});
when('fulfilled', terminal());
return { context: { orderId: null, total: 0 }, initial: 'pending' };
});

Creates and returns a Pipeline instance. The setup function runs synchronously; node() registrations are collected.

function definePipeline<TContext>(
id: string,
setup: () => PipelineSetupResult,
options?: DefinePipelineOptions<TContext>
): Pipeline<TContext>

id string

Unique pipeline identifier.

setup () => PipelineSetupResult

Synchronous setup function. node() calls inside register nodes. Must return a PipelineSetupResult:

FieldTypeRequiredDescription
initialstringYesID of the first node to execute

options DefinePipelineOptions<TContext> (optional)

Additional configuration:

FieldTypeRequiredDescription
tracerTracerNoTracer instance to attach
errorPolicyErrorPolicyNoDefault error policy for node actions
merge(prev: TContext, patch: Partial<TContext>, metadata: MergeMetadata) => TContextNoCustom context merge function; called instead of the default shallow spread

Pipeline<TContext>

import { definePipeline, node, action, fork, target, terminal } from '@tde.io/plexis';
type PaymentContext = { cardValid: boolean; charged: boolean };
const payment = definePipeline<PaymentContext>('payment', () => {
node('validate', () => {
action(async (ctx) => ({ cardValid: ctx.cardValid }));
fork('charge', target('charge'), (ctx) => ctx.cardValid);
fork('decline', target('decline'), (ctx) => !ctx.cardValid);
});
node('charge', terminal(async () => ({ charged: true })));
node('decline', terminal());
return { initial: 'validate' };
});

Passed as the third argument to a custom merge function. Describes the execution context in which a context patch is being applied.

FieldTypeRequiredDescription
phasestringYesExecution phase (e.g. 'flow-action', 'pipeline-action', 'enter')
domainIdstringNoID of the enclosing domain, when in a domain context
pipelineIdstringNoID of the enclosing pipeline, when in a pipeline context
nodeIdstringNoID of the current pipeline node
phaseIdstringNoID of the current domain phase
eventstringNoEvent name that triggered the transition
import { defineDomain } from '@tde.io/plexis';
import type { MergeMetadata } from '@tde.io/plexis';
type Ctx = { count: number; lastPhase: string };
const domain = defineDomain<Ctx>(
'counter',
() => {
// ... states ...
return { context: { count: 0, lastPhase: '' }, initial: 'idle' };
},
{
merge(prev, patch, meta: MergeMetadata) {
return { ...prev, ...patch, lastPhase: meta.phase };
},
}
);