CommandPool — pre-allocates Command objects in a circular buffer to
eliminate garbage collection pauses during dispatch bursts. When the
pool is exhausted, it wraps around and reuses the oldest slot.
Important: The pool is a standalone utility — bus.dispatch() still
creates its own Command internally and stamps meta (ts, id, correlationId).
Pooled commands do NOT have meta set. Use pool.acquire() for the action/target/payload,
then pass those values to bus.dispatch(cmd.action, cmd.target, cmd.payload).
The bus will create its own internal command with proper metadata.
Thread-safety note: single-threaded JS means no locking required.
Example
constpool = createCommandPool(64); constcmd = pool.acquire('cartAdd', cart, { id:1 }); bus.dispatch(cmd.action, cmd.target, cmd.payload); // bus stamps its own meta
pool.stats(); // { size: 64, acquired: 1, cursor: 1 } pool.reset(); // Reset cursor and clear all slots
CommandPool — pre-allocates Command objects in a circular buffer to eliminate garbage collection pauses during dispatch bursts. When the pool is exhausted, it wraps around and reuses the oldest slot.
Important: The pool is a standalone utility —
bus.dispatch()still creates its own Command internally and stampsmeta(ts, id, correlationId). Pooled commands do NOT havemetaset. Usepool.acquire()for the action/target/payload, then pass those values tobus.dispatch(cmd.action, cmd.target, cmd.payload). The bus will create its own internal command with proper metadata.Thread-safety note: single-threaded JS means no locking required.
Example