Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 35aeee5

Browse files
committed
fix: move back to an array solution (#467)
1 parent 64c42b8 commit 35aeee5

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

src/RealtimeChannel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ export default class RealtimeChannel {
279279
})
280280
} else {
281281
this.unsubscribe()
282+
this.state = CHANNEL_STATES.errored
282283
callback?.(
283284
REALTIME_SUBSCRIBE_STATES.CHANNEL_ERROR,
284285
new Error(
@@ -296,6 +297,7 @@ export default class RealtimeChannel {
296297
}
297298
})
298299
.receive('error', (error: { [key: string]: any }) => {
300+
this.state = CHANNEL_STATES.errored
299301
callback?.(
300302
REALTIME_SUBSCRIBE_STATES.CHANNEL_ERROR,
301303
new Error(

src/RealtimeClient.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const WORKER_SCRIPT = `
9191
export default class RealtimeClient {
9292
accessTokenValue: string | null = null
9393
apiKey: string | null = null
94-
channels: Set<RealtimeChannel> = new Set()
94+
channels: RealtimeChannel[] = new Array()
9595
endPoint: string = ''
9696
httpEndpoint: string = ''
9797
headers?: { [key: string]: string } = DEFAULT_HEADERS
@@ -262,7 +262,7 @@ export default class RealtimeClient {
262262
* Returns all created channels
263263
*/
264264
getChannels(): RealtimeChannel[] {
265-
return Array.from(this.channels)
265+
return this.channels
266266
}
267267

268268
/**
@@ -273,9 +273,11 @@ export default class RealtimeClient {
273273
channel: RealtimeChannel
274274
): Promise<RealtimeRemoveChannelResponse> {
275275
const status = await channel.unsubscribe()
276-
if (this.channels.size === 0) {
276+
this.channels = this.channels.filter((c) => c._joinRef !== channel._joinRef)
277+
if (this.channels.length === 0) {
277278
this.disconnect()
278279
}
280+
279281
return status
280282
}
281283

@@ -284,13 +286,10 @@ export default class RealtimeClient {
284286
*/
285287
async removeAllChannels(): Promise<RealtimeRemoveChannelResponse[]> {
286288
const values_1 = await Promise.all(
287-
Array.from(this.channels).map((channel) => {
288-
this.channels.delete(channel)
289-
return channel.unsubscribe()
290-
})
289+
this.channels.map((channel) => channel.unsubscribe())
291290
)
292291
this.disconnect()
293-
292+
this.channels = []
294293
return values_1
295294
}
296295

@@ -337,7 +336,8 @@ export default class RealtimeClient {
337336

338337
if (!exists) {
339338
const chan = new RealtimeChannel(`realtime:${topic}`, params, this)
340-
this.channels.add(chan)
339+
this.channels.push(chan)
340+
341341
return chan
342342
} else {
343343
return exists
@@ -480,7 +480,7 @@ export default class RealtimeClient {
480480
* @internal
481481
*/
482482
_leaveOpenTopic(topic: string): void {
483-
let dupChannel = Array.from(this.channels).find(
483+
let dupChannel = this.channels.find(
484484
(c) => c.topic === topic && (c._isJoined() || c._isJoining())
485485
)
486486
if (dupChannel) {
@@ -497,7 +497,7 @@ export default class RealtimeClient {
497497
* @internal
498498
*/
499499
_remove(channel: RealtimeChannel) {
500-
this.channels.delete(channel)
500+
this.channels = this.channels.filter((c) => c.topic !== channel.topic)
501501
}
502502

503503
/**

test/channel.test.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import RealtimeChannel from '../src/RealtimeChannel'
1717
import { Response } from '@supabase/node-fetch'
1818
import Worker from 'web-worker'
1919
import { Server, WebSocket } from 'mock-socket'
20+
import { CHANNEL_STATES } from '../src/lib/constants'
2021

2122
const defaultRef = '1'
2223
const defaultTimeout = 1000
@@ -303,19 +304,19 @@ describe('subscribe', () => {
303304

304305
test('unsubscribes to channel with incorrect server postgres_changes resp', () => {
305306
const unsubscribeSpy = sinon.spy(channel, 'unsubscribe')
306-
const cbSpy = sinon.spy()
307-
const func = () => {}
307+
const callbackSpy = sinon.spy()
308+
const dummyCallback = () => {}
308309

309310
channel.bindings.postgres_changes = [
310311
{
311312
type: 'postgres_changes',
312313
filter: { event: '*', schema: '*' },
313-
callback: func,
314+
callback: dummyCallback,
314315
},
315316
{
316317
type: 'postgres_changes',
317318
filter: { event: 'INSERT', schema: 'public', table: 'test' },
318-
callback: func,
319+
callback: dummyCallback,
319320
},
320321
{
321322
type: 'postgres_changes',
@@ -325,27 +326,28 @@ describe('subscribe', () => {
325326
table: 'test',
326327
filter: 'id=eq.1',
327328
},
328-
callback: func,
329+
callback: dummyCallback,
329330
},
330331
]
331-
channel.subscribe(cbSpy)
332332

333-
const cb = channel.bindings['chan_reply_1'][0].callback
334-
cb({
333+
channel.subscribe(callbackSpy)
334+
const replyCallback = channel.bindings['chan_reply_1'][0].callback
335+
replyCallback({
335336
status: 'ok',
336337
response: { postgres_changes: [{ id: 'abc', event: '*', schema: '*' }] },
337338
})
338339

339340
assert.ok(unsubscribeSpy.calledOnce)
340341
assert.ok(
341-
cbSpy.calledWith(
342+
callbackSpy.calledWith(
342343
'CHANNEL_ERROR',
343344
sinon.match({
344345
message:
345346
'mismatch between server and client bindings for postgres changes',
346347
})
347348
)
348349
)
350+
assert.equal(channel.state, CHANNEL_STATES.errored)
349351
})
350352

351353
test('can set timeout on joinPush', () => {
@@ -594,6 +596,7 @@ describe('joinPush', () => {
594596
helpers.receiveError()
595597

596598
assert.ok(spyError.calledOnce)
599+
assert.equal(channel.state, CHANNEL_STATES.errored)
597600
})
598601

599602
test("triggers receive('error') callback if error response already received", () => {
@@ -1113,7 +1116,7 @@ describe('leave', () => {
11131116
})
11141117

11151118
test("closes channel on 'ok' from server", () => {
1116-
const anotherChannel = socket.channel('another', { three: 'four' })
1119+
const anotherChannel = socket.channel('another')
11171120
assert.equal(socket.getChannels().length, 2)
11181121

11191122
channel.unsubscribe()

0 commit comments

Comments
 (0)