diff --git a/src/components/views/rooms/NotificationBadge.tsx b/src/components/views/rooms/NotificationBadge.tsx index 65a56691bf..af5a84ed92 100644 --- a/src/components/views/rooms/NotificationBadge.tsx +++ b/src/components/views/rooms/NotificationBadge.tsx @@ -205,7 +205,7 @@ export class RoomNotificationState extends EventEmitter implements IDestroyable } } -export class ListNotificationState extends EventEmitter { +export class ListNotificationState extends EventEmitter implements IDestroyable { private _count: number; private _color: NotificationColor; private rooms: Room[] = []; @@ -237,6 +237,7 @@ export class ListNotificationState extends EventEmitter { const oldRooms = this.rooms; const diff = arrayDiff(oldRooms, rooms); + this.rooms = rooms; for (const oldRoom of diff.removed) { const state = this.states[oldRoom.roomId]; delete this.states[oldRoom.roomId]; @@ -246,12 +247,24 @@ export class ListNotificationState extends EventEmitter { for (const newRoom of diff.added) { const state = new RoomNotificationState(newRoom); state.on(NOTIFICATION_STATE_UPDATE, this.onRoomNotificationStateUpdate); + if (this.states[newRoom.roomId]) { + // "Should never happen" disclaimer. + console.warn("Overwriting notification state for room:", newRoom.roomId); + this.states[newRoom.roomId].destroy(); + } this.states[newRoom.roomId] = state; } this.calculateTotalState(); } + public destroy() { + for (const state of Object.values(this.states)) { + state.destroy(); + } + this.states = {}; + } + private onRoomNotificationStateUpdate = () => { this.calculateTotalState(); }; diff --git a/src/components/views/rooms/RoomSublist2.tsx b/src/components/views/rooms/RoomSublist2.tsx index 0df9c2ad73..2282a03757 100644 --- a/src/components/views/rooms/RoomSublist2.tsx +++ b/src/components/views/rooms/RoomSublist2.tsx @@ -84,6 +84,10 @@ export default class RoomSublist2 extends React.Component { this.state.notificationState.setRooms(this.props.rooms); } + public componentWillUnmount() { + this.state.notificationState.destroy(); + } + private onAddRoom = (e) => { e.stopPropagation(); if (this.props.onAddRoom) this.props.onAddRoom();