hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | /** |
| 2 | * Pending-acknowledgement registry for fire-and-forget sockets. |
| 3 | * |
| 4 | * A WebSocket `send()` only means "handed to the OS" — it is not a save. The |
| 5 | * exam room waits for the server's `{"type":"saved", question_id}` frame before |
| 6 | * it may tell a student their answer is stored. `wait(seq, key)` hands back a |
| 7 | * promise that settles on `ack(seq)`, rejects on `fail(seq)` (an error frame |
| 8 | * that names the send it refused), rejects on `failAll()` (a socket drop, or a |
| 9 | * frame that identifies no send at all) and rejects on its own timeout when |
| 10 | * nothing ever comes back. |
| 11 | * |
| 12 | * **Acks are matched per send, not per question.** Every `answer` frame carries |
| 13 | * a `client_seq` the caller mints, and the server echoes it on the one |
| 14 | * `saved`/`error` frame that answers it — so a reply names its send exactly. |
| 15 | * Nothing is ever inferred from arrival order: a send that timed out or was |
| 16 | * overtaken is simply gone, and its late frame finds no entry and settles |
| 17 | * nobody. The `client_seq` is opaque to the server (no dedupe, no ordering); |
| 18 | * uniqueness within a socket's lifetime is the caller's job. |
| 19 | * |
| 20 | * A second `wait()` for the same key means the student saved again: the older |
| 21 | * promise resolves `"superseded"` so its caller stays quiet and the newer save |
| 22 | * owns the badge. |
| 23 | */ |
| 24 | export type AckResult = "ok" | "superseded"; |
| 25 | |
| 26 | /** Rejection reason when nothing came back at all — callers localize it. */ |
| 27 | export const ACK_TIMEOUT = "ack timeout"; |
| 28 | |
| 29 | type Entry = { |
| 30 | /** Question this send belongs to — only used to supersede its older sends. */ |
| 31 | key: string; |
| 32 | settle: (result: AckResult) => void; |
| 33 | fail: (reason: Error) => void; |
| 34 | timer: ReturnType<typeof setTimeout>; |
| 35 | }; |
| 36 | |
| 37 | export function createAckWaiter(timeoutMs = 8000) { |
| 38 | /** client_seq → the send still waiting on its reply. */ |
| 39 | const pending = new Map<number, Entry>(); |
| 40 | |
| 41 | const take = (seq: number) => { |
| 42 | const entry = pending.get(seq); |
| 43 | if (!entry) return undefined; |
| 44 | pending.delete(seq); |
| 45 | clearTimeout(entry.timer); |
| 46 | return entry; |
| 47 | }; |
| 48 | |
| 49 | return { |
| 50 | wait(seq: number, key: string): Promise<AckResult> { |
| 51 | for (const [at, entry] of [...pending]) { |
| 52 | if (entry.key !== key) continue; |
| 53 | pending.delete(at); |
| 54 | clearTimeout(entry.timer); |
| 55 | entry.settle("superseded"); |
| 56 | } |
| 57 | return new Promise<AckResult>((resolve, reject) => { |
| 58 | pending.set(seq, { |
| 59 | key, |
| 60 | settle: resolve, |
| 61 | fail: reject, |
| 62 | timer: setTimeout(() => { |
| 63 | pending.delete(seq); |
| 64 | reject(new Error(ACK_TIMEOUT)); |
| 65 | }, timeoutMs), |
| 66 | }); |
| 67 | }); |
| 68 | }, |
| 69 | ack(seq: number) { |
| 70 | take(seq)?.settle("ok"); |
| 71 | }, |
| 72 | /** Reject one save — the server blamed this send and no other. */ |
| 73 | fail(seq: number, reason: Error) { |
| 74 | take(seq)?.fail(reason); |
| 75 | }, |
| 76 | /** |
| 77 | * Reject everything in flight: the socket dropped, or a frame arrived that |
| 78 | * named no send (a room-level error, or an older server that echoes no |
| 79 | * `client_seq`). Refusing them all is the safe read — a badge must never |
| 80 | * say "Saved" for a payload the server did not confirm. |
| 81 | */ |
| 82 | failAll(reason: Error) { |
| 83 | for (const [seq] of [...pending]) take(seq)?.fail(reason); |
| 84 | }, |
| 85 | /** Sends still waiting for an outcome. */ |
| 86 | get size() { |
| 87 | return pending.size; |
| 88 | }, |
| 89 | }; |
| 90 | } |