iif<S, T> static method
A binary router that chooses between thenMap and elseMap based on
test (Rx iif).
iif acts as a Conditional Router. It evaluates a predicate on the
payload and routes the pulse to either the thenMap or elseMap
transformation path.
When to use
Use iif when you need to branch based on a boolean condition:
- Success vs Error: Different handling for success and error payloads
- Feature Flags: Different transformations based on feature flags
- Cheap vs Expensive: Different processing for cheap vs expensive operations
- Validation: Different handling for valid vs invalid inputs
- Type Checking: Different handling based on payload type
- State-Dependent Logic: Different behavior based on application state
- User Roles: Different transformations for admin vs regular users
- Environment: Different behavior based on environment (dev vs prod)
How it works
- Each incoming pulse's payload is extracted and type-checked.
- The
testpredicate is evaluated on the payload. - If the predicate returns
true, thethenMapfunction is called. - If the predicate returns
false, theelseMapfunction is called. - The result is emitted as a Pulse.
- The instruction preserves causal provenance.
Parameters:
source: The Source Cell. The cell providing the input values.test: The Condition. Returnstruefor thethenMappath.thenMap: The Then Transformation. Called whentestis true.elseMap: The Else Transformation. Called whentestis false.
Type Parameters:
S: The type of the input payload.T: The type of the output payload.
Returns:
A FlowHandle that can be used to observe the routed values.
Example
final status = Cell.ingress<int>();
final handle = Flow.iif<int, String>(
status.cell,
test: (code) => code < 400,
thenMap: (c) => 'ok-$c',
elseMap: (c) => 'err-$c',
);
status.emit(200); // -> ok-200
status.emit(404); // -> err-404
See Also:
Implementation
static FlowHandle iif<S, T>(
Cell source, {
required bool Function(S value) test,
required T Function(S value) thenMap,
required T Function(S value) elseMap,
}) {
return Iif<S, T>(
test,
thenMap: thenMap,
elseMap: elseMap,
).toHandle(source: source);
}