import * as Toolkit from 'chipmunk.client.toolkit' ; import * as IPCElectronMessages from '../../../../../common/ipc/electron.ipc.messages/index' ; import ControllerPluginIPC from '../controller/controller.plugin.ipc' ; import ServiceElectronIpc from './service.electron.ipc' ; import { IService } from '../interfaces/interface.service' ; type TToken = string ; export class PluginsIPCService implements IService { private _logger : Toolkit . Logger = new Toolkit . Logger ( 'PluginsIPCService' ) ; private _ipcs : Map < TToken , ControllerPluginIPC > = new Map ( ) ; private _subscriptionPluginMessages : Toolkit . Subscription | undefined ; constructor ( ) { ServiceElectronIpc . subscribeOnPluginMessage ( this . _onPluginMessage . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptionPluginMessages = subscription ; } ) . catch ( ( subscribeError ) => { this . _logger . error ( `Error to subscribe to income plugin messages due error: ${ subscribeError . message }` ) ; } ) ; } public init ( ) { return new Promise ( ( resolve , reject ) => { resolve ( ) ; } ) ; } public getName ( ) { return 'PluginsIPCService' ; } public destroy ( ) { if ( this . _subscriptionPluginMessages !== undefined ) { this . _subscriptionPluginMessages . unsubscribe ( ) ; } } public addPlugin ( token , controller ) { if ( this . _ipcs . has ( token ) ) { return new Error ( this . _logger . error ( `Plugin with token "${ token }" was already added.` ) ) ; } this . _ipcs . set ( token , controller ) ; return undefined ; } public removePlugin ( token ) { const controller = this . _ipcs . get ( token ) ; if ( controller === undefined ) { return new Error ( this . _logger . warn ( `Fail to find plugin with token "${ token }".` ) ) ; } controller . destroy ( ) ; this . _ipcs . delete ( token ) ; return undefined ; } public sendToHost ( message , token , streamId ? ) { ServiceElectronIpc . sendToPluginHost ( message , token , streamId ) ; } public requestFromHost ( message , token , streamId ? ) { return ServiceElectronIpc . requestToPluginHost ( message , token , streamId ) ; } private _onPluginMessage ( message ) { this . _ipcs . forEach ( ( ipc , token ) => { if ( token === message . token ) { ipc . accept ( message . data ) ; } } ) ; } } export default new PluginsIPCService ( ) ;