Skip to content

Multiplexing

Reuse loader instances by hash to deduplicate concurrent or repeated loads. Effective only when a hash is provided (or auto-generated from the loader function).

share: share one inflight load

typescript
// "share": loaders with the same hash share one inflight load
const l1 = new AsyncLoader(fn, { hash: "req", multiplex: "share" });
const l2 = new AsyncLoader(fn, { hash: "req", multiplex: "share" });
console.log(l1 === l2); // true — same instance, one underlying call

restart: abort and reload

typescript
// "restart": a new loader with the same hash aborts the inflight load and reloads
const a = new AsyncLoader(fnA, { hash: "req", multiplex: "restart" });
const b = new AsyncLoader(fnB, { hash: "req", multiplex: "restart" });
// a's inflight load is aborted, reloaded with fnA (fnB is ignored)

Three modes

multiplexBehavior
"off" (default)Each instance is independent
"restart"When hitting an inflight instance of the same hash, abort it and reload with the first instance's loaders array
"share"Fully share the inflight load and result of the first instance on a hash match

Same behavior when pending or unmatched

Both modes (restart / share) behave the same when the matched instance is pending (not yet loading) or there is no match.

Auto-generated hash

When not off and no hash is given, a hash is auto-generated from the loaders. In tests, use AsyncLoader.clearLoaderCache() to clear the instance cache for case isolation.

Released under the MIT License.