SubscriptionSet.js 859 B

123456789101112131415161718192021222324252627282930313233343536
  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. };
  12. }
  13. add(set, channel) {
  14. this.set[mapSet(set)][channel] = true;
  15. }
  16. del(set, channel) {
  17. delete this.set[mapSet(set)][channel];
  18. }
  19. channels(set) {
  20. return Object.keys(this.set[mapSet(set)]);
  21. }
  22. isEmpty() {
  23. return (this.channels("subscribe").length === 0 &&
  24. this.channels("psubscribe").length === 0);
  25. }
  26. }
  27. exports.default = SubscriptionSet;
  28. function mapSet(set) {
  29. if (set === "unsubscribe") {
  30. return "subscribe";
  31. }
  32. if (set === "punsubscribe") {
  33. return "psubscribe";
  34. }
  35. return set;
  36. }