Skip to content

Commit 03756c7

Browse files
committed
refactor: remove unused subscribe/unsubscribe transport synchronization
1 parent 420a9bc commit 03756c7

3 files changed

Lines changed: 8 additions & 92 deletions

File tree

src/transmit.ts

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,11 @@ export interface TransmitLifecycleHooks<Context> {
2323
unsubscribe: { uid: string; channel: string; context: Context }
2424
}
2525

26-
type TransmitMessage =
27-
| {
28-
type: typeof TransportMessageType.Broadcast
29-
channel: string
30-
payload: Broadcastable
31-
}
32-
| {
33-
type: typeof TransportMessageType.Subscribe
34-
channel: string
35-
payload: { uid: string }
36-
}
37-
| {
38-
type: typeof TransportMessageType.Unsubscribe
39-
channel: string
40-
payload: { uid: string }
41-
}
26+
type TransmitMessage = {
27+
type: typeof TransportMessageType.Broadcast
28+
channel: string
29+
payload: Broadcastable
30+
}
4231

4332
export class Transmit<Context extends unknown> {
4433
/**
@@ -82,15 +71,7 @@ export class Transmit<Context extends unknown> {
8271

8372
// Subscribe to the transport channel and handle incoming messages
8473
void this.#bus?.subscribe<TransmitMessage>(this.#transportChannel, (message) => {
85-
const { type, channel, payload } = message
86-
87-
if (type === TransportMessageType.Broadcast) {
88-
void this.#broadcastLocally(channel, payload)
89-
} else if (type === TransportMessageType.Subscribe) {
90-
void this.#subscribeLocally({ uid: payload.uid, channel })
91-
} else if (type === TransportMessageType.Unsubscribe) {
92-
void this.#unsubscribeLocally({ uid: payload.uid, channel })
93-
}
74+
void this.#broadcastLocally(message.channel, message.payload)
9475
})
9576

9677
// Start the ping interval if configured
@@ -133,13 +114,6 @@ export class Transmit<Context extends unknown> {
133114
this.#manager.authorize(channel, callback)
134115
}
135116

136-
#subscribeLocally(params: Omit<SubscribeParams<Context>, 'onSubscribe'>) {
137-
return this.#manager.subscribe({
138-
...params,
139-
skipAuthorization: true,
140-
})
141-
}
142-
143117
subscribe(params: Omit<SubscribeParams<Context>, 'onSubscribe'>) {
144118
return this.#manager.subscribe({
145119
...params,
@@ -149,22 +123,10 @@ export class Transmit<Context extends unknown> {
149123
channel,
150124
context,
151125
})
152-
153-
void this.#bus?.publish(this.#transportChannel, {
154-
type: TransportMessageType.Subscribe,
155-
channel,
156-
payload: { uid },
157-
})
158126
},
159127
})
160128
}
161129

162-
#unsubscribeLocally(params: Omit<UnsubscribeParams<Context>, 'onUnsubscribe'>) {
163-
return this.#manager.unsubscribe({
164-
...params,
165-
})
166-
}
167-
168130
unsubscribe(params: Omit<UnsubscribeParams<Context>, 'onUnsubscribe'>) {
169131
return this.#manager.unsubscribe({
170132
...params,
@@ -174,12 +136,6 @@ export class Transmit<Context extends unknown> {
174136
channel,
175137
context,
176138
})
177-
178-
void this.#bus?.publish(this.#transportChannel, {
179-
type: TransportMessageType.Unsubscribe,
180-
channel,
181-
payload: { uid },
182-
})
183139
},
184140
})
185141
}

src/transport_message_type.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@
77

88
export const TransportMessageType = {
99
Broadcast: 1,
10-
Subscribe: 2,
11-
Unsubscribe: 3,
1210
} as const

tests/transmit.spec.ts

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -315,44 +315,6 @@ test.group('Transmit', () => {
315315
assert.lengthOf(transport.transport.receivedMessages, 0)
316316
})
317317

318-
test('should broadcast to the bus when a client subscribe to a channel', async ({ assert }) => {
319-
const transport = makeTransport()
320-
const transmit = makeTransmitWithTransport(transport)
321-
makeTransmitWithTransport(transport)
322-
323-
const stream = makeStream(transmit)
324-
325-
await transmit.subscribe({
326-
uid: stream.getUid(),
327-
channel: 'channel1',
328-
})
329-
330-
assert.lengthOf(transport.transport.receivedMessages, 1)
331-
assert.equal(transport.transport.receivedMessages[0].type, TransportMessageType.Subscribe)
332-
})
333-
334-
test('should broadcast to the bus when a client unsubscribe a channel', async ({ assert }) => {
335-
const transport = makeTransport()
336-
const transmit = makeTransmitWithTransport(transport)
337-
338-
makeTransmitWithTransport(transport)
339-
340-
const stream = makeStream(transmit)
341-
342-
await transmit.subscribe({
343-
uid: stream.getUid(),
344-
channel: 'channel1',
345-
})
346-
347-
await transmit.unsubscribe({
348-
uid: stream.getUid(),
349-
channel: 'channel1',
350-
})
351-
352-
assert.lengthOf(transport.transport.receivedMessages, 2)
353-
assert.equal(transport.transport.receivedMessages[1].type, TransportMessageType.Unsubscribe)
354-
})
355-
356318
test('should broadcast to the bus when sending a message', async ({ assert }) => {
357319
const transport = makeTransport()
358320
const transmit = makeTransmitWithTransport(transport)
@@ -367,8 +329,8 @@ test.group('Transmit', () => {
367329

368330
transmit.broadcast('channel1', { message: 'hello' })
369331

370-
assert.lengthOf(transport.transport.receivedMessages, 2)
371-
assert.equal(transport.transport.receivedMessages[1].type, TransportMessageType.Broadcast)
332+
assert.lengthOf(transport.transport.receivedMessages, 1)
333+
assert.equal(transport.transport.receivedMessages[0].type, TransportMessageType.Broadcast)
372334
})
373335

374336
test('second instance should receive the broadcasted message', async ({ assert }) => {

0 commit comments

Comments
 (0)