This repository has been archived on 2025-01-27. You can view files and clone it, but cannot push or open issues or pull requests.
puyoskey-firefish/packages/client/src/stream.ts
naskya d096da02e6 🎉 First Commit
release: v20240729

Co-authored-by: Laura Hausmann <laura@hausmann.dev>
Co-authored-by: GitLab CI <project_7_bot_1bfaee5701aed20091a86249a967a6c1@noreply.firefish.dev>
Co-authored-by: Hosted Weblate <hosted@weblate.org>
Co-authored-by: Saamkhaih Kyakya <70475761+hiohlan@users.noreply.github.com>

See merge request firefish/firefish!11214
2024-08-01 00:03:39 +09:00

47 lines
1 KiB
TypeScript

import { Stream } from "firefish-js";
import { markRaw } from "vue";
import { url } from "@/config";
import { me } from "@/me";
let stream: Stream | null = null;
let timeoutHeartBeat: number | null = null;
export let isReloading = false;
export function useStream() {
if (stream != null) return stream;
stream = markRaw(
new Stream(
url,
me
? {
token: me.token,
}
: null,
),
);
timeoutHeartBeat = window.setTimeout(heartbeat, 1000 * 60);
return stream;
}
export function reloadStream() {
if (stream == null) return useStream();
if (timeoutHeartBeat != null) window.clearTimeout(timeoutHeartBeat);
isReloading = true;
stream.close();
// biome-ignore lint/suspicious/noAssignInExpressions: assign intentionally
stream.once("_connected_", () => (isReloading = false));
stream.stream.reconnect();
isReloading = false;
return stream;
}
function heartbeat(): void {
if (stream != null && document.visibilityState === "visible") {
stream.send("ping");
}
timeoutHeartBeat = window.setTimeout(heartbeat, 1000 * 60);
}