UNPKG

@nerjs/batchloader

Version:

`BatchLoader` is a tool for batching data requests with support for deduplication, caching, and parallel task management. It is designed to enhance flexibility and performance in scenarios requiring asynchronous data processing. This module was inspired b

57 lines 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BatchLoader = void 0; const batch_aggregator_1 = require("../batch-aggregator/batch-aggregator"); const deduplicator_1 = require("../deduplicator/deduplicator"); const cache_adapter_1 = require("./cache-adapter"); const prepareOptions = (options) => ({ getKey: (query) => `${query}`, timeoutMs: 60_000, unrefTimeouts: false, concurrencyLimit: Infinity, maxBatchSize: 1000, batchTimeMs: 50, maxWaitingTimeMs: 60_000, ...options, }); class BatchLoader { constructor(batchLoaderFn, options) { this.deduplicatorRunner = async (query, signal) => { const key = this.getKey(query); const cached = await this.cache.get(key); if (signal.aborted) throw signal.reason; if (cached !== undefined) return cached; this.deduplicator.restartTimeout(query); const loaded = await this.aggregator.load(query); this.deduplicator.restartTimeout(query); await this.cache.set(key, loaded); return loaded; }; const { cache, getKey, timeoutMs, unrefTimeouts, batchTimeMs, concurrencyLimit, maxBatchSize, maxWaitingTimeMs } = prepareOptions(options); this.getKey = getKey; this.cache = new cache_adapter_1.CacheAdapter(cache); this.deduplicator = new deduplicator_1.Deduplicator(this.deduplicatorRunner, { getKey, timeoutMs: timeoutMs + batchTimeMs, unrefTimeouts: !!unrefTimeouts, }); this.aggregator = new batch_aggregator_1.BatchAggregator(batchLoaderFn, { timeoutMs, batchTimeMs, concurrencyLimit, maxBatchSize, maxWaitingTimeMs }); } load(query) { return this.deduplicator.call(query); } async resetCache(query) { await this.cache.delete(this.getKey(query)); } clear() { this.deduplicator.clear(); this.aggregator.clear(); } async flush() { await this.cache.clear(); } } exports.BatchLoader = BatchLoader; //# sourceMappingURL=batch-loader.js.map