pubsub.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.PubSub = void 0;
  17. var events_1 = require("events");
  18. var pubsub_engine_1 = require("./pubsub-engine");
  19. var PubSub = (function (_super) {
  20. __extends(PubSub, _super);
  21. function PubSub(options) {
  22. if (options === void 0) { options = {}; }
  23. var _this = _super.call(this) || this;
  24. _this.ee = options.eventEmitter || new events_1.EventEmitter();
  25. _this.subscriptions = {};
  26. _this.subIdCounter = 0;
  27. return _this;
  28. }
  29. PubSub.prototype.publish = function (triggerName, payload) {
  30. this.ee.emit(triggerName, payload);
  31. return Promise.resolve();
  32. };
  33. PubSub.prototype.subscribe = function (triggerName, onMessage) {
  34. this.ee.addListener(triggerName, onMessage);
  35. this.subIdCounter = this.subIdCounter + 1;
  36. this.subscriptions[this.subIdCounter] = [triggerName, onMessage];
  37. return Promise.resolve(this.subIdCounter);
  38. };
  39. PubSub.prototype.unsubscribe = function (subId) {
  40. var _a = this.subscriptions[subId], triggerName = _a[0], onMessage = _a[1];
  41. delete this.subscriptions[subId];
  42. this.ee.removeListener(triggerName, onMessage);
  43. };
  44. return PubSub;
  45. }(pubsub_engine_1.PubSubEngine));
  46. exports.PubSub = PubSub;
  47. //# sourceMappingURL=pubsub.js.map