UNPKG

ipfs-http-client

Version:
44 lines (39 loc) 1.25 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var nativeAbortController = require('native-abort-controller'); class SubscriptionTracker { constructor() { this._subs = new Map(); } subscribe(topic, handler, signal) { const topicSubs = this._subs.get(topic) || []; if (topicSubs.find(s => s.handler === handler)) { throw new Error(`Already subscribed to ${ topic } with this handler`); } const controller = new nativeAbortController.AbortController(); this._subs.set(topic, [{ handler, controller }].concat(topicSubs)); if (signal) { signal.addEventListener('abort', () => this.unsubscribe(topic, handler)); } return controller.signal; } unsubscribe(topic, handler) { const subs = this._subs.get(topic) || []; let unsubs; if (handler) { this._subs.set(topic, subs.filter(s => s.handler !== handler)); unsubs = subs.filter(s => s.handler === handler); } else { this._subs.set(topic, []); unsubs = subs; } if (!(this._subs.get(topic) || []).length) { this._subs.delete(topic); } unsubs.forEach(s => s.controller.abort()); } } exports.SubscriptionTracker = SubscriptionTracker;