Clear timeouts on component close.

This commit is contained in:
Half-Shot
2024-11-08 09:47:05 +00:00
parent 17f2647d89
commit 63f1b7346f

View File

@@ -178,6 +178,7 @@ export const ReactionsProvider = ({
// This effect handles any *live* reaction/redactions in the room. // This effect handles any *live* reaction/redactions in the room.
useEffect(() => { useEffect(() => {
const reactionTimeouts = new Set<NodeJS.Timeout>();
const handleReactionEvent = (event: MatrixEvent): void => { const handleReactionEvent = (event: MatrixEvent): void => {
if (event.isSending()) { if (event.isSending()) {
// Skip any events that are still sending. // Skip any events that are still sending.
@@ -240,10 +241,12 @@ export const ReactionsProvider = ({
// We've still got a reaction from this user, ignore it to prevent spamming // We've still got a reaction from this user, ignore it to prevent spamming
return reactions; return reactions;
} }
setTimeout(() => { const timeout = setTimeout(() => {
// Clear the reaction after some time. // Clear the reaction after some time.
setReactions(({ [sender]: _unused, ...remaining }) => remaining); setReactions(({ [sender]: _unused, ...remaining }) => remaining);
reactionTimeouts.delete(timeout);
}, REACTION_ACTIVE_TIME_MS); }, REACTION_ACTIVE_TIME_MS);
reactionTimeouts.add(timeout);
return { return {
...reactions, ...reactions,
[sender]: reaction, [sender]: reaction,
@@ -297,6 +300,7 @@ export const ReactionsProvider = ({
room.off(MatrixRoomEvent.Timeline, handleReactionEvent); room.off(MatrixRoomEvent.Timeline, handleReactionEvent);
room.off(MatrixRoomEvent.Redaction, handleReactionEvent); room.off(MatrixRoomEvent.Redaction, handleReactionEvent);
room.off(MatrixRoomEvent.LocalEchoUpdated, handleReactionEvent); room.off(MatrixRoomEvent.LocalEchoUpdated, handleReactionEvent);
reactionTimeouts.forEach((t) => clearTimeout(t));
}; };
}, [room, addRaisedHand, removeRaisedHand, memberships, raisedHands]); }, [room, addRaisedHand, removeRaisedHand, memberships, raisedHands]);