Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AGenUI AGenUI BatchScope Coalescing Order

From Leeroopedia
Knowledge Sources
Domains Performance, UI_Framework
Last Updated 2026-10-24 17:21 GMT

Overview

When using composite BatchScope (multi-target), place the consumer guard first and the producer guard last so that inner-guards flush in reverse order — ensuring the producer flushes into the still-batching consumer, yielding a single consolidated notification.

Description

The `BatchScope` RAII helper opens batch windows on multiple `BatchGuard` targets in argument order on construction and closes them in reverse argument order on destruction. This ordering is critical for correctness: when one subsystem's flush feeds another (e.g. ComponentManager flush produces `updateNode` calls that feed VirtualDOM), the producer must flush before the consumer's batch window closes, so the consumer coalesces the producer's output into a single notification.

The rule is: put the consumer first and the producer last — `BatchScope(consumer_guard, producer_guard)`. On destruction, the producer (last arg) closes first, flushing its dirty state into the consumer (first arg), which is still batching. Then the consumer closes, emitting one consolidated layout-change notification instead of N separate ones.

Usage

Use this heuristic when:

  • You are wrapping multiple subsystem operations in a composite `BatchScope`
  • One subsystem's flush output feeds another subsystem (e.g. ComponentManager → VirtualDOM)
  • You want to minimize the number of layout/render notifications during a burst of updates

Do NOT use a single-target `BatchScope` when the subsystems have no data dependency — separate single-target scopes are fine.

The Insight (Rule of Thumb)

  • Action: In `BatchScope(consumer_guard, producer_guard)`, always place the consumer (the one that should consolidate) as the first argument and the producer (the one whose flush feeds the consumer) as the last argument.
  • Value: Argument order = open order; destruction order = reverse of argument order. Producer flushes first (last arg, closed first), consumer consolidates (first arg, closed last).
  • Trade-off: None — this is a correctness rule, not an optimization. Wrong ordering causes extra flush passes or missed coalescing.
  • Compatibility: Works with any number of targets; null targets are silently skipped.

Reasoning

The `BatchScope` destructor iterates targets in reverse: `for (i = N; i-- > 0; )`. The `BatchGuard::endBatch()` at depth 1 closes inner guards in reverse order while its own depth is still 1, so any `requestFlush()` triggered by inner flushes sees `_batchDepth > 0` and defers rather than firing immediately. This means the consumer guard's batch window absorbs all the producer's flush output, and only when the consumer itself closes does the final consolidated notification fire.

From `agenui_batch_guard.h:156-168`:

Ordering contract (important for correctness):
  - On construction, targets are begun in argument order: arg #0 first,
    arg #N-1 last. So the FIRST argument is the OUTERMOST scope.
  - On destruction, targets are ended in reverse argument order: arg
    #N-1 first, arg #0 last. So the LAST argument flushes FIRST.
  - When one subsystem's flush feeds another subsystem (e.g.
    ComponentManager flush produces updateNode calls that feed
    VirtualDOM), put the *consumer* first and the *producer* last:
    BatchScope(consumer_guard, producer_guard).

The re-entrancy guard (`_flushing` flag) in `BatchGuard::requestFlush()` further ensures that flush callbacks which trigger further `requestFlush()` calls (e.g. data-binding chains) are coalesced into a pending-flush loop with a hard cap of 8 passes (`kMaxFlushPasses`), preventing infinite feedback cycles.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment