Listen for and update the notification state when they change (#9438)

* Listen for and update the notification state when they change

* Remove unnecessary listeners: justify each listener left remaining

* Update removeListener too
This commit is contained in:
kegsay 2022-10-18 13:44:45 +01:00 committed by GitHub
parent 0ef8c80815
commit 67dbb36026
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -32,16 +32,15 @@ import { ThreadsRoomNotificationState } from "./ThreadsRoomNotificationState";
export class RoomNotificationState extends NotificationState implements IDestroyable { export class RoomNotificationState extends NotificationState implements IDestroyable {
constructor(public readonly room: Room, private readonly threadsState?: ThreadsRoomNotificationState) { constructor(public readonly room: Room, private readonly threadsState?: ThreadsRoomNotificationState) {
super(); super();
this.room.on(RoomEvent.Receipt, this.handleReadReceipt); this.room.on(RoomEvent.Receipt, this.handleReadReceipt); // for unread indicators
this.room.on(RoomEvent.Timeline, this.handleRoomEventUpdate); this.room.on(RoomEvent.MyMembership, this.handleMembershipUpdate); // for redness on invites
this.room.on(RoomEvent.Redaction, this.handleRoomEventUpdate); this.room.on(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated); // for redness on unsent messages
this.room.on(RoomEvent.MyMembership, this.handleMembershipUpdate); this.room.on(RoomEvent.UnreadNotifications, this.handleNotificationCountUpdate); // for server-sent counts
this.room.on(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated);
if (threadsState) { if (threadsState) {
threadsState.on(NotificationStateEvents.Update, this.handleThreadsUpdate); threadsState.on(NotificationStateEvents.Update, this.handleThreadsUpdate);
} }
MatrixClientPeg.get().on(MatrixEventEvent.Decrypted, this.onEventDecrypted); MatrixClientPeg.get().on(MatrixEventEvent.Decrypted, this.onEventDecrypted); // for local count calculation
MatrixClientPeg.get().on(ClientEvent.AccountData, this.handleAccountDataUpdate); MatrixClientPeg.get().on(ClientEvent.AccountData, this.handleAccountDataUpdate); // for push rules
this.updateNotificationState(); this.updateNotificationState();
} }
@ -52,10 +51,9 @@ export class RoomNotificationState extends NotificationState implements IDestroy
public destroy(): void { public destroy(): void {
super.destroy(); super.destroy();
this.room.removeListener(RoomEvent.Receipt, this.handleReadReceipt); 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.MyMembership, this.handleMembershipUpdate);
this.room.removeListener(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated); this.room.removeListener(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated);
this.room.removeListener(RoomEvent.UnreadNotifications, this.handleNotificationCountUpdate);
if (this.threadsState) { if (this.threadsState) {
this.threadsState.removeListener(NotificationStateEvents.Update, this.handleThreadsUpdate); this.threadsState.removeListener(NotificationStateEvents.Update, this.handleThreadsUpdate);
} }
@ -83,14 +81,12 @@ export class RoomNotificationState extends NotificationState implements IDestroy
this.updateNotificationState(); this.updateNotificationState();
}; };
private onEventDecrypted = (event: MatrixEvent) => { private handleNotificationCountUpdate = () => {
if (event.getRoomId() !== this.room.roomId) return; // ignore - not for us or notifications timeline
this.updateNotificationState(); this.updateNotificationState();
}; };
private handleRoomEventUpdate = (event: MatrixEvent, room: Room | null) => { private onEventDecrypted = (event: MatrixEvent) => {
if (room?.roomId !== this.room.roomId) return; // ignore - not for us or notifications timeline if (event.getRoomId() !== this.room.roomId) return; // ignore - not for us or notifications timeline
this.updateNotificationState(); this.updateNotificationState();
}; };