| "use strict"; |
| Object.defineProperty(exports, "__esModule", { |
| value: true |
| }); |
| Object.defineProperty(exports, "Batcher", { |
| enumerable: true, |
| get: function() { |
| return Batcher; |
| } |
| }); |
| const _detachedpromise = require("./detached-promise"); |
| class Batcher { |
| constructor(cacheKeyFn, |
| |
| |
| |
| schedulerFn = (fn)=>fn()){ |
| this.cacheKeyFn = cacheKeyFn; |
| this.schedulerFn = schedulerFn; |
| this.pending = new Map(); |
| } |
| static create(options) { |
| return new Batcher(options == null ? void 0 : options.cacheKeyFn, options == null ? void 0 : options.schedulerFn); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async batch(key, fn) { |
| const cacheKey = this.cacheKeyFn ? await this.cacheKeyFn(key) : key; |
| if (cacheKey === null) { |
| return fn(cacheKey, Promise.resolve); |
| } |
| const pending = this.pending.get(cacheKey); |
| if (pending) return pending; |
| const { promise, resolve, reject } = new _detachedpromise.DetachedPromise(); |
| this.pending.set(cacheKey, promise); |
| this.schedulerFn(async ()=>{ |
| try { |
| const result = await fn(cacheKey, resolve); |
| |
| |
| resolve(result); |
| } catch (err) { |
| reject(err); |
| } finally{ |
| this.pending.delete(cacheKey); |
| } |
| }); |
| return promise; |
| } |
| } |
|
|
| |