subscribable.js 643 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. class Subscribable {
  4. constructor() {
  5. this.listeners = new Set();
  6. this.subscribe = this.subscribe.bind(this);
  7. }
  8. subscribe(listener) {
  9. const identity = {
  10. listener
  11. };
  12. this.listeners.add(identity);
  13. this.onSubscribe();
  14. return () => {
  15. this.listeners.delete(identity);
  16. this.onUnsubscribe();
  17. };
  18. }
  19. hasListeners() {
  20. return this.listeners.size > 0;
  21. }
  22. onSubscribe() {// Do nothing
  23. }
  24. onUnsubscribe() {// Do nothing
  25. }
  26. }
  27. exports.Subscribable = Subscribable;
  28. //# sourceMappingURL=subscribable.js.map