iif<S, T> static method

FlowHandle iif<S, T>(
  1. Cell source, {
  2. required bool test(
    1. S value
    ),
  3. required T thenMap(
    1. S value
    ),
  4. required T elseMap(
    1. S value
    ),
})

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

  1. Each incoming pulse's payload is extracted and type-checked.
  2. The test predicate is evaluated on the payload.
  3. If the predicate returns true, the thenMap function is called.
  4. If the predicate returns false, the elseMap function is called.
  5. The result is emitted as a Pulse.
  6. The instruction preserves causal provenance.

Parameters:

  • source: The Source Cell. The cell providing the input values.
  • test: The Condition. Returns true for the thenMap path.
  • thenMap: The Then Transformation. Called when test is true.
  • elseMap: The Else Transformation. Called when test is 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:

  • partition: For tagging with match status.
  • iif: For multi-path routing.

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);
}