@kyneta/websocket-transport 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,78 +0,0 @@
1
- // client-state-machine — Websocket client state machine.
2
- //
3
- // Thin wrapper around the generic ClientStateMachine<S> from @kyneta/exchange,
4
- // providing websocket-specific convenience helpers (isConnectedOrReady, isReady)
5
- // and the 5-state transition map.
6
- //
7
- // States: disconnected → connecting → connected → ready
8
- // ↓ ↓ ↓
9
- // reconnecting ← ─ ┴ ─ ─ ─ ─ ┘
10
- // ↓
11
- // connecting (retry)
12
- // ↓
13
- // disconnected (max retries)
14
-
15
- import { ClientStateMachine } from "@kyneta/exchange"
16
- import type { WebsocketClientState } from "./types.js"
17
-
18
- // ---------------------------------------------------------------------------
19
- // Websocket transition map
20
- // ---------------------------------------------------------------------------
21
-
22
- const WS_VALID_TRANSITIONS: Record<string, string[]> = {
23
- disconnected: ["connecting"],
24
- connecting: ["connected", "disconnected", "reconnecting"],
25
- connected: ["ready", "disconnected", "reconnecting"],
26
- ready: ["disconnected", "reconnecting"],
27
- reconnecting: ["connecting", "disconnected"],
28
- }
29
-
30
- // ---------------------------------------------------------------------------
31
- // WebsocketClientStateMachine
32
- // ---------------------------------------------------------------------------
33
-
34
- /**
35
- * Observable state machine for Websocket client connection lifecycle.
36
- *
37
- * Extends the generic `ClientStateMachine<WebsocketClientState>` with
38
- * websocket-specific convenience helpers.
39
- *
40
- * Usage:
41
- * ```typescript
42
- * const sm = new WebsocketClientStateMachine()
43
- *
44
- * sm.subscribeToTransitions(({ from, to }) => {
45
- * console.log(`${from.status} → ${to.status}`)
46
- * })
47
- *
48
- * sm.transition({ status: "connecting", attempt: 1 })
49
- * sm.transition({ status: "connected" })
50
- * sm.transition({ status: "ready" })
51
- *
52
- * // Transitions are delivered asynchronously via microtask
53
- * // Listener will see: disconnected → connecting, connecting → connected, connected → ready
54
- * ```
55
- */
56
- export class WebsocketClientStateMachine extends ClientStateMachine<WebsocketClientState> {
57
- constructor() {
58
- super({
59
- initialState: { status: "disconnected" },
60
- validTransitions: WS_VALID_TRANSITIONS,
61
- })
62
- }
63
-
64
- /**
65
- * Check if the client is in a "connected" state (either connected or ready).
66
- */
67
- isConnectedOrReady(): boolean {
68
- const s = this.getStatus()
69
- return s === "connected" || s === "ready"
70
- }
71
-
72
- /**
73
- * Check if the client is ready (server ready signal received).
74
- */
75
- isReady(): boolean {
76
- return this.getStatus() === "ready"
77
- }
78
- }