UNPKG

nestjs-resilience

Version:

A module for improving the reliability and fault-tolerance of your NestJS applications

37 lines (36 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DedupeStrategy = void 0; const base_strategy_1 = require("./base.strategy"); const rxjs_1 = require("rxjs"); class DedupeStrategy extends base_strategy_1.Strategy { constructor(options) { super(Object.assign(Object.assign({}, DedupeStrategy.DEFAULT_OPTIONS), options)); this.activeCommands = new Map(); } process(observable, command, ...args) { const key = this.options.keyFn(args, command); if (this.activeCommands.has(key)) { return this.activeCommands.get(key).asObservable(); } const result = observable; const subject = new rxjs_1.Subject(); this.activeCommands.set(key, subject); return result.pipe((0, rxjs_1.tap)({ next: res => { subject.next(res); subject.complete(); this.activeCommands.delete(key); }, error: err => { subject.error(err); subject.complete(); this.activeCommands.delete(key); } })); } } exports.DedupeStrategy = DedupeStrategy; DedupeStrategy.DEFAULT_OPTIONS = { keyFn: (args, command) => [DedupeStrategy.name, command, JSON.stringify(args)].join('/') };