@prunellejs/live 0.1.1 → 0.5.3
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.
- package/package.json +2 -2
- package/src/client.ts +11 -2
- package/src/index.ts +5 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/live",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "Zero-dependency typed SSE rooms and coalesced presence plugin for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"directory": "packages/plugin-live"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"prunelle": "^0.
|
|
25
|
+
"prunelle": "^0.5.3"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/src/client.ts
CHANGED
|
@@ -29,6 +29,11 @@ export interface UseLiveOptions {
|
|
|
29
29
|
const DEFAULT_BASE_DELAY_MS = 1_000
|
|
30
30
|
const DEFAULT_MAX_DELAY_MS = 30_000
|
|
31
31
|
const DEFAULT_MAX_RETRIES = 5
|
|
32
|
+
const DEFAULT_INITIAL_MAX_RETRIES = 2
|
|
33
|
+
|
|
34
|
+
function isPermanentHttpStatus(status: unknown): boolean {
|
|
35
|
+
return typeof status === 'number' && status >= 400 && status < 500 && status !== 408 && status !== 429
|
|
36
|
+
}
|
|
32
37
|
|
|
33
38
|
export function useLive<TMessage>(
|
|
34
39
|
url: string,
|
|
@@ -40,6 +45,7 @@ export function useLive<TMessage>(
|
|
|
40
45
|
let source: EventSource | undefined
|
|
41
46
|
let reconnectTimer: ReturnType<typeof setTimeout> | undefined
|
|
42
47
|
let attempt = 0
|
|
48
|
+
let hasOpened = false
|
|
43
49
|
let manuallyClosed = false
|
|
44
50
|
|
|
45
51
|
const clearReconnectTimer = () => {
|
|
@@ -70,6 +76,7 @@ export function useLive<TMessage>(
|
|
|
70
76
|
es.onopen = () => {
|
|
71
77
|
if (source !== es || manuallyClosed)
|
|
72
78
|
return
|
|
79
|
+
hasOpened = true
|
|
73
80
|
attempt = 0
|
|
74
81
|
status.value = 'open'
|
|
75
82
|
}
|
|
@@ -78,16 +85,18 @@ export function useLive<TMessage>(
|
|
|
78
85
|
if (source !== es || manuallyClosed)
|
|
79
86
|
return
|
|
80
87
|
const errStatus = (ev as { status?: unknown } | undefined)?.status
|
|
81
|
-
if (errStatus
|
|
88
|
+
if (isPermanentHttpStatus(errStatus)) {
|
|
82
89
|
es.close()
|
|
83
90
|
source = undefined
|
|
84
91
|
status.value = 'closed'
|
|
85
92
|
return
|
|
86
93
|
}
|
|
87
94
|
if (es.readyState === EventSource.CLOSED) {
|
|
95
|
+
es.close()
|
|
88
96
|
status.value = 'closed'
|
|
89
97
|
source = undefined
|
|
90
|
-
|
|
98
|
+
const maxRetries = options?.maxRetries ?? (hasOpened ? DEFAULT_MAX_RETRIES : DEFAULT_INITIAL_MAX_RETRIES)
|
|
99
|
+
if (attempt >= maxRetries)
|
|
91
100
|
return
|
|
92
101
|
const base = options?.baseDelayMs ?? DEFAULT_BASE_DELAY_MS
|
|
93
102
|
const max = options?.maxDelayMs ?? DEFAULT_MAX_DELAY_MS
|
package/src/index.ts
CHANGED
|
@@ -216,14 +216,18 @@ function openRoomStream<TMember>(
|
|
|
216
216
|
try {
|
|
217
217
|
controller.enqueue(RETRY_CHUNK)
|
|
218
218
|
state.join(room, sub)
|
|
219
|
-
onJoin?.(room, member)
|
|
220
219
|
joined = true
|
|
220
|
+
onJoin?.(room, member)
|
|
221
221
|
}
|
|
222
222
|
catch (err) {
|
|
223
|
+
joined = false
|
|
223
224
|
cleanup()
|
|
224
225
|
throw err
|
|
225
226
|
}
|
|
226
227
|
|
|
228
|
+
if (cleaned)
|
|
229
|
+
return
|
|
230
|
+
|
|
227
231
|
if (pingMs > 0) {
|
|
228
232
|
timer = setInterval(() => {
|
|
229
233
|
sub?.send(PING_CHUNK)
|