diff --git a/src/transmit.ts b/src/transmit.ts index dc6e6ab..4e9a3c0 100644 --- a/src/transmit.ts +++ b/src/transmit.ts @@ -235,6 +235,11 @@ export class Transmit { 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: {} } }) diff --git a/tests/transmit.spec.ts b/tests/transmit.spec.ts index d8bb387..1b65c98 100644 --- a/tests/transmit.spec.ts +++ b/tests/transmit.spec.ts @@ -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()) + }) })