Skip to main content
You are viewing 1.37.x. The latest is 1.45.x.

Waits

View as Markdown

WaitFor returns a Wait with a wait type and the conditions to wait for. Dex persists the waiting state and evaluates the conditions when related state changes. Timer, Channel, and SubFlow are the condition kinds.

  • Until waits for one condition.
  • AnyOf is met when any condition is met.
  • AllOf is met when all conditions are met.
  • AnyCombinationOf is met when any one group of conditions is all met.

Until, AnyOf, and AllOf accept Conditions without condition IDs. Do not add IDs merely because a Condition is nested in one of these waits. Read Channel results from the Channel definition and inspect Timer outcomes through the Context. Add IDs only when another API requires them, such as AnyCombinationOf or a Timer selected by ID.

TypeScript
return Wait.anyOf(
channelA.forOne(),
Timer.byDuration(timeoutMs),
);

Example: examples/typescript/src/primitives/wait-types/wait-types-flow.ts

TypeScript
return Wait.allOf(
channelA.forOne(),
channelB.forOne(),
);

Example: examples/typescript/src/primitives/wait-types/wait-types-flow.ts

Condition results​

After WaitFor completes, Execute can read what satisfied the Wait. For a Channel, use GetConditionResults / results / condition_results to read published messages. For a SubFlow, use SubFlowResult / getConditionResults / condition_result to decode the child Flow output.

Channel consumption is not greedy across AnyOf alternatives. Dex consumes messages only from the Channel condition selected to satisfy the Wait. Other Channel conditions consume nothing even if they are also ready, so their messages remain queued.

When several AnyOf alternatives are ready in one evaluation, Dex selects the first feasible candidate in canonical order: Timer conditions in declaration order, then Channel conditions in declaration order, then SubFlow conditions in declaration order. An earlier condition that is not ready does not block a later ready condition. Mixed condition kinds therefore do not share one global call argument order.

Use AnyOf for a race among the current alternatives. For strict priority that must keep waiting for a higher-priority input, return only that condition until it is resolved.

Channel results​

TypeScript
const approval = new Channel("Approval", stringCodec);

class ChannelWait implements Step<number> {
public readonly inputCodec = doubleCodec;

public getStepType(): string {
return "ChannelWait";
}

public waitFor(_context: Context, input: number): Wait {
return Wait.anyOf(
approval.forOne(),
Timer.byDuration(input * 1000),
);
}

public execute(context: Context, _input: number): StepDecision {
if (context.hasTimerFired()) {
return gracefulComplete("approval timed out");
}
const approvals = approval.results(context);
return gracefulComplete(approvals[0]!);
}
}

const channelWait = new ChannelWait();

Example: examples/typescript/src/primitives/channel/channel-flow.ts

SubFlow results​

TypeScript
public execute(context: Context, _input: number): StepDecision {
const result = SubFlow.getConditionResults(context);
const output = result.singleOutput(doubleCodec);
const flowId = SubFlow.getFlowId(context);
return gracefulComplete(`${flowId}|${output}`);
}

Example: examples/typescript/src/primitives/subflow/subflow-flow.ts

AnyCombinationOf​

AnyCombinationOf expresses an OR of AND groups. Build each inner group as an all-of combination.

Channel consumption is not greedy across combinations. Dex consumes messages only from Channel conditions in the selected combination. Conditions belonging only to other ready combinations consume nothing, so their messages remain queued.

Every Condition in its combinations requires a unique condition ID.

TypeScript
return Wait.anyCombinationOf(
ConditionCombination.of(
channelA.forOne("signal-a"),
Timer.byDuration(timeoutMs, "timeout"),
),
ConditionCombination.of(channelB.forOne("signal-b")),
);

Example: examples/typescript/src/primitives/wait-types/wait-types-flow.ts

Step execution local​

SetStepExecutionLocal stores a value on this StepExecution so Execute can read it with GetStepExecutionLocal. Use it when WaitFor computes something Execute needs but a Flow-level Attribute is overkill. Step input is already passed to both methods. Do not copy input into a local.

TypeScript
public waitFor(context: Context, input: number): Wait {
context.setStepExecutionLocal("note", `approval:${input}`, stringCodec);
return Wait.until(approval.forOne());
}

public execute(context: Context, _input: number): StepDecision {
const note = context.getStepExecutionLocal("note", stringCodec) ?? "";
return gracefulComplete(note);
}

Example: examples/typescript/src/primitives/step-execution-local/step-execution-local-flow.ts

Skip immediately​

SkipWaitImmediately waits for no condition. Use it in a dynamic WaitFor method when one branch must not wait for anything.

TypeScript
return Wait.skipImmediately();

Example: examples/typescript/src/primitives/flow/example-flow.ts