mergeMap<S, T> static method

FlowHandle mergeMap<S, T>(
  1. Cell source, {
  2. required FutureOr<Object?> project(
    1. S value
    ),
})

Maps each payload to an inner sequence and flattens them concurrently (Rx mergeMap).

mergeMap maps each incoming value to an inner sequence and emits values from all inner sequences as they complete, with optional concurrency control.

When to use

Use mergeMap when you need to process multiple async operations concurrently and don't need to cancel previous operations.

  • Parallel HTTP Calls: Making multiple independent API calls.
  • Batch Processing: Processing multiple items concurrently.
  • Background Tasks: Running multiple background operations.
  • Data Enrichment: Enriching multiple items in parallel.
  • File Operations: Reading/writing multiple files concurrently.
  • Image Processing: Processing images in parallel.

How it works

  1. Each incoming pulse triggers the project function.
  2. The project returns an inner sequence.
  3. The inner sequence is drained asynchronously.
  4. Values are emitted as they complete (unordered).
  5. All inners run to completion (no cancellation).

Parameters:

  • source: The Source Cell. The cell providing the input values.
  • project: The Flattening Logic. Takes an input value and returns an inner sequence.

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 flattened values.

Example

final ids = Cell.ingress<int>();

final handle = Flow.mergeMap<int, User>(
  ids.cell,
  project: (id) async => await api.getUser(id),
);

ids.emit(1); // Starts fetching
ids.emit(2); // Starts fetching (parallel)
// Results arrive in completion order

See Also:

AI Agent Guide

Layer: Flow — concurrent flattener. Not a Cell factory, not a Tissue book. When to use: parallel CAD lookups / independent I/O per trigger; results in completion order. When NOT to use: order matters (concatMap) or only the latest matters (switchMap). Live signature: Flow.mergeMap<S, T>(Cell source, {required FutureOr<Object?> Function(S value) project}) — pinned at cell_flow: 1.2.0 Do NOT confuse with: Flow.switchMap (cancels previous) and Flow.concatMap (queues inners). Guide: HowTo-Mitose-Flow.md §3.4 "Flatten (inner sequences)" (#flow-flatten) Example: example/async_map_parallel_search_demo.dart Cross-layer: flatten wraps I/O beside the decision, never inside riskOf. Minimal compilable example

final src = Cell.ingress<int>();
final h = Flow.mergeMap<int, int>(src.cell, project: (v) async => v * 2);
final o = Cell.observe(source: h.cell, effect: (Pulse p) => print(p.payload));
src.emit(1);
o.stop();

Implementation

static FlowHandle mergeMap<S, T>(
  Cell source, {
  required FutureOr<Object?> Function(S value) project,
}) {
  return MergeMap<S, T>(project).toHandle(source: source);
}