Open History to see the last commit
1type Listener = () => void
2β
3export class Subscribable<TListener extends Function = Listener> {
4 protected listeners: TListener[]
5β
6 constructor() {
7 this.listeners = []
8 }
9β
10 subscribe(listener?: TListener): () => void {
11 const callback = listener || (() => undefined)
12β
13 this.listeners.push(callback as TListener)
14β
15 this.onSubscribe()
16β
17 return () => {
18 this.listeners = this.listeners.filter(x => x !== callback)
19 this.onUnsubscribe()
20 }
21 }
22β
23 hasListeners(): boolean {
24 return this.listeners.length > 0
25 }
26β
27 protected onSubscribe(): void {
28 // Do nothing
29 }
30β
31 protected onUnsubscribe(): void {
32 // Do nothing
33 }
34}