Skip to main content
Version: 3.0

Control Flow

Operaide provides declarative control flow Aktors for conditionals and loops. These let you express branching and iteration within the Aktor graph without resorting to imperative TypeScript control structures.

Conditionals

aktorIfElse

Routes execution to one of two branches based on a boolean condition.

import { aktorIfElse } from '@operaide/aktor';

const result = aktorIfElse({
if_: conditionAktor, // Aktor<boolean>
then_: branchA, // Aktor<T> — evaluated when true
else_: branchB, // Aktor<T> — evaluated when false
});

Only the matching branch is evaluated. The other branch is skipped entirely.

Example — choose a city source based on a setting:

import { aktorIfElse, aktorSetting, aktorConst } from '@operaide/aktor';
import { z } from 'zod';

const useAutoDetect = aktorSetting(z.boolean(), false, 'Auto-detect city from IP');

const city = aktorIfElse({
if_: useAutoDetect,
then_: aktorDetectCityFromIP({}),
else_: params.city,
});

aktorIf

Executes a branch only when the condition is true. Returns null when false.

import { aktorIf } from '@operaide/aktor';

const result = aktorIf({
if_: shouldNotify, // Aktor<boolean>
then_: aktorSendAlert({}), // Aktor<T> — evaluated when true
});
// result: Aktor<T | null>

Loops

aktorDoWhile

Executes the body at least once, then repeats as long as the condition remains true. This is useful when you need to process at least one item before checking whether to continue.

import { aktorDoWhile } from '@operaide/aktor';

const result = aktorDoWhile({
do_: processItem, // Aktor<T> — executed at least once
while_: hasMoreItems, // Aktor<boolean> — checked after each iteration
});

aktorWhileDo

Checks the condition first. If true, executes the body and repeats. If false on the first check, executes the else_ branch instead.

import { aktorWhileDo } from '@operaide/aktor';

const result = aktorWhileDo({
while_: hasItems, // Aktor<boolean> — checked before each iteration
do_: processItem, // Aktor<T> — executed while true
else_: noItemsResult, // Aktor<T> — executed if false on first check
});

Loop Safety

All loops are capped at 100 iterations. If the condition is still true after 100 iterations, the loop throws an error to prevent runaway execution:

Error: While loop condition is always true, this will cause an infinite loop

Summary

AktorBehaviorReturns
aktorIfElseEvaluates one of two branchesAktor<T>
aktorIfEvaluates branch or returns nullAktor<T | null>
aktorDoWhileBody first, then conditionAktor<T>
aktorWhileDoCondition first, else if never trueAktor<T>