Skip to content

Commit ccb1e17

Browse files
authored
feat(transmit): add an api to get subscribers' uuid for a channel (#23)
* feat(transmit): getAllSubscribersUUIDs * refacto: method name * fix(tests): method name * refacto(tests)
1 parent 4832001 commit ccb1e17

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/transmit.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ export class Transmit<Context extends unknown> {
235235
await this.#bus?.disconnect()
236236
}
237237

238+
getSubscribersFor(channel: string) {
239+
const subscribers = this.#manager.findByChannel(channel)
240+
return Array.from(subscribers).map((subscriber) => subscriber.getUid())
241+
}
242+
238243
#ping() {
239244
for (const [stream] of this.#manager.getAllSubscribers()) {
240245
stream.writeMessage({ data: { channel: '$$transmit/ping', payload: {} } })

tests/transmit.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,52 @@ test.group('Transmit', () => {
398398

399399
assert.isTrue(dataReceived)
400400
})
401+
402+
test('should return all subscribers for a channel', async ({ assert }) => {
403+
const transport = makeTransport()
404+
const transmit = makeTransmitWithTransport(transport)
405+
406+
const stream1 = makeStream(transmit)
407+
const stream2 = makeStream(transmit)
408+
const stream3 = makeStream(transmit)
409+
const stream4 = makeStream(transmit)
410+
const stream5 = makeStream(transmit)
411+
412+
await transmit.subscribe({
413+
uid: stream1.getUid(),
414+
channel: 'channel1',
415+
})
416+
417+
await transmit.subscribe({
418+
uid: stream2.getUid(),
419+
channel: 'channel1',
420+
})
421+
422+
await transmit.subscribe({
423+
uid: stream3.getUid(),
424+
channel: 'channel2',
425+
})
426+
427+
await transmit.subscribe({
428+
uid: stream4.getUid(),
429+
channel: 'channel2',
430+
})
431+
432+
await transmit.subscribe({
433+
uid: stream5.getUid(),
434+
channel: 'channel2',
435+
})
436+
437+
const uuidsChannel1 = transmit.getSubscribersFor('channel1')
438+
const uuidsChannel2 = transmit.getSubscribersFor('channel2')
439+
440+
assert.lengthOf(uuidsChannel1, 2)
441+
assert.equal(uuidsChannel1[0], stream1.getUid())
442+
assert.equal(uuidsChannel1[1], stream2.getUid())
443+
444+
assert.lengthOf(uuidsChannel2, 3)
445+
assert.equal(uuidsChannel2[0], stream3.getUid())
446+
assert.equal(uuidsChannel2[1], stream4.getUid())
447+
assert.equal(uuidsChannel2[2], stream5.getUid())
448+
})
401449
})

0 commit comments

Comments
 (0)