Skip to content

Commit acef0fc

Browse files
committed
Await on redis.subscribe() to ensure that the method returns only after Redis has acknowledged the registration of the subscriber.
1 parent 8130489 commit acef0fc

4 files changed

Lines changed: 11 additions & 15 deletions

File tree

src/bus.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ export class Bus {
5454
await this.processErrorRetryQueue()
5555
}
5656

57-
subscribe<T extends Serializable>(channel: string, handler: SubscribeHandler<T>) {
57+
async subscribe<T extends Serializable>(channel: string, handler: SubscribeHandler<T>) {
5858
debug(`subscribing to channel ${channel}`)
5959

60-
return this.#transport.subscribe(channel, async (message) => {
60+
return await this.#transport.subscribe(channel, async (message) => {
6161
debug('received message %j from bus', message)
6262
// @ts-expect-error - TODO: Weird typing issue
6363
handler(message)
@@ -94,7 +94,7 @@ export class Bus {
9494
return this.#transport.disconnect()
9595
}
9696

97-
unsubscribe(channel: string) {
98-
return this.#transport.unsubscribe(channel)
97+
async unsubscribe(channel: string) {
98+
return await this.#transport.unsubscribe(channel)
9999
}
100100
}

src/bus_manager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ export class BusManager<KnownTransports extends Record<string, TransportConfig>>
5858
return this.use().publish(channel, message)
5959
}
6060

61-
subscribe<T extends Serializable>(channel: string, handler: SubscribeHandler<T>) {
62-
return this.use().subscribe(channel, handler)
61+
async subscribe<T extends Serializable>(channel: string, handler: SubscribeHandler<T>) {
62+
return await this.use().subscribe(channel, handler)
6363
}
6464

65-
unsubscribe(channel: string) {
66-
return this.use().unsubscribe(channel)
65+
async unsubscribe(channel: string) {
66+
return await this.use().unsubscribe(channel)
6767
}
6868

6969
disconnect() {

src/transports/redis.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ export class RedisTransport implements Transport {
8989
channel: string,
9090
handler: SubscribeHandler<T>
9191
): Promise<void> {
92-
this.#subscriber.subscribe(channel, (err) => {
93-
if (err) {
94-
throw err
95-
}
96-
})
92+
await this.#subscriber.subscribe(channel)
9793

9894
const event = this.#useMessageBuffer ? 'messageBuffer' : 'message'
9995
this.#subscriber.on(event, (receivedChannel: Buffer | string, message: Buffer | string) => {

test_helpers/chaos_transport.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export class ChaosTransport implements Transport {
5959
return this.#innerTransport.subscribe(channel, handler)
6060
}
6161

62-
unsubscribe(channel: string) {
63-
return this.#innerTransport.unsubscribe(channel)
62+
async unsubscribe(channel: string) {
63+
return await this.#innerTransport.unsubscribe(channel)
6464
}
6565

6666
disconnect() {

0 commit comments

Comments
 (0)