import { Subscription , Subject , Observable } from 'rxjs' ; import { Session } from '../controller/session/session' ; import { CommonInterfaces } from '../interfaces/interface.common' ; import { IPC } from './service.electron.ipc' ; import { ControllerFileMergeSession } from '../controller/controller.file.merge.session' ; import { IService } from '../interfaces/interface.service' ; import { CGuids } from '../states/state.default.sidebar.apps' ; import EventsSessionService from './standalone/service.events.session' ; import SessionsService from './service.sessions.tabs' ; import SidebarSessionsService from './service.sessions.sidebar' ; import * as Toolkit from 'chipmunk.client.toolkit' ; export interface IFileInfo { path : string ; name : string ; parser : string ; preview : string ; size : number ; } export interface IMergeFile { path : string ; info : IFileInfo ; format : CommonInterfaces . Detect . ITimestampFormat ; } export interface IMergeFilesService { add ( files , session ) : void ; getController ( session ? ) : ControllerFileMergeSession | undefined ; closeSidebarView ( ) : void ; } export class MergeFilesService implements IService , IMergeFilesService { private _subscriptions : { [ key ] : Subscription ; } = { } ; private _logger : Toolkit . Logger = new Toolkit . Logger ( 'MergeFilesService' ) ; private _controllers : Map < string , ControllerFileMergeSession > = new Map ( ) ; constructor ( ) { this . _subscriptions . onSessionChange = EventsSessionService . getObservable ( ) . onSessionChange . subscribe ( this . _onSessionChange . bind ( this ) ) ; this . _subscriptions . onSessionClosed = EventsSessionService . getObservable ( ) . onSessionClosed . subscribe ( this . _onSessionClosed . bind ( this ) ) ; } public init ( ) { return new Promise ( ( resolve ) => { resolve ( ) ; } ) ; } public getName ( ) { return 'MergeFilesService' ; } public add ( files , session ) { const controller = this . _controllers . get ( session ) ; if ( controller === undefined ) { this . _logger . error ( `Fail to find ControllerFileMergeSession for session: ${ session }` ) ; return ; } controller . add ( files . map ( ( file ) => { return file . path ; } ) ) . catch ( ( addErr ) => { this . _logger . error ( `Fail add files to merge controller due error: ${ addErr . message }` ) ; } ) ; } public getController ( session ? ) { if ( session === undefined ) { const controller = SessionsService . getActive ( ) ; if ( controller === undefined ) { return undefined ; } else { session = controller . getGuid ( ) ; } } return this . _controllers . get ( session ) ; } public closeSidebarView ( ) { SidebarSessionsService . remove ( CGuids . merging ) ; } private _onSessionChange ( session ) { if ( session === undefined ) { return ; } if ( this . _controllers . has ( session . getGuid ( ) ) ) { return ; } this . _controllers . set ( session . getGuid ( ) , new ControllerFileMergeSession ( session . getGuid ( ) ) ) ; } private _onSessionClosed ( guid ) { const controller = this . _controllers . get ( guid ) ; if ( controller === undefined ) { return ; } controller . destroy ( ) . catch ( ( destroyErr ) => { this . _logger . error ( `Fail to destroy controller for "${ guid }" due errr: ${ destroyErr . message }` ) ; } ) . finally ( ( ) => { this . _controllers . delete ( guid ) ; } ) ; } } export default new MergeFilesService ( ) ;