UNPKG

batchloader

Version:

BatchLoader is a utility for data fetching layer to reduce requests via batching written in TypeScript. Inspired by Facebook's DataLoader

28 lines 920 B
export class MappedBatchLoader { constructor(loader, mapFn) { this.loader = loader; this.mapFn = mapFn; } load(key) { return this.loader.load(key).then((value) => this.mapFn(value, key)); } loadMany(keys) { return this.loader.loadMany(keys).then((values) => { let hasPromise = false; const results = []; const len = values.length; for (let i = 0; i < len; i += 1) { const res = this.mapFn(values[i], keys[i]); results.push(res); hasPromise = hasPromise || (res != null && typeof res.then === 'function'); } return hasPromise ? Promise.all(results) : results; }); } mapLoader(mapFn) { return new MappedBatchLoader(this, mapFn); } } //# sourceMappingURL=mappedbatchloader.js.map