Skip to content

Batcher

Batcher<K, E, V> = object

Defined in: Core/Batcher.ts:27

Groups nearby key lookups into bulk requests and returns an outcome for each caller.

Duplicate keys share a lookup while they are queued. Results are not cached, so a lookup after dispatch can fetch the same key again, even if the previous request is still running.

const userBatcher = Batcher.make(
  (ids: readonly string[]) => userStore.findByIds(ids),
  { maxBatchSize: 100 },
);

// The outcome reports a found user, a missing key, or a fetch error.
const outcome = await userBatcher.load("u_1");

K

E

V

readonly flush: () => Deferred<void>

Defined in: Core/Batcher.ts:65

Dispatches queued keys without waiting for the scheduled microtask or timer, then waits for queued and in-flight batches to finish. Work queued while waiting also delays completion.

Primarily used in unit tests or before transaction boundaries to guarantee deterministic batch execution.

Deferred<void>

Batcher.load to enqueue individual lookups.


readonly load: {(key): Deferred<Outcome<E, V, K>>; (key, signal): Deferred<Outcome<Aborted | E, V, K>>; }

Defined in: Core/Batcher.ts:36

Queues a key and immediately returns a Deferred of its lookup outcome. An individual caller can cancel without affecting other callers in its batch.

(key): Deferred<Outcome<E, V, K>>

K

Deferred<Outcome<E, V, K>>

(key, signal): Deferred<Outcome<Aborted | E, V, K>>

K

AbortSignal

Deferred<Outcome<Aborted | E, V, K>>

key

The key to look up.

signal

Optional AbortSignal to cancel this individual caller’s lookup.

Batcher.loadMany to coalesce multiple keys in a single call.


readonly loadMany: {(keys): Deferred<readonly Outcome<E, V, K>[]>; (keys, signal): Deferred<readonly Outcome<Aborted | E, V, K>[]>; }

Defined in: Core/Batcher.ts:51

Queues multiple keys and returns their outcomes in input order.

Does not short-circuit: preserves discrete per-key outcomes (Ok, Missing, or Err) for each requested key.

(keys): Deferred<readonly Outcome<E, V, K>[]>

readonly K[]

Deferred<readonly Outcome<E, V, K>[]>

(keys, signal): Deferred<readonly Outcome<Aborted | E, V, K>[]>

readonly K[]

AbortSignal

Deferred<readonly Outcome<Aborted | E, V, K>[]>

keys

The collection of keys to look up.

signal

Optional AbortSignal to cancel this caller’s lookup batch.

Batcher.load to look up an individual key.