import { strict as assert } from 'assert' ; import { createClient } from 'redis' ; export class Subscriber < T > { private redisUrl : string ; private channel : string ; private consumeFunc : ( msg : T ) => Promise < void > ; constructor ( consumeFunc , channel , redisUrl ) { this . consumeFunc = consumeFunc ; this . channel = channel ; this . redisUrl = redisUrl ; } async run ( ) { const client = createClient ( { url : this . redisUrl } ) ; client . on ( 'subscribe' , ( channel , count ) => { console . info ( `Subscribed to ${ channel } successfully! There are ${ count } subscribers on this channel so far.` ) ; } ) ; client . on ( 'message' , ( channel , message ) => { assert . equal ( channel , this . channel ) ; const msg = JSON . parse ( message ) as T ; this . consumeFunc ( msg ) ; } ) ; client . subscribe ( this . channel ) ; } }