Open History to see the last commit
1type Listener = () => void
2β
3export class Subscribable<TListener extends Function = Listener> {
4 protected listeners: Set<TListener>
5β
6 constructor() {
7 this.listeners = new Set()
8 this.subscribe = this.subscribe.bind(this)
9 }
10β
11 subscribe(listener: TListener): () => void {
12 this.listeners.add(listener)
13β
14 this.onSubscribe()
15β
16 return () => {
17 this.listeners.delete(listener)
18 this.onUnsubscribe()
19 }
20 }
21β
22 hasListeners(): boolean {
23 return this.listeners.size > 0
24 }
25β
26 protected onSubscribe(): void {
27 // Do nothing
28 }
29β
30 protected onUnsubscribe(): void {
31 // Do nothing
32 }
33}