import * as Toolkit from 'chipmunk.client.toolkit' ; import { Observable , Subject } from 'rxjs' ; import { IService } from '../interfaces/interface.service' ; import { AControllerFileOptions } from '../interfaces/interface.controller.file.options' ; import { ControllerDltFileOptions } from '../controller/file.options/controller.file.dlt' ; import { Session } from '../controller/session/session' ; import { Storage } from '../controller/helpers/virtualstorage' ; import ServiceElectronIpc , { IPC , Subscription , TResponseFunc } from './service.electron.ipc' ; import EventsHubService from './standalone/service.eventshub' ; enum EFileTypes { dlt = 'dlt' } type TFileTypes = 'dlt' ; const CControllers = { [ EFileTypes . dlt ] : ControllerDltFileOptions , } ; const CMetaRegs = { [ EFileTypes . dlt ] : /^dlt/gi , } ; type TReopenCallback = ( ) => void ; export class FileOptionsService implements IService { private _subscriptions : { [ key ] : Subscription ; } = { } ; private _logger : Toolkit . Logger = new Toolkit . Logger ( 'FileOptionsService' ) ; private _subjects = { onFileOpenRequest : new Subject < void > ( ) , } ; constructor ( ) { } public init ( ) { return new Promise ( ( resolve ) => { this . _subscriptions . FileGetOptionsRequest = ServiceElectronIpc . subscribe ( IPC . FileGetOptionsRequest , this . _onRequest . bind ( this ) ) ; resolve ( ) ; } ) ; } public getName ( ) { return 'FileOptionsService' ; } public desctroy ( ) { return new Promise ( ( resolve ) => { Object . keys ( this . _subscriptions ) . forEach ( ( key ) => { this . _subscriptions [ key ] . destroy ( ) ; } ) ; resolve ( ) ; } ) ; } public getObservable ( ) { return { onFileOpenRequest : this . _subjects . onFileOpenRequest . asObservable ( ) , } ; } public hasOptions ( meta ) { return this . _getOptionsController ( meta ) !== undefined ; } public getReopenOptionsCallback < T > ( session , meta , file , storage ) { const classRef = this . _getOptionsController ( meta ) ; if ( classRef === undefined ) { return undefined ; } return ( ) => { const inst = new ( classRef as any ) ( ) ; inst . reopen ( file , storage . get ( ) ) . then ( ( opt ) => { session . resetSessionContent ( ) . then ( ( ) => { ServiceElectronIpc . request < IPC . FileOpenResponse > ( new IPC . FileOpenRequest ( { file : file , options : opt , session : session . getGuid ( ) , } ) , IPC . FileOpenResponse ) . then ( ( response ) => { if ( response . error ) { this . _logger . error ( `Fail reopen file due error: ${ response . error }` ) ; } storage . set ( opt ) ; } ) . catch ( ( openError ) => { this . _logger . error ( `Unexpected error during reopening file: ${ openError . message }` ) ; } ) ; } ) . catch ( ( error ) => { this . _logger . error ( `Fail to drop session content due error: ${ error . message }` ) ; } ) ; } ) . catch ( ( getOptionsError ) => { this . _logger . error ( `Fail to get options for file "${ file }" due error: ${ getOptionsError . message }` ) ; } ) ; } ; } private _getOptionsController ( meta ) { let alias ; Object . keys ( CMetaRegs ) . forEach ( ( key ) => { if ( ( CMetaRegs as any ) [ key ] !== undefined && meta . search ( ( CMetaRegs as any ) [ key ] ) !== - 1 ) { alias = key as EFileTypes ; } } ) ; if ( alias === undefined || CControllers [ alias ] === undefined ) { return undefined ; } return CControllers [ alias ] ; } private _onRequest ( request , response ) { this . _subjects . onFileOpenRequest . next ( ) ; EventsHubService . getSubject ( ) . onKeepScrollPrevent . next ( undefined ) ; const fileType = request . type ; if ( ( CControllers as any ) [ fileType ] === undefined ) { response ( new IPC . FileGetOptionsResponse ( { allowed : true , } ) ) . catch ( ( error ) => { this . _logger . error ( `Fail to send response due error: ${ error . message }` ) ; } ) ; return ; } const inst = new ( CControllers as any ) [ fileType ] ( request ) ; inst . getOptions ( request ) . then ( ( options ) => { return response ( new IPC . FileGetOptionsResponse ( { allowed : true , options : options , } ) ) . catch ( ( error ) => { this . _logger . error ( `Fail to send response for file "${ fileType }" due error: ${ error . message }` ) ; } ) ; } ) . catch ( ( getOptionsError ) => { this . _logger . error ( `Fail to get options for file "${ fileType }" due error: ${ getOptionsError . message }` ) ; return response ( new IPC . FileGetOptionsResponse ( { allowed : false , } ) ) . catch ( ( error ) => { this . _logger . error ( `Fail to send response due error: ${ error . message }` ) ; } ) ; } ) ; } } export default new FileOptionsService ( ) ;