SubscriptionSet.js 1012 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * Tiny class to simplify dealing with subscription set
  5. */
  6. class SubscriptionSet {
  7. constructor() {
  8. this.set = {
  9. subscribe: {},
  10. psubscribe: {},
  11. ssubscribe: {},
  12. };
  13. }
  14. add(set, channel) {
  15. this.set[mapSet(set)][channel] = true;
  16. }
  17. del(set, channel) {
  18. delete this.set[mapSet(set)][channel];
  19. }
  20. channels(set) {
  21. return Object.keys(this.set[mapSet(set)]);
  22. }
  23. isEmpty() {
  24. return (this.channels("subscribe").length === 0 &&
  25. this.channels("psubscribe").length === 0 &&
  26. this.channels("ssubscribe").length === 0);
  27. }
  28. }
  29. exports.default = SubscriptionSet;
  30. function mapSet(set) {
  31. if (set === "unsubscribe") {
  32. return "subscribe";
  33. }
  34. if (set === "punsubscribe") {
  35. return "psubscribe";
  36. }
  37. if (set === "sunsubscribe") {
  38. return "ssubscribe";
  39. }
  40. return set;
  41. }