import { RequestCollector } from "./RequestCollector";
export class CollectorRegistry {
private readonly collectors = new Map>();
constructor(
private readonly keyOf: (input: Input) => string,
private readonly factory: (input: Input) => RequestCollector
) {}
get(input: Input) {
const key = this.keyOf(input);
let collector = this.collectors.get(key);
if (!collector) {
collector = this.factory(input);
this.collectors.set(key, collector);
}
return collector;
}
delete(input: Input) {
const key = this.keyOf(input);
this.collectors.get(key)?.dispose();
this.collectors.delete(key);
}
disposeAll() {
for (const collector of this.collectors.values()) {
collector.dispose();
}
this.collectors.clear();
}
}