Skip to content

实例复用(multiplex)

hash 复用加载器实例,对并发或重复加载去重。仅在提供了 hash(或基于 loader 函数自动生成)时生效。

share:共享同一次进行中加载

typescript
// "share":相同 hash 的加载器共享同一次进行中加载
const l1 = new AsyncLoader(fn, { hash: "req", multiplex: "share" });
const l2 = new AsyncLoader(fn, { hash: "req", multiplex: "share" });
console.log(l1 === l2); // true — 同一实例,底层只调用一次

restart:中止并重新加载

typescript
// "restart":相同 hash 的新加载器会中止进行中加载并重新加载
const a = new AsyncLoader(fnA, { hash: "req", multiplex: "restart" });
const b = new AsyncLoader(fnB, { hash: "req", multiplex: "restart" });
// a 的进行中加载被中止,以 fnA 重新加载(fnB 被忽略)

三种模式

multiplex行为
"off"(默认)各实例相互独立
"restart"命中同 hash正在进行中的实例时,中止它并以首个实例的 loaders 数组重新加载
"share"命中同 hash 时完全共享进行中加载与结果

pending 或未命中时行为一致

两种模式(restart / share)在命中的实例处于 pending(尚未加载)或未命中时,行为是一致的。

hash 自动生成

off 且未提供 hash 时,会基于 loaders 自动生成 hash。测试中可用 AsyncLoader.clearLoaderCache() 清空实例缓存以隔离用例。

基于 MIT 许可协议发布