PlayerBatchLoader class
Coalesces single-id player lookups into one batch request per frame.
The batch endpoint players?ids= is the decided alternative to
denormalising identity onto game rows, but the app reads identity one id at
a time through a per-id cache (PlayerInfoCache): a lobby of N games, a
roster of N seats. Left alone that turns one screen into N requests to a
batch endpoint, the exact N+1 the endpoint exists to avoid.
This sits between the cache and the repository and turns that back into one request. load records the id and returns a future; a short timer then fires, fetches every id collected in that window in a single call, and completes each waiter.
The window is a few milliseconds rather than zero on purpose. A zero-delay
timer would coalesce a build only because PlayerInfoCache.build happens
to call load synchronously, before its first await, is an invisible
coupling that a later refactor (an await slipped in ahead of the load)
would break silently, degrading back to one request per id with no error.
A small real window makes coalescing robust regardless of how the callers
are scheduled, and the cost is imperceptible: the persisted cache already
paints identity in ~5 ms, so the network refresh arriving a frame later is
invisible. A later frame (scrolling reveals more cards) opens a fresh
window: one request per burst, not one per player.
The cache above already dedupes by id and holds in-flight futures, so this deliberately keeps no in-flight bookkeeping of its own.
Constructors
-
PlayerBatchLoader(Future<
List< _fetch(List<Player> >String> ids), {Duration window = const Duration(milliseconds: 8)})
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
dispose(
) → void -
Cancels a pending flush. Wire to the owning provider's
ref.onDisposeso a loader replaced mid-window (its repository rebuilt) does not fire a timer that completes futures nothing awaits. A no-op when idle. -
load(
String id) → Future< Player> - Resolves one player, batched with every other load in the same window.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited