UNPKG

batchloader

Version:

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

31 lines 1.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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); } } exports.MappedBatchLoader = MappedBatchLoader; //# sourceMappingURL=mappedbatchloader.js.map