From d2877191ac6698b0780c58d039d0f20b08f6ccfb Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 17 Oct 2022 17:30:02 +0100 Subject: [PATCH 1/3] Listen for and update the notification state when they change --- src/stores/notifications/RoomNotificationState.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/stores/notifications/RoomNotificationState.ts b/src/stores/notifications/RoomNotificationState.ts index c4c803483df..8254f83b780 100644 --- a/src/stores/notifications/RoomNotificationState.ts +++ b/src/stores/notifications/RoomNotificationState.ts @@ -37,6 +37,7 @@ export class RoomNotificationState extends NotificationState implements IDestroy this.room.on(RoomEvent.Redaction, this.handleRoomEventUpdate); this.room.on(RoomEvent.MyMembership, this.handleMembershipUpdate); this.room.on(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated); + this.room.on(RoomEvent.UnreadNotifications, this.handleNotificationCountUpdate); if (threadsState) { threadsState.on(NotificationStateEvents.Update, this.handleThreadsUpdate); } @@ -83,6 +84,10 @@ export class RoomNotificationState extends NotificationState implements IDestroy this.updateNotificationState(); }; + private handleNotificationCountUpdate = () => { + this.updateNotificationState(); + }; + private onEventDecrypted = (event: MatrixEvent) => { if (event.getRoomId() !== this.room.roomId) return; // ignore - not for us or notifications timeline From e6a53e9c5957792cd3e3bb144f51236bbe48a049 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 18 Oct 2022 10:48:37 +0100 Subject: [PATCH 2/3] Remove unnecessary listeners: justify each listener left remaining --- src/stores/notifications/RoomNotificationState.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/stores/notifications/RoomNotificationState.ts b/src/stores/notifications/RoomNotificationState.ts index 8254f83b780..675621fb677 100644 --- a/src/stores/notifications/RoomNotificationState.ts +++ b/src/stores/notifications/RoomNotificationState.ts @@ -32,17 +32,15 @@ import { ThreadsRoomNotificationState } from "./ThreadsRoomNotificationState"; export class RoomNotificationState extends NotificationState implements IDestroyable { constructor(public readonly room: Room, private readonly threadsState?: ThreadsRoomNotificationState) { super(); - this.room.on(RoomEvent.Receipt, this.handleReadReceipt); - this.room.on(RoomEvent.Timeline, this.handleRoomEventUpdate); - this.room.on(RoomEvent.Redaction, this.handleRoomEventUpdate); - this.room.on(RoomEvent.MyMembership, this.handleMembershipUpdate); - this.room.on(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated); - this.room.on(RoomEvent.UnreadNotifications, this.handleNotificationCountUpdate); + this.room.on(RoomEvent.Receipt, this.handleReadReceipt); // for unread indicators + this.room.on(RoomEvent.MyMembership, this.handleMembershipUpdate); // for redness on invites + this.room.on(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated); // for redness on unsent messages + this.room.on(RoomEvent.UnreadNotifications, this.handleNotificationCountUpdate); // for server-sent counts if (threadsState) { threadsState.on(NotificationStateEvents.Update, this.handleThreadsUpdate); } - MatrixClientPeg.get().on(MatrixEventEvent.Decrypted, this.onEventDecrypted); - MatrixClientPeg.get().on(ClientEvent.AccountData, this.handleAccountDataUpdate); + MatrixClientPeg.get().on(MatrixEventEvent.Decrypted, this.onEventDecrypted); // for local count calculation + MatrixClientPeg.get().on(ClientEvent.AccountData, this.handleAccountDataUpdate); // for push rules this.updateNotificationState(); } From b95b21d43bc89fede3fb9c1f2193ea74026413ff Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 18 Oct 2022 10:50:34 +0100 Subject: [PATCH 3/3] Update removeListener too --- src/stores/notifications/RoomNotificationState.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/stores/notifications/RoomNotificationState.ts b/src/stores/notifications/RoomNotificationState.ts index 675621fb677..9c64b7ec424 100644 --- a/src/stores/notifications/RoomNotificationState.ts +++ b/src/stores/notifications/RoomNotificationState.ts @@ -51,10 +51,9 @@ export class RoomNotificationState extends NotificationState implements IDestroy public destroy(): void { super.destroy(); this.room.removeListener(RoomEvent.Receipt, this.handleReadReceipt); - this.room.removeListener(RoomEvent.Timeline, this.handleRoomEventUpdate); - this.room.removeListener(RoomEvent.Redaction, this.handleRoomEventUpdate); this.room.removeListener(RoomEvent.MyMembership, this.handleMembershipUpdate); this.room.removeListener(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated); + this.room.removeListener(RoomEvent.UnreadNotifications, this.handleNotificationCountUpdate); if (this.threadsState) { this.threadsState.removeListener(NotificationStateEvents.Update, this.handleThreadsUpdate); } @@ -92,12 +91,6 @@ export class RoomNotificationState extends NotificationState implements IDestroy this.updateNotificationState(); }; - private handleRoomEventUpdate = (event: MatrixEvent, room: Room | null) => { - if (room?.roomId !== this.room.roomId) return; // ignore - not for us or notifications timeline - - this.updateNotificationState(); - }; - private handleAccountDataUpdate = (ev: MatrixEvent) => { if (ev.getType() === "m.push_rules") { this.updateNotificationState();