From 997c5b3d35ce9b9862b33674eb932ba5cc9fcc54 Mon Sep 17 00:00:00 2001 From: eternal-flame-AD Date: Sun, 10 Nov 2024 00:09:08 -0600 Subject: [PATCH] fix(frontend): eventListener leak Signed-off-by: eternal-flame-AD --- packages/frontend/src/stream.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/frontend/src/stream.ts b/packages/frontend/src/stream.ts index e23937e6b6..57fae49d4b 100644 --- a/packages/frontend/src/stream.ts +++ b/packages/frontend/src/stream.ts @@ -68,14 +68,20 @@ export function useStream(): Misskey.IStream { if (timeoutHeartBeat) window.clearTimeout(timeoutHeartBeat); timeoutHeartBeat = window.setTimeout(heartbeat, HEART_BEAT_INTERVAL); - // send heartbeat right now when last send time is over HEART_BEAT_INTERVAL - document.addEventListener('visibilitychange', () => { + const target = () => { if ( !stream || document.visibilityState !== 'visible' || Date.now() - lastHeartbeatCall < HEART_BEAT_INTERVAL ) return; heartbeat(); + }; + + // send heartbeat right now when last send time is over HEART_BEAT_INTERVAL + document.addEventListener('visibilitychange', target); + + stream.on('_disconnected_', () => { + document.removeEventListener('visibilitychange', target); }); return stream;