Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/transmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ export class Transmit<Context extends unknown> {
await this.#bus?.disconnect()
}

getSubscribersFor(channel: string) {
const subscribers = this.#manager.findByChannel(channel)
return Array.from(subscribers).map((subscriber) => subscriber.getUid())
}

#ping() {
for (const [stream] of this.#manager.getAllSubscribers()) {
stream.writeMessage({ data: { channel: '$$transmit/ping', payload: {} } })
Expand Down
48 changes: 48 additions & 0 deletions tests/transmit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,52 @@ test.group('Transmit', () => {

assert.isTrue(dataReceived)
})

test('should return all subscribers for a channel', async ({ assert }) => {
const transport = makeTransport()
const transmit = makeTransmitWithTransport(transport)

const stream1 = makeStream(transmit)
const stream2 = makeStream(transmit)
const stream3 = makeStream(transmit)
const stream4 = makeStream(transmit)
const stream5 = makeStream(transmit)

await transmit.subscribe({
uid: stream1.getUid(),
channel: 'channel1',
})

await transmit.subscribe({
uid: stream2.getUid(),
channel: 'channel1',
})

await transmit.subscribe({
uid: stream3.getUid(),
channel: 'channel2',
})

await transmit.subscribe({
uid: stream4.getUid(),
channel: 'channel2',
})

await transmit.subscribe({
uid: stream5.getUid(),
channel: 'channel2',
})

const uuidsChannel1 = transmit.getSubscribersFor('channel1')
const uuidsChannel2 = transmit.getSubscribersFor('channel2')

assert.lengthOf(uuidsChannel1, 2)
assert.equal(uuidsChannel1[0], stream1.getUid())
assert.equal(uuidsChannel1[1], stream2.getUid())

assert.lengthOf(uuidsChannel2, 3)
assert.equal(uuidsChannel2[0], stream3.getUid())
assert.equal(uuidsChannel2[1], stream4.getUid())
assert.equal(uuidsChannel2[2], stream5.getUid())
})
})