Advanced

ChangeNotifier

The pub/sub system that powers reactive updates with microtask batching.

How it works

Every mutation (add, put, delete, clear) on a Store emits a change event through the ChangeNotifier. The notifier uses microtask batching — multiple rapid mutations in the same execution context result in a single "batch" notification, preventing unnecessary re-queries.

Batching example

When you perform multiple operations in a loop, the notifier batches them into a single update.

batching.ts

The batching uses queueMicrotask(), so the flush happens after all synchronous code in the current microtask completes.

Architecture

The ChangeNotifier is a map of store names to subscriber sets. When a store mutation occurs, the store name is added to a pending set. On the next microtask, all pending stores are flushed and their subscribers notified with a "batch" event.