Skip to content

Commit

Permalink
feat: add heartbeats to line view
Browse files Browse the repository at this point in the history
  • Loading branch information
martinstark committed Apr 15, 2024
1 parent f987605 commit cf32492
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type TDeleteAudioSessionOptions = {
sessionId: string;
};

type THeartbeatOptions = {
sessionId: string;
};

export const API = {
createProduction: async ({ name, lines }: TCreateProductionOptions) =>
handleFetchRequest<TBasicProductionResponse>(
Expand Down Expand Up @@ -131,7 +135,7 @@ export const API = {
{ method: "DELETE" }
)
),
heartbeat: ({ sessionId }: TDeleteAudioSessionOptions): Promise<string> =>
heartbeat: ({ sessionId }: THeartbeatOptions): Promise<string> =>
handleFetchRequest<string>(
fetch(`${API_URL}heartbeat/${sessionId}`, { method: "GET" })
),
Expand Down
3 changes: 3 additions & 0 deletions src/components/production-line/production-line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Spinner } from "../loader/loader.tsx";
import { TLine, TProduction } from "./types.ts";
import { DisplayContainerHeader } from "../landing-page/display-container-header.tsx";
import { DisplayContainer, FlexContainer } from "../generic-components.ts";
import { useHeartbeat } from "./use-heartbeat.ts";

const TempDiv = styled.div`
padding: 1rem 0;
Expand Down Expand Up @@ -106,6 +107,8 @@ export const ProductionLine: FC = () => {
// TODO add handling for `connectionState === "failed"`
}, [connectionState]);

useHeartbeat({ sessionId });

// TODO if (!input !output !username) return <JoinProductionForm />

const exit = () => {
Expand Down
19 changes: 19 additions & 0 deletions src/components/production-line/use-heartbeat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect } from "react";
import { API } from "../../api/api.ts";
import { noop } from "../../helpers.ts";

type TProps = { sessionId: string | null };

export const useHeartbeat = ({ sessionId }: TProps) => {
useEffect(() => {
if (!sessionId) return noop;

const interval = window.setInterval(() => {
API.heartbeat({ sessionId }).catch(console.error);
}, 10_000);

return () => {
window.clearInterval(interval);
};
}, [sessionId]);
};

0 comments on commit cf32492

Please sign in to comment.