batchloader
Version:
BatchLoader is a utility for data fetching layer to reduce requests via batching written in TypeScript. Inspired by Facebook's DataLoader
34 lines • 1.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var MappedBatchLoader = (function () {
function MappedBatchLoader(loader, mapFn) {
this.loader = loader;
this.mapFn = mapFn;
}
MappedBatchLoader.prototype.load = function (key) {
var _this = this;
return this.loader.load(key).then(function (value) { return _this.mapFn(value, key); });
};
MappedBatchLoader.prototype.loadMany = function (keys) {
var _this = this;
return this.loader.loadMany(keys).then(function (values) {
var hasPromise = false;
var results = [];
var len = values.length;
for (var i = 0; i < len; i += 1) {
var res = _this.mapFn(values[i], keys[i]);
results.push(res);
hasPromise =
hasPromise ||
(res != null && typeof res.then === 'function');
}
return hasPromise ? Promise.all(results) : results;
});
};
MappedBatchLoader.prototype.mapLoader = function (mapFn) {
return new MappedBatchLoader(this, mapFn);
};
return MappedBatchLoader;
}());
exports.MappedBatchLoader = MappedBatchLoader;
//# sourceMappingURL=mappedbatchloader.js.map