export interface OWFileListenerDelegate { onLineAdd ( line ) : void ; onLineRead ? ( line ) : void ; onError ( error ? ) : void ; } const RESTART_DELAY = 1000 * 10 ; interface OWFileListenerResult extends overwolf . Result { content : string ; info : string ; } export class OWFileListener { private restartHandle : any ; private path : string ; private skipToEnd : boolean ; constructor ( private readonly id , private readonly delegate ) { } public start ( path , skipToEnd = true ) { this . path = path ; this . skipToEnd = skipToEnd ; this . listen ( ) ; } public stop ( ) { overwolf . io . stopFileListener ( this . id ) ; } private restart ( ) { this . stop ( ) ; clearTimeout ( this . restartHandle ) ; this . restartHandle = setTimeout ( ( ) => this . listen ( ) , RESTART_DELAY ) ; } private listen ( ) { overwolf . io . listenOnFile ( this . id , this . path , { skipToEnd : this . skipToEnd } , this . onListenOnFile ) ; } private onListenOnFile = ( event ) : void => { if ( ! event . success || event . error ) { this . delegate . onError ( event . error ) ; this . restart ( ) ; return ; } if ( ! event . info ?. length ) { return ; } let info ; try { info = JSON . parse ( event . info ) ; } catch ( error ) { this . delegate . onError ( error ) ; this . restart ( ) ; return ; } if ( info . isNew ) { this . delegate . onLineAdd ( event . content ) ; } else { if ( this . delegate . onLineRead ) { this . delegate . onLineRead ( event . content ) ; } } } ; }