@supatype/client 0.9.1 → 0.10.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.
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +112 -72
- package/.turbo/turbo-typecheck.log +2 -2
- package/CHANGELOG.md +123 -20
- package/README.md +18 -0
- package/dist/auth.d.ts +13 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +53 -13
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +4 -1
- package/dist/bindings.d.ts.map +1 -1
- package/dist/bindings.js +2 -1
- package/dist/bindings.js.map +1 -1
- package/dist/channel-watch.d.ts +42 -0
- package/dist/channel-watch.d.ts.map +1 -0
- package/dist/channel-watch.js +59 -0
- package/dist/channel-watch.js.map +1 -0
- package/dist/errors.d.ts +40 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +31 -0
- package/dist/errors.js.map +1 -1
- package/dist/exact-values.d.ts +18 -5
- package/dist/exact-values.d.ts.map +1 -1
- package/dist/exact-values.js +76 -12
- package/dist/exact-values.js.map +1 -1
- package/dist/field-kinds-registry.d.ts +1 -1
- package/dist/field-kinds-registry.d.ts.map +1 -1
- package/dist/field-kinds-registry.js +20 -2
- package/dist/field-kinds-registry.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/latest-rows.d.ts +15 -0
- package/dist/latest-rows.d.ts.map +1 -1
- package/dist/latest-rows.js +14 -7
- package/dist/latest-rows.js.map +1 -1
- package/dist/public-env.d.ts +13 -0
- package/dist/public-env.d.ts.map +1 -0
- package/dist/public-env.js +23 -0
- package/dist/public-env.js.map +1 -0
- package/dist/query-cache.d.ts +2 -1
- package/dist/query-cache.d.ts.map +1 -1
- package/dist/query-cache.js.map +1 -1
- package/dist/query.d.ts +7 -0
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +12 -4
- package/dist/query.js.map +1 -1
- package/dist/realtime-refusals.d.ts +35 -0
- package/dist/realtime-refusals.d.ts.map +1 -0
- package/dist/realtime-refusals.js +73 -0
- package/dist/realtime-refusals.js.map +1 -0
- package/dist/realtime.d.ts +87 -8
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +304 -51
- package/dist/realtime.js.map +1 -1
- package/dist/runtime-env.d.ts +2 -0
- package/dist/runtime-env.d.ts.map +1 -1
- package/dist/runtime-env.js +45 -5
- package/dist/runtime-env.js.map +1 -1
- package/dist/table-subscription.d.ts +27 -11
- package/dist/table-subscription.d.ts.map +1 -1
- package/dist/table-subscription.js +159 -25
- package/dist/table-subscription.js.map +1 -1
- package/dist/types.d.ts +34 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/src/auth.ts +55 -12
- package/src/bindings.ts +4 -1
- package/src/channel-watch.ts +86 -0
- package/src/errors.ts +54 -0
- package/src/exact-values.ts +101 -13
- package/src/field-kinds-registry.ts +2 -3
- package/src/index.ts +14 -2
- package/src/latest-rows.ts +24 -7
- package/src/public-env.ts +22 -0
- package/src/query-cache.ts +2 -1
- package/src/query.ts +12 -4
- package/src/realtime-refusals.ts +100 -0
- package/src/realtime.ts +385 -56
- package/src/runtime-env.ts +53 -5
- package/src/table-subscription.ts +190 -32
- package/src/types.ts +40 -1
- package/tests/auth-restore-user.test.ts +84 -0
- package/tests/auth-sso.test.ts +32 -1
- package/tests/auth.test.ts +11 -0
- package/tests/channel-watch-private.test.ts +105 -0
- package/tests/channel-watch.test.ts +97 -0
- package/tests/exact-response.test.ts +101 -0
- package/tests/exact-values.test.ts +57 -0
- package/tests/examples-runtime-env.test.ts +135 -0
- package/tests/field-kinds-registry.test.ts +12 -0
- package/tests/helpers/close-event.ts +12 -0
- package/tests/helpers/fake-client.ts +38 -0
- package/tests/helpers/mock-websocket.ts +22 -0
- package/tests/helpers/reactive-options-binding.ts +99 -0
- package/tests/public-env.test.ts +43 -0
- package/tests/query-cache.test.ts +1 -1
- package/tests/query.test.ts +39 -2
- package/tests/realtime-connection.test.ts +228 -0
- package/tests/realtime-exact-values.test.ts +42 -0
- package/tests/realtime-multi-tenant-integration.test.ts +0 -1
- package/tests/realtime-private-channels.test.ts +377 -0
- package/tests/realtime-refusal-error.test.ts +80 -0
- package/tests/realtime-rows-after-broadcast.test.ts +205 -0
- package/tests/realtime-session-auth.test.ts +0 -1
- package/tests/realtime-shared-channel.test.ts +9 -27
- package/tests/realtime-typed-channels.test.ts +49 -0
- package/tests/runtime-env-expo.test.ts +58 -0
- package/tests/runtime-env.test.ts +49 -0
- package/tests/subscribe-frame.test.ts +48 -0
- package/tests/table-subscription-rows.test.ts +136 -0
- package/tests/table-subscription.test.ts +4 -3
- package/tsconfig.json +2 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/type-tests/declared-channels.ts +30 -0
- package/type-tests/tsconfig.json +4 -0
package/src/realtime.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { asHeadersProvider, bearerToken, type HeadersProvider } from "./query.js"
|
|
2
2
|
import { settle, type Deferred } from "./runtime-env.js"
|
|
3
|
-
import {
|
|
3
|
+
import type { ChannelName } from "./types.js"
|
|
4
|
+
import { coerceRecordKinds, knownExactKinds, type ExactFields } from "./exact-values.js"
|
|
5
|
+
import {
|
|
6
|
+
PRIVATE_CHANNELS_FEATURE,
|
|
7
|
+
channelRefusal,
|
|
8
|
+
classifyRefusal,
|
|
9
|
+
isActionRefusal,
|
|
10
|
+
unsupportedServer,
|
|
11
|
+
type ChannelActionRefusal,
|
|
12
|
+
} from "./realtime-refusals.js"
|
|
4
13
|
import { isArrayOf, isJsonObject, isOneOf, isRecordOf, isShape, isString, nullable, optional, type Guard } from "./json.js"
|
|
5
14
|
|
|
6
15
|
export type RealtimeEvent = "INSERT" | "UPDATE" | "DELETE" | "*"
|
|
@@ -18,7 +27,43 @@ type RealtimeCallback<TRow> = (payload: RealtimePayload<TRow>) => void
|
|
|
18
27
|
|
|
19
28
|
export type ChannelStatus = "SUBSCRIBED" | "SUBSCRIBING" | "CLOSED" | "CHANNEL_ERROR" | "TIMED_OUT"
|
|
20
29
|
|
|
21
|
-
|
|
30
|
+
/**
|
|
31
|
+
* The socket itself, as opposed to any one channel's join on it.
|
|
32
|
+
*
|
|
33
|
+
* "connected": the socket is open. "connecting": it is opening for the first time, reopening after
|
|
34
|
+
* a drop or a sign-in swap, or waiting out a backoff. "disconnected": nothing is open and nothing
|
|
35
|
+
* is scheduled, because no channel has subscribed, disconnect() was called, or every retry failed.
|
|
36
|
+
*/
|
|
37
|
+
export type RealtimeConnectionState = "connected" | "connecting" | "disconnected"
|
|
38
|
+
|
|
39
|
+
/** Called each time the connection state changes, with the new state. */
|
|
40
|
+
export type RealtimeConnectionListener = (state: RealtimeConnectionState) => void
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Told each status a channel reaches. `err` carries the server's reason with a CHANNEL_ERROR it
|
|
44
|
+
* refused, such as a join for a table that does not exist, and is undefined otherwise: the same
|
|
45
|
+
* `(status, err)` shape Supabase's client uses, so a callback taking only the status is unaffected.
|
|
46
|
+
*/
|
|
47
|
+
export type ChannelStatusCallback = (status: ChannelStatus, err?: Error | undefined) => void
|
|
48
|
+
|
|
49
|
+
/** What a channel can be opened with. */
|
|
50
|
+
export interface ChannelOptions {
|
|
51
|
+
/**
|
|
52
|
+
* Assert that this channel is private: guarded by a `Channel` declaration in the schema.
|
|
53
|
+
*
|
|
54
|
+
* The server enforces every declared channel whether or not this is set, so it never loosens
|
|
55
|
+
* anything. What it adds is two refusals instead of two silent open channels. A name no
|
|
56
|
+
* declaration matches is refused (`RealtimeChannelError` reason `not_declared`), which catches a
|
|
57
|
+
* typo at once. And a server that does not announce private channel support is not asked at all
|
|
58
|
+
* (reason `unsupported_server`), since an older server would ignore the flag and admit an open
|
|
59
|
+
* channel. Both arrive as `CHANNEL_ERROR` on the status callback.
|
|
60
|
+
*
|
|
61
|
+
* Handles on one channel name share one join: if any handle says `private: true`, the join does.
|
|
62
|
+
*/
|
|
63
|
+
private?: boolean | undefined
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ChannelSubscription<TRow> {
|
|
22
67
|
on(
|
|
23
68
|
event: "postgres_changes",
|
|
24
69
|
opts: {
|
|
@@ -38,8 +83,14 @@ interface ChannelSubscription<TRow> {
|
|
|
38
83
|
event: string,
|
|
39
84
|
callback: (payload: Record<string, unknown>) => void,
|
|
40
85
|
): ChannelSubscription<TRow>
|
|
86
|
+
/**
|
|
87
|
+
* Listen for a broadcast, presence track or payload the server refused on this channel (a
|
|
88
|
+
* `ChannelActionRefusal`: `send_denied`, `track_denied` or `payload_refused`). The channel is
|
|
89
|
+
* still joined, so its status does not change; this is where the app learns its send went nowhere.
|
|
90
|
+
*/
|
|
91
|
+
onRefused(callback: (refusal: ChannelActionRefusal) => void): ChannelSubscription<TRow>
|
|
41
92
|
/** Subscribe to the channel, opening the WebSocket if needed. */
|
|
42
|
-
subscribe(callback?:
|
|
93
|
+
subscribe(callback?: ChannelStatusCallback | undefined): ChannelSubscription<TRow>
|
|
43
94
|
/** Unsubscribe and clean up. */
|
|
44
95
|
unsubscribe(): void
|
|
45
96
|
/** Send a broadcast event to the channel. */
|
|
@@ -103,6 +154,26 @@ function parseChannelName(name: string): { schema: string; table: string } {
|
|
|
103
154
|
return { schema: "public", table: name }
|
|
104
155
|
}
|
|
105
156
|
|
|
157
|
+
/**
|
|
158
|
+
* The schema and table a listener on channel `name` is for.
|
|
159
|
+
*
|
|
160
|
+
* The name is read as `schema:table` only when it supplies the table as well. A listener that
|
|
161
|
+
* names its table and no schema means `public`, as the docs say. 0.9.1 release test (RB4): the
|
|
162
|
+
* schema was taken from the name whatever the listener said, so the documented
|
|
163
|
+
* `channel("post:42").on(..., { table: "comment" })` joined `post.comment`, reported SUBSCRIBED
|
|
164
|
+
* and delivered nothing. The server's `resolveTarget` reads a frame the same way.
|
|
165
|
+
*/
|
|
166
|
+
function listenerTarget(
|
|
167
|
+
name: string,
|
|
168
|
+
opts: { schema?: string | undefined; table?: string | undefined },
|
|
169
|
+
): { schema: string; table: string } {
|
|
170
|
+
if (opts.table === undefined) {
|
|
171
|
+
const named = parseChannelName(name)
|
|
172
|
+
return { schema: opts.schema ?? named.schema, table: named.table }
|
|
173
|
+
}
|
|
174
|
+
return { schema: opts.schema ?? "public", table: opts.table }
|
|
175
|
+
}
|
|
176
|
+
|
|
106
177
|
// ─── Server message types (subset matching @supatype/realtime) ───────────────
|
|
107
178
|
|
|
108
179
|
interface ServerChangeMessage {
|
|
@@ -115,8 +186,11 @@ interface ServerChangeMessage {
|
|
|
115
186
|
*
|
|
116
187
|
* The service reads them from the database's own declared types, so this is authoritative in a
|
|
117
188
|
* way the local registry is not: a project that never generated still gets the right value.
|
|
189
|
+
*
|
|
190
|
+
* The kind is any string: one this client does not know is ignored, and that column passes through
|
|
191
|
+
* as sent, so a kind added later never drops a change.
|
|
118
192
|
*/
|
|
119
|
-
exactColumns?:
|
|
193
|
+
exactColumns?: Readonly<Record<string, string>>
|
|
120
194
|
timestamp: string
|
|
121
195
|
}
|
|
122
196
|
|
|
@@ -140,8 +214,12 @@ interface ServerSystemMessage {
|
|
|
140
214
|
message: string
|
|
141
215
|
/** The channel a refusal is about; an error without one is about the socket. */
|
|
142
216
|
channel?: string
|
|
217
|
+
/** Why, as a fixed code (packages/realtime `refusal-codes.ts`). A server that predates codes sends none. */
|
|
218
|
+
code?: string
|
|
143
219
|
/** The `ref` of the join this answers. A server that predates refs sends none. */
|
|
144
220
|
ref?: string
|
|
221
|
+
/** What the server can do beyond the base protocol, on the first frame of a socket. */
|
|
222
|
+
features?: string[]
|
|
145
223
|
}
|
|
146
224
|
|
|
147
225
|
type ServerMessage = ServerChangeMessage | ServerPresenceMessage | ServerBroadcastMessage | ServerSystemMessage
|
|
@@ -155,7 +233,7 @@ const isChangeMessage: Guard<ServerChangeMessage> = isShape({
|
|
|
155
233
|
channel: isString,
|
|
156
234
|
event: isOneOf(["INSERT", "UPDATE", "DELETE"]),
|
|
157
235
|
payload: isShape({ old: isRecordOrNull, new: isRecordOrNull }),
|
|
158
|
-
exactColumns: optional(isRecordOf(
|
|
236
|
+
exactColumns: optional(isRecordOf(isString)),
|
|
159
237
|
timestamp: isString,
|
|
160
238
|
})
|
|
161
239
|
|
|
@@ -178,7 +256,9 @@ const isSystemMessage: Guard<ServerSystemMessage> = isShape({
|
|
|
178
256
|
status: isOneOf(["ok", "error"]),
|
|
179
257
|
message: isString,
|
|
180
258
|
channel: optional(isString),
|
|
259
|
+
code: optional(isString),
|
|
181
260
|
ref: optional(isString),
|
|
261
|
+
features: optional(isArrayOf(isString)),
|
|
182
262
|
})
|
|
183
263
|
|
|
184
264
|
/** A frame from the realtime service (packages/realtime/src/types.ts) in one of the four shapes. */
|
|
@@ -189,6 +269,15 @@ function isServerMessage(value: unknown): value is ServerMessage {
|
|
|
189
269
|
/** How the server's acknowledgement of a join begins; the channel name follows. */
|
|
190
270
|
const SUBSCRIBED_TO = "subscribed to "
|
|
191
271
|
|
|
272
|
+
/** The longest wait between reconnect attempts. Without it ten doublings from one second reach 512 s. */
|
|
273
|
+
const MAX_RECONNECT_DELAY_MS = 30_000
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* How long a socket may stay CONNECTING. One opened while realtime restarts behind the gateway can
|
|
277
|
+
* stay there with no error and no close, and the client would wait on it for good.
|
|
278
|
+
*/
|
|
279
|
+
const CONNECT_TIMEOUT_MS = 10_000
|
|
280
|
+
|
|
192
281
|
// ─── Realtime client ─────────────────────────────────────────────────────────
|
|
193
282
|
|
|
194
283
|
export class RealtimeClient {
|
|
@@ -200,6 +289,8 @@ export class RealtimeClient {
|
|
|
200
289
|
private reconnectAttempts = 0
|
|
201
290
|
private maxReconnectAttempts = 10
|
|
202
291
|
private baseReconnectDelay = 1000
|
|
292
|
+
private connection: RealtimeConnectionState = "disconnected"
|
|
293
|
+
private readonly connectionListeners = new Set<RealtimeConnectionListener>()
|
|
203
294
|
/** The token the live socket was opened with, so an auth change can tell whether it still holds. */
|
|
204
295
|
private currentToken: string | undefined
|
|
205
296
|
/**
|
|
@@ -214,6 +305,11 @@ export class RealtimeClient {
|
|
|
214
305
|
private attempt: Promise<void> | null = null
|
|
215
306
|
/** The last `ref` a join was sent with. Unique for the life of the client, across sockets. */
|
|
216
307
|
private lastRef = 0
|
|
308
|
+
/**
|
|
309
|
+
* What the server on the live socket announced on its first frame: undefined until that frame
|
|
310
|
+
* arrives, an empty set for a server that announces nothing (one that predates private channels).
|
|
311
|
+
*/
|
|
312
|
+
private serverFeatures: ReadonlySet<string> | undefined
|
|
217
313
|
|
|
218
314
|
/**
|
|
219
315
|
* `auth` is either a headers provider or a fixed set of headers: the same pair `QueryBuilder` and
|
|
@@ -229,6 +325,39 @@ export class RealtimeClient {
|
|
|
229
325
|
this.getHeaders = asHeadersProvider(auth)
|
|
230
326
|
}
|
|
231
327
|
|
|
328
|
+
/** The socket's own state. A channel's status cannot say this: it reads SUBSCRIBING for a drop and a first connect alike. */
|
|
329
|
+
get connectionState(): RealtimeConnectionState {
|
|
330
|
+
return this.connection
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** True while the socket is open. A channel may still be waiting on its join. */
|
|
334
|
+
get isConnected(): boolean {
|
|
335
|
+
return this.connection === "connected"
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Call `listener` each time the connection state changes. It is not called with the current state
|
|
340
|
+
* on registering: read `connectionState` or `isConnected` for that. Returns a function that stops it.
|
|
341
|
+
*/
|
|
342
|
+
onConnectionChange(listener: RealtimeConnectionListener): () => void {
|
|
343
|
+
this.connectionListeners.add(listener)
|
|
344
|
+
return () => {
|
|
345
|
+
this.connectionListeners.delete(listener)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private setConnection(next: RealtimeConnectionState): void {
|
|
350
|
+
if (this.connection === next) return
|
|
351
|
+
this.connection = next
|
|
352
|
+
for (const listener of [...this.connectionListeners]) {
|
|
353
|
+
try {
|
|
354
|
+
listener(next)
|
|
355
|
+
} catch {
|
|
356
|
+
// One listener's bug must not stop the others hearing, or stop the socket doing its work.
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
232
361
|
/** The socket's address, as ws(s) for an http(s) one. Read when connecting, not before. */
|
|
233
362
|
private get url(): string {
|
|
234
363
|
return settle(this.base).replace(/^http/, "ws")
|
|
@@ -294,6 +423,7 @@ export class RealtimeClient {
|
|
|
294
423
|
const old = this.ws
|
|
295
424
|
this.ws = null
|
|
296
425
|
this.currentToken = undefined
|
|
426
|
+
this.serverFeatures = undefined
|
|
297
427
|
if (old !== null) {
|
|
298
428
|
old.onopen = null
|
|
299
429
|
old.onmessage = null
|
|
@@ -321,16 +451,8 @@ export class RealtimeClient {
|
|
|
321
451
|
state.joined = undefined
|
|
322
452
|
state.joining = undefined
|
|
323
453
|
state.joinRef = undefined
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
private forgetMembers(state: ChannelState): void {
|
|
329
|
-
if (state.members.length === 0) return
|
|
330
|
-
const leaves = state.members
|
|
331
|
-
state.members = []
|
|
332
|
-
for (const handle of state.handles) {
|
|
333
|
-
for (const listener of handle.presenceListeners) listener({ joins: [], leaves })
|
|
454
|
+
state.joinedPrivate = false
|
|
455
|
+
forgetMembers(state)
|
|
334
456
|
}
|
|
335
457
|
}
|
|
336
458
|
|
|
@@ -343,29 +465,31 @@ export class RealtimeClient {
|
|
|
343
465
|
* `useSubscription` hooks with one channel name broke each other when either unmounted. The join
|
|
344
466
|
* is now left only when the last handle on the name unsubscribes.
|
|
345
467
|
*/
|
|
346
|
-
channel<TRow = Record<string, unknown>>(name:
|
|
468
|
+
channel<TRow = Record<string, unknown>>(name: ChannelName, opts: ChannelOptions = {}): ChannelSubscription<TRow> {
|
|
347
469
|
const state = this.getOrCreateChannel(name)
|
|
348
470
|
const handle: ChannelHandle = {
|
|
349
471
|
pgListeners: [],
|
|
350
472
|
presenceListeners: [],
|
|
351
473
|
broadcastListeners: new Map(),
|
|
474
|
+
refusalListeners: [],
|
|
352
475
|
statusCallback: null,
|
|
476
|
+
private: opts.private === true,
|
|
353
477
|
}
|
|
354
478
|
state.handles.add(handle)
|
|
355
479
|
|
|
356
480
|
const sub: ChannelSubscription<TRow> = {
|
|
357
|
-
on(_event, opts, callback) {
|
|
358
|
-
//
|
|
359
|
-
//
|
|
360
|
-
// named `app:orders` was registered against `public.orders`.
|
|
361
|
-
const named = parseChannelName(name)
|
|
481
|
+
on: (_event, opts, callback) => {
|
|
482
|
+
// A channel named `app:orders` with no table on the listener is `app.orders`, the way the
|
|
483
|
+
// server's fallback reads it; a listener naming its table defaults to `public`.
|
|
362
484
|
handle.pgListeners.push({
|
|
363
485
|
event: opts.event,
|
|
364
|
-
|
|
365
|
-
table: opts.table ?? named.table,
|
|
486
|
+
...listenerTarget(name, opts),
|
|
366
487
|
filter: opts.filter,
|
|
367
488
|
callback: forCallerRows(callback),
|
|
368
489
|
})
|
|
490
|
+
// A join already sent for broadcast or presence alone delivers no rows, and `subscribe()`
|
|
491
|
+
// will not send another, so the first row listener on it joins again here.
|
|
492
|
+
this.rejoinIfUnheard(state)
|
|
369
493
|
return sub
|
|
370
494
|
},
|
|
371
495
|
|
|
@@ -381,13 +505,24 @@ export class RealtimeClient {
|
|
|
381
505
|
return sub
|
|
382
506
|
},
|
|
383
507
|
|
|
508
|
+
onRefused(callback) {
|
|
509
|
+
handle.refusalListeners.push(callback)
|
|
510
|
+
return sub
|
|
511
|
+
},
|
|
512
|
+
|
|
384
513
|
subscribe: (callback) => {
|
|
385
514
|
handle.statusCallback = callback ?? null
|
|
386
515
|
// Joining twice is a no-op rather than a second frame, so `from(table).subscribe()` can
|
|
387
516
|
// join on its own and a caller can still call this afterwards just to watch the status.
|
|
388
517
|
// A join already acknowledged will not be acknowledged again, so say so here or the new
|
|
389
518
|
// callback waits for a SUBSCRIBED that is never coming.
|
|
390
|
-
if (state.subscribed) callback?.("SUBSCRIBED")
|
|
519
|
+
if (state.subscribed && serves(state.joined, handle)) callback?.("SUBSCRIBED")
|
|
520
|
+
// An app subscribing again after a withdrawal is asking to try the join again.
|
|
521
|
+
if (state.withdrawn) {
|
|
522
|
+
state.withdrawn = false
|
|
523
|
+
state.joinSent = false
|
|
524
|
+
}
|
|
525
|
+
this.rejoinForPrivacy(state)
|
|
391
526
|
this.ensureConnection()
|
|
392
527
|
this.sendSubscribe(state)
|
|
393
528
|
return sub
|
|
@@ -454,6 +589,7 @@ export class RealtimeClient {
|
|
|
454
589
|
*/
|
|
455
590
|
disconnect(): void {
|
|
456
591
|
this.abandonSocket("client disconnect")
|
|
592
|
+
this.setConnection("disconnected")
|
|
457
593
|
this.notifyChannels("CLOSED")
|
|
458
594
|
}
|
|
459
595
|
|
|
@@ -470,6 +606,9 @@ export class RealtimeClient {
|
|
|
470
606
|
joined: undefined,
|
|
471
607
|
joining: undefined,
|
|
472
608
|
joinRef: undefined,
|
|
609
|
+
joinedPrivate: false,
|
|
610
|
+
withdrawn: false,
|
|
611
|
+
unsupported: false,
|
|
473
612
|
members: [],
|
|
474
613
|
}
|
|
475
614
|
this.channels.set(name, state)
|
|
@@ -490,6 +629,7 @@ export class RealtimeClient {
|
|
|
490
629
|
// Read here, synchronously, so a client with no address or anon key throws from the subscribe
|
|
491
630
|
// that asked for the socket rather than from a background attempt nobody is waiting on.
|
|
492
631
|
const url = this.url
|
|
632
|
+
this.setConnection("connecting")
|
|
493
633
|
|
|
494
634
|
let attempt: Promise<void>
|
|
495
635
|
const isCurrent = (): boolean => this.attempt === attempt
|
|
@@ -535,12 +675,18 @@ export class RealtimeClient {
|
|
|
535
675
|
if (!isCurrent()) return
|
|
536
676
|
|
|
537
677
|
this.currentToken = token
|
|
678
|
+
this.serverFeatures = undefined
|
|
538
679
|
const wsUrl = token ? `${url}?token=${encodeURIComponent(token)}` : url
|
|
539
680
|
const ws = new WebSocket(wsUrl)
|
|
540
681
|
this.ws = ws
|
|
682
|
+
const handshake = setTimeout(() => {
|
|
683
|
+
if (ws.readyState === WebSocket.CONNECTING) this.socketLost(ws)
|
|
684
|
+
}, CONNECT_TIMEOUT_MS)
|
|
541
685
|
|
|
542
686
|
ws.onopen = () => {
|
|
687
|
+
clearTimeout(handshake)
|
|
543
688
|
this.reconnectAttempts = 0
|
|
689
|
+
this.setConnection("connected")
|
|
544
690
|
// Re-subscribe all channels
|
|
545
691
|
for (const state of this.channels.values()) {
|
|
546
692
|
if (!state.subscribed) {
|
|
@@ -559,28 +705,41 @@ export class RealtimeClient {
|
|
|
559
705
|
}
|
|
560
706
|
}
|
|
561
707
|
|
|
562
|
-
ws.onclose = () =>
|
|
563
|
-
this.ws = null
|
|
564
|
-
this.forgetJoins()
|
|
565
|
-
// Attempt reconnection with exponential backoff
|
|
566
|
-
if (this.channels.size > 0) {
|
|
567
|
-
this.scheduleReconnect()
|
|
568
|
-
}
|
|
569
|
-
}
|
|
708
|
+
ws.onclose = () => this.socketLost(ws)
|
|
570
709
|
|
|
571
710
|
ws.onerror = () => {
|
|
572
|
-
//
|
|
711
|
+
// An open socket's error is followed by its close. One that never opened may not be: Node's
|
|
712
|
+
// built-in WebSocket, refused by a gateway answering 502, fires error and then nothing, and
|
|
713
|
+
// counted as live it was never replaced. So that one is dropped here.
|
|
714
|
+
if (ws.readyState !== WebSocket.OPEN) this.socketLost(ws)
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/** `ws` closed or failed to open: forget it and reconnect with backoff, once per socket. */
|
|
719
|
+
private socketLost(ws: WebSocket): void {
|
|
720
|
+
if (this.ws !== ws) return
|
|
721
|
+
ws.onclose = null
|
|
722
|
+
ws.onerror = null
|
|
723
|
+
if (ws.readyState !== WebSocket.CLOSED) ws.close()
|
|
724
|
+
this.ws = null
|
|
725
|
+
this.forgetJoins()
|
|
726
|
+
if (this.channels.size > 0) {
|
|
727
|
+
this.scheduleReconnect()
|
|
728
|
+
} else {
|
|
729
|
+
this.setConnection("disconnected")
|
|
573
730
|
}
|
|
574
731
|
}
|
|
575
732
|
|
|
576
733
|
private scheduleReconnect(): void {
|
|
577
734
|
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
735
|
+
this.setConnection("disconnected")
|
|
578
736
|
this.notifyChannels("TIMED_OUT")
|
|
579
737
|
return
|
|
580
738
|
}
|
|
581
739
|
|
|
582
|
-
const delay = this.baseReconnectDelay * Math.pow(2, this.reconnectAttempts)
|
|
740
|
+
const delay = Math.min(MAX_RECONNECT_DELAY_MS, this.baseReconnectDelay * Math.pow(2, this.reconnectAttempts))
|
|
583
741
|
this.reconnectAttempts++
|
|
742
|
+
this.setConnection("connecting")
|
|
584
743
|
|
|
585
744
|
this.notifyChannels("SUBSCRIBING")
|
|
586
745
|
|
|
@@ -607,6 +766,10 @@ export class RealtimeClient {
|
|
|
607
766
|
}
|
|
608
767
|
|
|
609
768
|
private handleSystem(msg: ServerSystemMessage): void {
|
|
769
|
+
if (this.serverFeatures === undefined && msg.status === "ok" && !msg.message.startsWith(SUBSCRIBED_TO)) {
|
|
770
|
+
this.serverFeatures = new Set(msg.features ?? [])
|
|
771
|
+
this.joinWaitingChannels()
|
|
772
|
+
}
|
|
610
773
|
if (msg.status === "ok" && msg.message.startsWith(SUBSCRIBED_TO)) {
|
|
611
774
|
const state = this.answeredJoin(msg.message.slice(SUBSCRIBED_TO.length), msg.ref)
|
|
612
775
|
if (state !== undefined) acknowledgeJoin(state)
|
|
@@ -635,14 +798,31 @@ export class RealtimeClient {
|
|
|
635
798
|
* whose refused join it has since replaced, is told nothing.
|
|
636
799
|
*/
|
|
637
800
|
private handleRefusal(msg: ServerSystemMessage): void {
|
|
801
|
+
const kind = msg.channel === undefined ? undefined : classifyRefusal(msg.code, msg.message)
|
|
802
|
+
// A refused send is not a failed channel: it goes to `onRefused` and the status stays as it was.
|
|
803
|
+
if (msg.channel !== undefined && kind !== undefined && isActionRefusal(kind)) {
|
|
804
|
+
refuseAction(this.channels.get(msg.channel), { channel: msg.channel, code: kind, message: msg.message })
|
|
805
|
+
return
|
|
806
|
+
}
|
|
638
807
|
const affected = msg.channel === undefined ? [...this.channels.values()] : [this.answeredJoin(msg.channel, msg.ref)]
|
|
639
808
|
for (const state of affected) {
|
|
640
809
|
if (state === undefined) continue
|
|
810
|
+
const refusal =
|
|
811
|
+
msg.channel === undefined || kind === undefined
|
|
812
|
+
? new Error(msg.message)
|
|
813
|
+
: channelRefusal(msg.channel, msg.message, kind)
|
|
814
|
+
if (msg.channel !== undefined && state.subscribed) {
|
|
815
|
+
if (kind === "withdrawn") withdrawJoin(state, refusal)
|
|
816
|
+
else refuseRejoin(state, refusal)
|
|
817
|
+
continue
|
|
818
|
+
}
|
|
641
819
|
// A refused join registered nothing on the server, so a channel not yet confirmed may
|
|
642
820
|
// join again on this socket. Marked as sent, a retry after authenticating sent nothing.
|
|
643
821
|
if (!state.subscribed) state.joinSent = false
|
|
644
822
|
if (msg.ref !== undefined) state.joinRef = undefined
|
|
645
|
-
|
|
823
|
+
// The server's reason goes to the callback (N-FF5): "table public.notes does not exist"
|
|
824
|
+
// tells the developer what to fix, and a bare CHANNEL_ERROR did not.
|
|
825
|
+
notifyHandles(state, "CHANNEL_ERROR", refusal)
|
|
646
826
|
}
|
|
647
827
|
}
|
|
648
828
|
|
|
@@ -653,7 +833,7 @@ export class RealtimeClient {
|
|
|
653
833
|
// A bigint column arrives as a string and becomes a native bigint here, and a numeric stays
|
|
654
834
|
// the exact string `@supatype/types` declares for Decimal and Money. The same coercion the
|
|
655
835
|
// REST path uses, so a row read either way holds the same values.
|
|
656
|
-
const exact = msg.exactColumns ?? {}
|
|
836
|
+
const exact = knownExactKinds(msg.exactColumns ?? {})
|
|
657
837
|
const newRecord = coerceRecord(msg.payload.new, exact)
|
|
658
838
|
const oldRecord = coerceRecord(msg.payload.old, exact)
|
|
659
839
|
|
|
@@ -704,7 +884,10 @@ export class RealtimeClient {
|
|
|
704
884
|
private sendSubscribe(state: ChannelState): void {
|
|
705
885
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return
|
|
706
886
|
if (state.joinSent) return
|
|
887
|
+
const asPrivate = isPrivate(state)
|
|
888
|
+
if (asPrivate && !this.mayJoinPrivately(state)) return
|
|
707
889
|
state.joinSent = true
|
|
890
|
+
state.joinedPrivate = asPrivate
|
|
708
891
|
|
|
709
892
|
const target = joinTargetOf(state)
|
|
710
893
|
state.joining = target
|
|
@@ -712,27 +895,69 @@ export class RealtimeClient {
|
|
|
712
895
|
// the server delivers for until it acknowledges this one, so the old target stays until then.
|
|
713
896
|
state.joined ??= target
|
|
714
897
|
state.joinRef = String(++this.lastRef)
|
|
715
|
-
this.sendMessage(subscribeFrame(state.name, target, state.joinRef))
|
|
898
|
+
this.sendMessage(subscribeFrame(state.name, target, state.joinRef, asPrivate))
|
|
716
899
|
}
|
|
717
900
|
|
|
718
901
|
/**
|
|
719
|
-
*
|
|
902
|
+
* Whether a private join may be sent on this socket now.
|
|
903
|
+
*
|
|
904
|
+
* Not before the server's first frame says what it can do: the join waits for it. And not at all
|
|
905
|
+
* to a server that does not announce `private_channels`: it would ignore the flag and admit an
|
|
906
|
+
* open channel, so the join is failed here instead and never sent.
|
|
907
|
+
*/
|
|
908
|
+
private mayJoinPrivately(state: ChannelState): boolean {
|
|
909
|
+
if (this.serverFeatures === undefined) return false
|
|
910
|
+
if (this.serverFeatures.has(PRIVATE_CHANNELS_FEATURE)) return true
|
|
911
|
+
if (!state.unsupported) {
|
|
912
|
+
state.unsupported = true
|
|
913
|
+
notifyHandles(state, "CHANNEL_ERROR", unsupportedServer(state.name))
|
|
914
|
+
}
|
|
915
|
+
return false
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/** The server's first frame has arrived: send the joins that were waiting for it. */
|
|
919
|
+
private joinWaitingChannels(): void {
|
|
920
|
+
for (const state of this.channels.values()) {
|
|
921
|
+
state.unsupported = false
|
|
922
|
+
if (!state.subscribed) this.sendSubscribe(state)
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/**
|
|
927
|
+
* A handle asking for `private: true` on a name already joined as open joins again as private,
|
|
928
|
+
* in place, as `rejoinIfUnheard` does: the open join stands until the server takes the new one.
|
|
929
|
+
*/
|
|
930
|
+
private rejoinForPrivacy(state: ChannelState): void {
|
|
931
|
+
if (state.joinSent && !state.joinedPrivate && isPrivate(state)) {
|
|
932
|
+
state.joinSent = false
|
|
933
|
+
if (state.subscribed) notifyUnserved(state, "SUBSCRIBING")
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* Join `state` again when the join it holds delivers rows to no listener left on it.
|
|
720
939
|
*
|
|
721
940
|
* Handles share one join, made for the oldest row listener. When that listener's handle left, the
|
|
722
941
|
* join stayed registered for its table, and every other handle reported SUBSCRIBED and was sent
|
|
723
|
-
* nothing.
|
|
724
|
-
*
|
|
725
|
-
*
|
|
942
|
+
* nothing. A join sent before any row listener asked for no rows, so a row listener added after it
|
|
943
|
+
* was never sent any either (0.9.2 release test, FF-1). Joined again in place, without leaving
|
|
944
|
+
* first: the server replaces a channel's subscription on a second join, where leaving would drop
|
|
945
|
+
* the presence this socket tracks there.
|
|
946
|
+
*
|
|
947
|
+
* An acknowledged join stays registered and serving until the server takes this one, and for good
|
|
948
|
+
* if it refuses it. So the handles it serves stay SUBSCRIBED and its members stay present, and
|
|
949
|
+
* only a handle it does not serve is told SUBSCRIBING. Every handle used to be, and a refused
|
|
950
|
+
* rejoin then left them all in CHANNEL_ERROR with broadcast still arriving (0.9.2 round 4, S-1).
|
|
726
951
|
*/
|
|
727
952
|
private rejoinIfUnheard(state: ChannelState): void {
|
|
728
953
|
const held = state.joining ?? state.joined
|
|
729
954
|
if (!state.joinSent || held === undefined) return
|
|
730
955
|
const listeners = pgListenersOf(state)
|
|
731
|
-
if (listeners.length === 0
|
|
732
|
-
|
|
956
|
+
if (listeners.length === 0) return
|
|
957
|
+
if (held.rows && listeners.some((listener) => sameJoin(listener, held))) return
|
|
733
958
|
state.joinSent = false
|
|
734
|
-
|
|
735
|
-
notifyHandles(state, "SUBSCRIBING")
|
|
959
|
+
if (state.subscribed) notifyUnserved(state, "SUBSCRIBING")
|
|
960
|
+
else notifyHandles(state, "SUBSCRIBING")
|
|
736
961
|
this.sendSubscribe(state)
|
|
737
962
|
}
|
|
738
963
|
|
|
@@ -762,7 +987,10 @@ interface ChannelHandle {
|
|
|
762
987
|
pgListeners: PgListener[]
|
|
763
988
|
presenceListeners: Array<(event: { joins: PresenceEntry[]; leaves: PresenceEntry[] }) => void>
|
|
764
989
|
broadcastListeners: Map<string, Array<(payload: Record<string, unknown>) => void>>
|
|
765
|
-
|
|
990
|
+
refusalListeners: Array<(refusal: ChannelActionRefusal) => void>
|
|
991
|
+
statusCallback: ChannelStatusCallback | null
|
|
992
|
+
/** Whether this handle asked for `private: true`. */
|
|
993
|
+
private: boolean
|
|
766
994
|
}
|
|
767
995
|
|
|
768
996
|
/** One channel name's join on the socket, shared by every handle on that name. */
|
|
@@ -793,12 +1021,26 @@ interface ChannelState {
|
|
|
793
1021
|
* reply carrying any other ref answers a join since replaced or left, and is not this one's.
|
|
794
1022
|
*/
|
|
795
1023
|
joinRef: string | undefined
|
|
1024
|
+
/** The join sent on the current socket asked for `private: true`. */
|
|
1025
|
+
joinedPrivate: boolean
|
|
1026
|
+
/** The server took this channel back, and the client does not join it again until the app asks. */
|
|
1027
|
+
withdrawn: boolean
|
|
1028
|
+
/** The channel's private join was failed on this socket for want of server support. */
|
|
1029
|
+
unsupported: boolean
|
|
796
1030
|
/** Who the current socket has said is tracked on this channel, one entry per tracking socket. */
|
|
797
1031
|
members: PresenceEntry[]
|
|
798
1032
|
}
|
|
799
1033
|
|
|
800
|
-
/**
|
|
801
|
-
type
|
|
1034
|
+
/** The table, event and filter a row listener is for. */
|
|
1035
|
+
type RowTarget = Pick<PgListener, "schema" | "table" | "event" | "filter">
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* What a join registers on the server: the table whose changes it delivers, and which of them.
|
|
1039
|
+
* `rows` is whether it asked for them at all: a join for broadcast and presence alone is sent none.
|
|
1040
|
+
*/
|
|
1041
|
+
interface JoinTarget extends RowTarget {
|
|
1042
|
+
rows: boolean
|
|
1043
|
+
}
|
|
802
1044
|
|
|
803
1045
|
/**
|
|
804
1046
|
* What a join on `state` registers now: its oldest row listener's table, event and filter. With no
|
|
@@ -807,12 +1049,14 @@ type JoinTarget = Pick<PgListener, "schema" | "table" | "event" | "filter">
|
|
|
807
1049
|
*/
|
|
808
1050
|
function joinTargetOf(state: ChannelState): JoinTarget {
|
|
809
1051
|
const first = pgListenersOf(state)[0]
|
|
810
|
-
if (first !== undefined)
|
|
811
|
-
|
|
1052
|
+
if (first !== undefined) {
|
|
1053
|
+
return { schema: first.schema, table: first.table, event: first.event, filter: first.filter, rows: true }
|
|
1054
|
+
}
|
|
1055
|
+
return { ...parseChannelName(state.name), event: "*", filter: undefined, rows: false }
|
|
812
1056
|
}
|
|
813
1057
|
|
|
814
1058
|
/** Whether a listener is for exactly what a join registered. */
|
|
815
|
-
function sameJoin(listener:
|
|
1059
|
+
function sameJoin(listener: RowTarget, join: RowTarget): boolean {
|
|
816
1060
|
return listener.schema === join.schema && listener.table === join.table &&
|
|
817
1061
|
listener.event === join.event && listener.filter === join.filter
|
|
818
1062
|
}
|
|
@@ -834,15 +1078,20 @@ function pgListenersOf(state: ChannelState): PgListener[] {
|
|
|
834
1078
|
* `lobby-chat` carrying `table: "chat_message"` (which is what `useSubscription` exists to let you
|
|
835
1079
|
* write), registered against a table no change can match. The socket reported SUBSCRIBED and
|
|
836
1080
|
* delivered nothing, which is indistinguishable from a quiet table.
|
|
1081
|
+
*
|
|
1082
|
+
* `changes` says whether the join has a row listener. Only a row join has its table checked and is
|
|
1083
|
+
* sent row changes: broadcast and presence on `room:x` were refused as a join for room.x (R2-RB2).
|
|
837
1084
|
*/
|
|
838
|
-
function subscribeFrame(channel: string, target: JoinTarget, ref: string): Record<string, unknown> {
|
|
1085
|
+
function subscribeFrame(channel: string, target: JoinTarget, ref: string, asPrivate: boolean): Record<string, unknown> {
|
|
839
1086
|
return {
|
|
840
1087
|
type: "subscribe",
|
|
841
1088
|
channel,
|
|
842
1089
|
event: target.event,
|
|
843
1090
|
table: target.table,
|
|
844
1091
|
schema: target.schema,
|
|
1092
|
+
changes: target.rows,
|
|
845
1093
|
filter: filterFrame(target.filter),
|
|
1094
|
+
...(asPrivate && { private: true }),
|
|
846
1095
|
ref,
|
|
847
1096
|
}
|
|
848
1097
|
}
|
|
@@ -858,18 +1107,98 @@ function filterFrame(filter: string | undefined): Record<string, string> | undef
|
|
|
858
1107
|
return Object.keys(parsed).length > 0 ? parsed : undefined
|
|
859
1108
|
}
|
|
860
1109
|
|
|
861
|
-
/**
|
|
1110
|
+
/**
|
|
1111
|
+
* The server took the join `state` was waiting on: from now on its rows are that join's.
|
|
1112
|
+
*
|
|
1113
|
+
* Over a join it had acknowledged, the server tells the new join who is present all over again,
|
|
1114
|
+
* straight after this reply, so the members known now leave first. The handles the old join served
|
|
1115
|
+
* were never told otherwise than SUBSCRIBED, so only the others are told it now.
|
|
1116
|
+
*/
|
|
862
1117
|
function acknowledgeJoin(state: ChannelState): void {
|
|
1118
|
+
const replaced = state.subscribed
|
|
1119
|
+
if (replaced) {
|
|
1120
|
+
forgetMembers(state)
|
|
1121
|
+
notifyUnserved(state, "SUBSCRIBED")
|
|
1122
|
+
}
|
|
863
1123
|
state.subscribed = true
|
|
864
1124
|
if (state.joining !== undefined) state.joined = state.joining
|
|
865
1125
|
state.joining = undefined
|
|
866
1126
|
state.joinRef = undefined
|
|
867
|
-
notifyHandles(state, "SUBSCRIBED")
|
|
1127
|
+
if (!replaced) notifyHandles(state, "SUBSCRIBED")
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
/**
|
|
1131
|
+
* The server refused a rejoin, so the acknowledged join it was to replace still stands, and stays
|
|
1132
|
+
* the one this socket holds. Only a handle with a row listener it does not serve gets the refusal.
|
|
1133
|
+
*/
|
|
1134
|
+
function refuseRejoin(state: ChannelState, refusal: Error): void {
|
|
1135
|
+
state.joinSent = true
|
|
1136
|
+
state.joining = undefined
|
|
1137
|
+
state.joinRef = undefined
|
|
1138
|
+
notifyUnserved(state, "CHANNEL_ERROR", refusal)
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/**
|
|
1142
|
+
* Whether `join` delivers rows to every row listener of `handle`, as `handleChange` routes them: by
|
|
1143
|
+
* the join's table. Always, for a handle listening for none.
|
|
1144
|
+
*/
|
|
1145
|
+
function serves(join: JoinTarget | undefined, handle: ChannelHandle): boolean {
|
|
1146
|
+
return handle.pgListeners.every(
|
|
1147
|
+
(listener) => join?.rows === true && listener.schema === join.schema && listener.table === join.table,
|
|
1148
|
+
)
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
/** Whether any handle on the channel asked for `private: true`: the shared join then does. */
|
|
1152
|
+
function isPrivate(state: ChannelState): boolean {
|
|
1153
|
+
for (const handle of state.handles) if (handle.private) return true
|
|
1154
|
+
return false
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/**
|
|
1158
|
+
* The server removed this client from a channel it held: the token changed, a re-check ran, or the
|
|
1159
|
+
* declaration went. Every handle is told, and the client does not join again by itself, since the
|
|
1160
|
+
* server would refuse it again. The app joins again by subscribing again.
|
|
1161
|
+
*/
|
|
1162
|
+
function withdrawJoin(state: ChannelState, refusal: Error): void {
|
|
1163
|
+
state.subscribed = false
|
|
1164
|
+
state.joinSent = true
|
|
1165
|
+
state.withdrawn = true
|
|
1166
|
+
state.joined = undefined
|
|
1167
|
+
state.joining = undefined
|
|
1168
|
+
state.joinRef = undefined
|
|
1169
|
+
state.joinedPrivate = false
|
|
1170
|
+
forgetMembers(state)
|
|
1171
|
+
notifyHandles(state, "CHANNEL_ERROR", refusal)
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/** Hand a refused send, track or payload to the `onRefused` listeners of every handle on the channel. */
|
|
1175
|
+
function refuseAction(state: ChannelState | undefined, refusal: ChannelActionRefusal): void {
|
|
1176
|
+
if (state === undefined) return
|
|
1177
|
+
for (const handle of state.handles) {
|
|
1178
|
+
for (const listener of handle.refusalListeners) listener(refusal)
|
|
1179
|
+
}
|
|
868
1180
|
}
|
|
869
1181
|
|
|
870
1182
|
/** Tell each handle on a channel its status. */
|
|
871
|
-
function notifyHandles(state: ChannelState, status: ChannelStatus): void {
|
|
872
|
-
for (const handle of state.handles) handle.statusCallback?.(status)
|
|
1183
|
+
function notifyHandles(state: ChannelState, status: ChannelStatus, err?: Error | undefined): void {
|
|
1184
|
+
for (const handle of state.handles) handle.statusCallback?.(status, err)
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
/** Tell its status to each handle the join `state` holds does not serve. */
|
|
1188
|
+
function notifyUnserved(state: ChannelState, status: ChannelStatus, err?: Error | undefined): void {
|
|
1189
|
+
for (const handle of state.handles) {
|
|
1190
|
+
if (!serves(state.joined, handle)) handle.statusCallback?.(status, err)
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/** Every member this socket was told of leaves, as the next join is told them all again. */
|
|
1195
|
+
function forgetMembers(state: ChannelState): void {
|
|
1196
|
+
if (state.members.length === 0) return
|
|
1197
|
+
const leaves = state.members
|
|
1198
|
+
state.members = []
|
|
1199
|
+
for (const handle of state.handles) {
|
|
1200
|
+
for (const listener of handle.presenceListeners) listener({ joins: [], leaves })
|
|
1201
|
+
}
|
|
873
1202
|
}
|
|
874
1203
|
|
|
875
1204
|
/** `members` with one entry removed per leave: a user on two tabs is two entries and leaves twice. */
|