@@ -18,7 +18,7 @@ import {
1818 EventType ,
1919 RoomEvent as MatrixRoomEvent ,
2020} from "matrix-js-sdk" ;
21- import { BehaviorSubject , delay , type Subscription } from "rxjs" ;
21+ import { BehaviorSubject , delay } from "rxjs" ;
2222
2323import {
2424 ElementCallReactionEventType ,
@@ -28,6 +28,7 @@ import {
2828 type RaisedHandInfo ,
2929 type ReactionInfo ,
3030} from "." ;
31+ import { type ObservableScope } from "../state/ObservableScope" ;
3132
3233export const REACTION_ACTIVE_TIME_MS = 3000 ;
3334
@@ -54,12 +55,13 @@ export class ReactionsReader {
5455 */
5556 public readonly reactions$ = this . reactionsSubject$ . asObservable ( ) ;
5657
57- private readonly reactionsSub : Subscription ;
58-
59- public constructor ( private readonly rtcSession : MatrixRTCSession ) {
58+ public constructor (
59+ private readonly scope : ObservableScope ,
60+ private readonly rtcSession : MatrixRTCSession ,
61+ ) {
6062 // Hide reactions after a given time.
61- this . reactionsSub = this . reactionsSubject$
62- . pipe ( delay ( REACTION_ACTIVE_TIME_MS ) )
63+ this . reactionsSubject$
64+ . pipe ( delay ( REACTION_ACTIVE_TIME_MS ) , this . scope . bind ( ) )
6365 . subscribe ( ( reactions ) => {
6466 const date = new Date ( ) ;
6567 const nextEntries = Object . fromEntries (
@@ -71,27 +73,62 @@ export class ReactionsReader {
7173 this . reactionsSubject$ . next ( nextEntries ) ;
7274 } ) ;
7375
76+ // TODO: Convert this class to the functional reactive style and get rid of
77+ // all this manual setup and teardown for event listeners
78+
7479 this . rtcSession . room . on ( MatrixRoomEvent . Timeline , this . handleReactionEvent ) ;
80+ this . scope . onEnd ( ( ) =>
81+ this . rtcSession . room . off (
82+ MatrixRoomEvent . Timeline ,
83+ this . handleReactionEvent ,
84+ ) ,
85+ ) ;
86+
7587 this . rtcSession . room . on (
7688 MatrixRoomEvent . Redaction ,
7789 this . handleReactionEvent ,
7890 ) ;
91+ this . scope . onEnd ( ( ) =>
92+ this . rtcSession . room . off (
93+ MatrixRoomEvent . Redaction ,
94+ this . handleReactionEvent ,
95+ ) ,
96+ ) ;
97+
7998 this . rtcSession . room . client . on (
8099 MatrixEventEvent . Decrypted ,
81100 this . handleReactionEvent ,
82101 ) ;
102+ this . scope . onEnd ( ( ) =>
103+ this . rtcSession . room . client . off (
104+ MatrixEventEvent . Decrypted ,
105+ this . handleReactionEvent ,
106+ ) ,
107+ ) ;
83108
84109 // We listen for a local echo to get the real event ID, as timeline events
85110 // may still be sending.
86111 this . rtcSession . room . on (
87112 MatrixRoomEvent . LocalEchoUpdated ,
88113 this . handleReactionEvent ,
89114 ) ;
115+ this . scope . onEnd ( ( ) =>
116+ this . rtcSession . room . off (
117+ MatrixRoomEvent . LocalEchoUpdated ,
118+ this . handleReactionEvent ,
119+ ) ,
120+ ) ;
90121
91- rtcSession . on (
122+ this . rtcSession . on (
92123 MatrixRTCSessionEvent . MembershipsChanged ,
93124 this . onMembershipsChanged ,
94125 ) ;
126+ this . scope . onEnd ( ( ) =>
127+ this . rtcSession . off (
128+ MatrixRTCSessionEvent . MembershipsChanged ,
129+ this . onMembershipsChanged ,
130+ ) ,
131+ ) ;
95132
96133 // Run this once to ensure we have fetched the state from the call.
97134 this . onMembershipsChanged ( [ ] ) ;
@@ -309,31 +346,4 @@ export class ReactionsReader {
309346 this . removeRaisedHand ( targetUser ) ;
310347 }
311348 } ;
312-
313- /**
314- * Stop listening for events.
315- */
316- public destroy ( ) : void {
317- this . rtcSession . off (
318- MatrixRTCSessionEvent . MembershipsChanged ,
319- this . onMembershipsChanged ,
320- ) ;
321- this . rtcSession . room . off (
322- MatrixRoomEvent . Timeline ,
323- this . handleReactionEvent ,
324- ) ;
325- this . rtcSession . room . off (
326- MatrixRoomEvent . Redaction ,
327- this . handleReactionEvent ,
328- ) ;
329- this . rtcSession . room . client . off (
330- MatrixEventEvent . Decrypted ,
331- this . handleReactionEvent ,
332- ) ;
333- this . rtcSession . room . off (
334- MatrixRoomEvent . LocalEchoUpdated ,
335- this . handleReactionEvent ,
336- ) ;
337- this . reactionsSub . unsubscribe ( ) ;
338- }
339349}
0 commit comments