File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
examples/SampleApp/src/utils Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments