import { SearchEngine } from './engine/controller' ; import { getSearchRegExp } from '../../../../common/functionlity/functions.search.requests' ; import { IPCMessages as IPCElectronMessages , Subscription } from '../../services/service.electron' ; import { IRange } from '../../../../common/interfaces/interface.timerange' ; import ServiceElectron from '../../services/service.electron' ; import Logger from '../../tools/env.logger' ; import ControllerStreamProcessor from '../stream.main/controller' ; export default class ControllerStreamRanges { private _logger : Logger ; private _subscriptions : { [ key ] : Subscription | undefined ; } = { } ; private _searching : SearchEngine ; private _processor : ControllerStreamProcessor ; private _streamGuid : string ; constructor ( streamGuid , streamFile , stream ) { this . _streamGuid = streamGuid ; this . _processor = stream ; this . _logger = new Logger ( `ControllerStreamSearchRanges: ${ streamGuid }` ) ; this . _searching = new SearchEngine ( streamGuid , streamFile ) ; ServiceElectron . IPC . subscribe ( IPCElectronMessages . TimerangeSearchRequest , this . _ipc_onTimerangeSearchRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . TimerangeSearchRequest = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to render event "TimerangeSearchRequest" due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; } ) ; } public destroy ( ) { Object . keys ( this . _subscriptions ) . forEach ( ( key ) => { ( this . _subscriptions as any ) [ key ] . destroy ( ) ; } ) ; this . _searching . removeAllListeners ( ) ; return this . _searching . destroy ( ) ; } private _ipc_onTimerangeSearchRequest ( message , response ) { const request = message as IPCElectronMessages . TimerangeSearchRequest ; if ( this . _streamGuid !== request . session ) { return ; } if ( this . _processor . getStreamSize ( ) === 0 ) { return response ( new IPCElectronMessages . TimerangeSearchResponse ( { session : this . _streamGuid , ranges : [ ] , id : request . id , } ) ) ; } const measure = this . _logger . measure ( `searching` ) ; const regs = request . points . map ( _ => getSearchRegExp ( _ . request , _ . flags ) ) ; const errors = ( regs as any [ ] ) . filter ( _ => _ instanceof Error ) ; if ( errors . length !== 0 ) { const error = new Error ( `${ errors . map ( _ => _ . message ) }` ) ; return response ( new IPCElectronMessages . TimerangeSearchResponse ( { session : this . _streamGuid , ranges : [ ] , error : this . _logger . warn ( `Fail to get regular expression due error: ${ error . message }` ) , id : request . id , } ) ) ; } this . _searching . search ( request . id , request . format , { points : regs , strict : request . strict } ) . then ( ( ranges ) => { response ( new IPCElectronMessages . TimerangeSearchResponse ( { session : this . _streamGuid , ranges : ranges , id : request . id , } ) ) ; } ) . catch ( ( err ) => { response ( new IPCElectronMessages . TimerangeSearchResponse ( { session : this . _streamGuid , ranges : [ ] , error : this . _logger . warn ( `Fail to make a search due error: ${ err . message }` ) , id : request . id , } ) ) ; } ) . canceled ( ( ) => { response ( new IPCElectronMessages . TimerangeSearchResponse ( { session : this . _streamGuid , ranges : [ ] , id : request . id , } ) ) ; } ) . finally ( ( ) => { measure ( ) ; } ) ; } }