UNPKG

1.3 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var events_1 = require("events");
4var event_emitter_to_async_iterator_1 = require("./event-emitter-to-async-iterator");
5var PubSub = (function () {
6 function PubSub() {
7 this.ee = new events_1.EventEmitter();
8 this.subscriptions = {};
9 this.subIdCounter = 0;
10 }
11 PubSub.prototype.publish = function (triggerName, payload) {
12 this.ee.emit(triggerName, payload);
13 return true;
14 };
15 PubSub.prototype.subscribe = function (triggerName, onMessage) {
16 this.ee.addListener(triggerName, onMessage);
17 this.subIdCounter = this.subIdCounter + 1;
18 this.subscriptions[this.subIdCounter] = [triggerName, onMessage];
19 return Promise.resolve(this.subIdCounter);
20 };
21 PubSub.prototype.unsubscribe = function (subId) {
22 var _a = this.subscriptions[subId], triggerName = _a[0], onMessage = _a[1];
23 delete this.subscriptions[subId];
24 this.ee.removeListener(triggerName, onMessage);
25 };
26 PubSub.prototype.asyncIterator = function (triggers) {
27 return event_emitter_to_async_iterator_1.eventEmitterAsyncIterator(this.ee, triggers);
28 };
29 return PubSub;
30}());
31exports.PubSub = PubSub;
32//# sourceMappingURL=pubsub.js.map
\No newline at end of file