Skip to content

Commit 8636dd7

Browse files
committed
fix: improve draft manager
1 parent f7e3213 commit 8636dd7

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Unsubscribe } from 'stream-chat';
2+
3+
/**
4+
* @private
5+
* Class to use as a template for subscribable entities.
6+
*/
7+
export abstract class WithSubscriptions {
8+
private unsubscribeFunctions: Set<Unsubscribe> = new Set();
9+
/**
10+
* Workaround for the missing TS keyword - ensures that inheritants
11+
* overriding `unregisterSubscriptions` call the base method and return
12+
* its unique symbol value.
13+
*/
14+
private static symbol = Symbol(WithSubscriptions.name);
15+
16+
public abstract registerSubscriptions(): void;
17+
18+
/**
19+
* Returns a boolean, provides information of whether `registerSubscriptions`
20+
* method has already been called for this instance.
21+
*/
22+
public get hasSubscriptions() {
23+
return this.unsubscribeFunctions.size > 0;
24+
}
25+
26+
public addUnsubscribeFunction(unsubscribeFunction: Unsubscribe) {
27+
this.unsubscribeFunctions.add(unsubscribeFunction);
28+
}
29+
30+
/**
31+
* If you re-declare `unregisterSubscriptions` method within your class
32+
* make sure to run the original too.
33+
*
34+
* @example
35+
* ```ts
36+
* class T extends WithSubscriptions {
37+
* ...
38+
* public unregisterSubscriptions = () => {
39+
* this.customThing();
40+
* return super.unregisterSubscriptions();
41+
* }
42+
* }
43+
* ```
44+
*/
45+
public unregisterSubscriptions(): typeof WithSubscriptions.symbol {
46+
this.unsubscribeFunctions.forEach((unsubscribe) => unsubscribe());
47+
this.unsubscribeFunctions.clear();
48+
49+
return WithSubscriptions.symbol;
50+
}
51+
}

0 commit comments

Comments
 (0)