forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

websocket-messages.ts54 lines, 5.0 KiBHistoryRawDownload
1import type { AttemptStatus } from "@/api/client";
2
3type RecordValue = Record<string, unknown>;
4
5const isRecord = (value: unknown): value is RecordValue => !!value && typeof value === "object" && !Array.isArray(value);
6const isStringArray = (value: unknown): value is string[] => Array.isArray(value) && value.every((item) => typeof item === "string");
7const isNullableNumber = (value: unknown): value is number | null => value === null || typeof value === "number";
8const isAttemptStatus = (value: unknown): value is AttemptStatus =>
9 value === "in_progress" || value === "submitted" || value === "expired";
10
11export type ExamWsMessage =
12 | { type: "state"; status: AttemptStatus; deadline: number | null; remaining_ms: number | null; now: number; answered: number; question_count: number }
13 | { type: "saved"; question_id: string; updated_at: number; client_seq?: number }
14 | { type: "finished"; finished_at: number }
15 | { type: "expired" }
16 | { type: "pong" }
17 | { type: "error"; message: string; question_id?: string; client_seq?: number };
18
19export function parseExamWsMessage(value: unknown): ExamWsMessage | null {
20 if (!isRecord(value) || typeof value.type !== "string") return null;
21 if (value.type === "state" && isAttemptStatus(value.status) && isNullableNumber(value.deadline) && isNullableNumber(value.remaining_ms) && typeof value.now === "number" && typeof value.answered === "number" && typeof value.question_count === "number") return value as ExamWsMessage;
22 if (value.type === "saved" && typeof value.question_id === "string" && typeof value.updated_at === "number" && (value.client_seq === undefined || typeof value.client_seq === "number")) return value as ExamWsMessage;
23 if (value.type === "finished" && typeof value.finished_at === "number") return value as ExamWsMessage;
24 if (value.type === "expired" || value.type === "pong") return value as ExamWsMessage;
25 if (value.type === "error" && typeof value.message === "string" && (value.question_id === undefined || typeof value.question_id === "string") && (value.client_seq === undefined || typeof value.client_seq === "number")) return value as ExamWsMessage;
26 return null;
27}
28
29export type BoardWsMessage =
30 | { type: "state"; board: string; epoch: number; locked: boolean; closed_at: number | null; creator: string; participants: string[]; now: number }
31 | { type: "strokes"; epoch: number; strokes: { id: string; author: string; payload: string }[] }
32 | { type: "synced"; epoch: number; cursor: string | null }
33 | { type: "stroke"; id: string; author: string; payload: string; epoch: number }
34 | { type: "saved"; id: string; client_seq?: number }
35 | { type: "pong" } | { type: "cleared"; epoch: number; by: string | null }
36 | { type: "locked"; locked: boolean; by: string } | { type: "closed"; closed_at: number | null }
37 | { type: "participants"; creator: string; participants: string[] } | { type: "deleted" }
38 | { type: "error"; code: string; message: string; client_seq?: number };
39
40export function parseBoardWsMessage(value: unknown): BoardWsMessage | null {
41 if (!isRecord(value) || typeof value.type !== "string") return null;
42 if (value.type === "state" && typeof value.board === "string" && typeof value.epoch === "number" && typeof value.locked === "boolean" && isNullableNumber(value.closed_at) && typeof value.creator === "string" && isStringArray(value.participants) && typeof value.now === "number") return value as BoardWsMessage;
43 if (value.type === "strokes" && typeof value.epoch === "number" && Array.isArray(value.strokes) && value.strokes.every((row) => isRecord(row) && typeof row.id === "string" && typeof row.author === "string" && typeof row.payload === "string")) return value as BoardWsMessage;
44 if (value.type === "synced" && typeof value.epoch === "number" && (value.cursor === null || typeof value.cursor === "string")) return value as BoardWsMessage;
45 if (value.type === "stroke" && typeof value.id === "string" && typeof value.author === "string" && typeof value.payload === "string" && typeof value.epoch === "number") return value as BoardWsMessage;
46 if (value.type === "saved" && typeof value.id === "string" && (value.client_seq === undefined || typeof value.client_seq === "number")) return value as BoardWsMessage;
47 if (value.type === "cleared" && typeof value.epoch === "number" && (value.by === null || typeof value.by === "string")) return value as BoardWsMessage;
48 if (value.type === "locked" && typeof value.locked === "boolean" && typeof value.by === "string") return value as BoardWsMessage;
49 if (value.type === "closed" && isNullableNumber(value.closed_at)) return value as BoardWsMessage;
50 if (value.type === "participants" && typeof value.creator === "string" && isStringArray(value.participants)) return value as BoardWsMessage;
51 if (value.type === "error" && typeof value.code === "string" && typeof value.message === "string" && (value.client_seq === undefined || typeof value.client_seq === "number")) return value as BoardWsMessage;
52 if (value.type === "pong" || value.type === "deleted") return value as BoardWsMessage;
53 return null;
54}