1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.EventEmitterPubSub = void 0;
- var _events = _interopRequireDefault(require("events"));
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- const emitter = new _events.default.EventEmitter();
- class Publisher {
- constructor(emitter) {
- this.emitter = emitter;
- }
- publish(channel, message) {
- this.emitter.emit(channel, message);
- }
- }
- class Subscriber extends _events.default.EventEmitter {
- constructor(emitter) {
- super();
- this.emitter = emitter;
- this.subscriptions = new Map();
- }
- subscribe(channel) {
- const handler = message => {
- this.emit('message', channel, message);
- };
- this.subscriptions.set(channel, handler);
- this.emitter.on(channel, handler);
- }
- unsubscribe(channel) {
- if (!this.subscriptions.has(channel)) {
- return;
- }
- this.emitter.removeListener(channel, this.subscriptions.get(channel));
- this.subscriptions.delete(channel);
- }
- }
- function createPublisher() {
- return new Publisher(emitter);
- }
- function createSubscriber() {
- // createSubscriber is called once at live query server start
- // to avoid max listeners warning, we should clean up the event emitter
- // each time this function is called
- emitter.removeAllListeners();
- return new Subscriber(emitter);
- }
- const EventEmitterPubSub = exports.EventEmitterPubSub = {
- createPublisher,
- createSubscriber
- };
- //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfZXZlbnRzIiwiX2ludGVyb3BSZXF1aXJlRGVmYXVsdCIsInJlcXVpcmUiLCJlIiwiX19lc01vZHVsZSIsImRlZmF1bHQiLCJlbWl0dGVyIiwiZXZlbnRzIiwiRXZlbnRFbWl0dGVyIiwiUHVibGlzaGVyIiwiY29uc3RydWN0b3IiLCJwdWJsaXNoIiwiY2hhbm5lbCIsIm1lc3NhZ2UiLCJlbWl0IiwiU3Vic2NyaWJlciIsInN1YnNjcmlwdGlvbnMiLCJNYXAiLCJzdWJzY3JpYmUiLCJoYW5kbGVyIiwic2V0Iiwib24iLCJ1bnN1YnNjcmliZSIsImhhcyIsIn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