( channel: Channel, type: keyof IPublishChannels[Channel], callback: (event: IPublishChannels[Channel][typeof type]) => void, )
| 43 | } |
| 44 | |
| 45 | export function subscribeToPublishedEvent< |
| 46 | Channel extends keyof IPublishChannels, |
| 47 | >( |
| 48 | channel: Channel, |
| 49 | type: keyof IPublishChannels[Channel], |
| 50 | callback: (event: IPublishChannels[Channel][typeof type]) => void, |
| 51 | ) { |
| 52 | const subscribeChannel = getSubscribeChannel(channel, type); |
| 53 | getRedisSub().subscribe(subscribeChannel); |
| 54 | |
| 55 | const message = (messageChannel: string, message: string) => { |
| 56 | if (subscribeChannel === messageChannel) { |
| 57 | const event = parsePublishedEvent(channel, type, message); |
| 58 | if (event) { |
| 59 | callback(event); |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | getRedisSub().on('message', message); |
| 65 | |
| 66 | return () => { |
| 67 | getRedisSub().unsubscribe(subscribeChannel); |
| 68 | getRedisSub().off('message', message); |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | export function psubscribeToPublishedEvent( |
| 73 | pattern: string, |
no test coverage detected