Skip to main content

Module batch

Module batch 

Source
Expand description

Batch update coalescing for [Observable] notifications.

When multiple Observable values are updated in rapid succession, subscribers receive a notification for each change. In render-heavy scenarios this causes redundant intermediate renders. Batch coalescing defers all notifications until the batch scope exits, then fires each unique callback at most once.

§Usage

use ftui_runtime::reactive::batch::BatchScope;

let x = Observable::new(0);
let y = Observable::new(0);

{
    let _batch = BatchScope::new();
    x.set(1);  // notification deferred
    y.set(2);  // notification deferred
    x.set(3);  // notification deferred (coalesced with first x.set)
}  // all notifications fire here, x subscribers called once with value 3

§Invariants

  1. Nested batches are supported: only the outermost scope triggers flush.
  2. Within a batch, Observable::get() always returns the latest value (values are updated immediately, only notifications are deferred).
  3. After a batch exits, all subscribers see the final state, never an intermediate state.
  4. Flush calls deferred callbacks in the order they were first enqueued.

§Failure Modes

  • Callback panics during flush: Remaining callbacks are still called. The first panic is re-raised after all callbacks have been attempted.

Structs§

BatchScope
RAII guard that begins a batch scope.

Functions§

defer_or_run
Enqueue a deferred notification to be fired when the current batch exits.
is_batching
Returns true if a batch is currently active on this thread.