From bf8a3d4419c66f0c5318d2a2d4f8b843e48ec3ea Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 26 May 2020 16:20:51 -0600 Subject: [PATCH] Bind to the room instead of the client Also add the other missing deregister handlers --- src/components/views/rooms/RoomTile2.tsx | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/components/views/rooms/RoomTile2.tsx b/src/components/views/rooms/RoomTile2.tsx index d9d1d39142..47ce001983 100644 --- a/src/components/views/rooms/RoomTile2.tsx +++ b/src/components/views/rooms/RoomTile2.tsx @@ -89,17 +89,18 @@ export default class RoomTile2 extends React.Component { notificationState: this.getNotificationState(), }; - // TODO: We shouldn't have to listen to every room update - // We don't have a model which works in a better way though. - MatrixClientPeg.get().on("Room.receipt", this.tryUpdateIfRoomMatches); - MatrixClientPeg.get().on("Room.timeline", this.tryUpdateIfRoomMatches); - MatrixClientPeg.get().on("Event.decrypted", this.tryUpdateIfRoomMatches); - MatrixClientPeg.get().on("Room.redaction", this.tryUpdateIfRoomMatches); + this.props.room.on("Room.receipt", this.handleRoomEventUpdate); + this.props.room.on("Room.timeline", this.handleRoomEventUpdate); + this.props.room.on("Event.decrypted", this.handleRoomEventUpdate); + this.props.room.on("Room.redaction", this.handleRoomEventUpdate); } public componentWillUnmount() { - if (MatrixClientPeg.get()) { - MatrixClientPeg.get().removeListener("Room.receipt", this.tryUpdateIfRoomMatches); + if (this.props.room) { + this.props.room.removeListener("Room.receipt", this.handleRoomEventUpdate); + this.props.room.removeListener("Room.timeline", this.handleRoomEventUpdate); + this.props.room.removeListener("Event.decrypted", this.handleRoomEventUpdate); + this.props.room.removeListener("Room.redaction", this.handleRoomEventUpdate); } } @@ -110,8 +111,10 @@ export default class RoomTile2 extends React.Component { return getEffectiveMembership(this.props.room.getMyMembership()) === EffectiveMembership.Invite; } - private tryUpdateIfRoomMatches = (event: MatrixEvent) => { + private handleRoomEventUpdate = (event: MatrixEvent) => { const roomId = event.getRoomId(); + + // Sanity check: should never happen if (roomId !== this.props.room.roomId) return; this.updateNotificationState();