1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-05 02:08:14 +09:00

feat: send heartbeat right after visibility changed to 'visible' (#13581)

This commit is contained in:
BackRunner 2024-03-17 17:47:29 +08:00 committed by GitHub
parent 282cfae1b2
commit dcfab918e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,7 +8,12 @@ import { markRaw } from 'vue';
import { $i } from '@/account.js'; import { $i } from '@/account.js';
import { wsOrigin } from '@/config.js'; import { wsOrigin } from '@/config.js';
// heart beat interval in ms
const HEART_BEAT_INTERVAL = 1000 * 60;
let stream: Misskey.Stream | null = null; let stream: Misskey.Stream | null = null;
let timeoutHeartBeat: ReturnType<typeof setTimeout> | null = null;
let lastHeartbeatCall = 0;
export function useStream(): Misskey.Stream { export function useStream(): Misskey.Stream {
if (stream) return stream; if (stream) return stream;
@ -17,7 +22,18 @@ export function useStream(): Misskey.Stream {
token: $i.token, token: $i.token,
} : null)); } : null));
window.setTimeout(heartbeat, 1000 * 60); 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', () => {
if (
!stream
|| document.visibilityState !== 'visible'
|| Date.now() - lastHeartbeatCall < HEART_BEAT_INTERVAL
) return;
heartbeat();
});
return stream; return stream;
} }
@ -26,5 +42,7 @@ function heartbeat(): void {
if (stream != null && document.visibilityState === 'visible') { if (stream != null && document.visibilityState === 'visible') {
stream.heartbeat(); stream.heartbeat();
} }
window.setTimeout(heartbeat, 1000 * 60); lastHeartbeatCall = Date.now();
if (timeoutHeartBeat) window.clearTimeout(timeoutHeartBeat);
timeoutHeartBeat = window.setTimeout(heartbeat, HEART_BEAT_INTERVAL);
} }