import { socketTypes } from './constants' ; import { State } from '../types/redux' ; import { createLogger } from '../log' ; const log = createLogger ( __filename ) ; export class SocketNotify { state : State | null ; sockets : Set < SocketIO . Socket > = new Set ( ) ; constructor ( state ) { this . state = state ; } register ( socket ) { if ( ! this . sockets . has ( socket ) ) { log . debug ( 'register socket %s' , socket . id ) ; } this . sockets . add ( socket ) ; if ( this . state !== null ) { socket . emit ( socketTypes . state , this . state ) ; } } unregister ( socket ) { if ( this . sockets . has ( socket ) ) { log . debug ( 'unregister socket %s' , socket . id ) ; } this . sockets . delete ( socket ) ; } onStateChange ( state ) { this . state = state ; this . sockets . forEach ( ( socket ) => { socket . emit ( socketTypes . state , this . state ) ; } ) ; } }