subscribable.mjs 553 B

1234567891011121314151617181920212223242526272829303132
  1. class Subscribable {
  2. constructor() {
  3. this.listeners = new Set();
  4. this.subscribe = this.subscribe.bind(this);
  5. }
  6. subscribe(listener) {
  7. const identity = {
  8. listener
  9. };
  10. this.listeners.add(identity);
  11. this.onSubscribe();
  12. return () => {
  13. this.listeners.delete(identity);
  14. this.onUnsubscribe();
  15. };
  16. }
  17. hasListeners() {
  18. return this.listeners.size > 0;
  19. }
  20. onSubscribe() {// Do nothing
  21. }
  22. onUnsubscribe() {// Do nothing
  23. }
  24. }
  25. export { Subscribable };
  26. //# sourceMappingURL=subscribable.mjs.map