Skip to content

Commit 43a6b0f

Browse files
committed
fix: preserve falsy payload values in broadcast
1 parent f861371 commit 43a6b0f

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/transmit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ export class Transmit<Context extends unknown> {
161161
}
162162

163163
broadcast(channel: string, payload?: Broadcastable) {
164-
if (!payload) {
165-
payload = {}
164+
if (payload === undefined) {
165+
payload = null
166166
}
167167

168168
void this.#bus?.publish(this.#transportChannel, {

tests/transmit.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,35 @@ test.group('Transmit', () => {
361361
assert.isTrue(dataReceived)
362362
})
363363

364+
test('should broadcast falsy payloads without converting them to empty object', async ({
365+
assert,
366+
}) => {
367+
const transmit = new Transmit({
368+
transport: null,
369+
})
370+
371+
const stream = makeStream(transmit)
372+
373+
await transmit.subscribe({
374+
uid: stream.getUid(),
375+
channel: 'channel1',
376+
})
377+
378+
const receivedPayloads: any[] = []
379+
stream.on('data', (message: any) => {
380+
if (message === '\n') return
381+
382+
const parsed = JSON.parse(message.replace('data: ', ''))
383+
receivedPayloads.push(parsed.payload)
384+
})
385+
386+
transmit.broadcast('channel1', 0)
387+
transmit.broadcast('channel1', false)
388+
transmit.broadcast('channel1', '')
389+
390+
assert.deepEqual(receivedPayloads, [0, false, ''])
391+
})
392+
364393
test('should return all subscribers for a channel', async ({ assert }) => {
365394
const transport = makeTransport()
366395
const transmit = makeTransmitWithTransport(transport)

0 commit comments

Comments
 (0)