import Logger from '../../tools/env.logger' ; import ServiceElectron from '../service.electron' ; import ServiceStorage from '../service.storage' ; import { IPCMessages } from '../service.electron' ; import { IStorageScheme } from '../service.storage' ; import { IService } from '../../interfaces/interface.service' ; import { Subscription } from '../../tools/index' ; export const MAX_NUMBER_OF_RECENT_RECORDS = 15 ; export class ServiceTimestampFormatRecent implements IService { private _subscriptions : { [ key ] : Subscription ; } = { } ; private _logger : Logger = new Logger ( 'ServiceTimestampFormatRecent' ) ; public init ( ) { return new Promise ( ( resolve , reject ) => { ServiceElectron . IPC . subscribe ( IPCMessages . TimestampFormatRecentRequest , this . _ipc_TimestampFormatRecentRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . TimestampFormatRecentRequest = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to "FilterRecentRequest" due error: ${ error . message }.` ) ; } ) ; ServiceElectron . IPC . subscribe ( IPCMessages . TimestampFormatRecentAdd , this . _ipc_TimestampFormatRecentAdd . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . TimestampFormatRecentAdd = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to "FilterRecentRequest" due error: ${ error . message }.` ) ; } ) ; resolve ( ) ; } ) ; } public destroy ( ) { return new Promise ( ( resolve ) => { Object . keys ( this . _subscriptions ) . forEach ( ( key ) => { this . _subscriptions [ key ] . destroy ( ) ; } ) ; resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceTimestampFormatRecent' ; } public add ( format ) { const stored = ServiceStorage . get ( ) . get ( ) ; const formats = stored . recentDateTimeFormats . filter ( ( f ) => { return format !== f ; } ) ; if ( formats . length > MAX_NUMBER_OF_RECENT_RECORDS ) { formats . splice ( formats . length - 1 , 1 ) ; } formats . unshift ( format ) ; ServiceStorage . get ( ) . set ( { recentDateTimeFormats : formats , } ) . catch ( ( err ) => { this . _logger . error ( err . message ) ; } ) ; } public get ( ) { return new Promise ( ( resolve ) => { const stored = ServiceStorage . get ( ) . get ( ) ; resolve ( stored . recentDateTimeFormats ) ; } ) ; } public clear ( ) { ServiceStorage . get ( ) . set ( { recentFiles : [ ] , } ) . catch ( ( err ) => { this . _logger . error ( err . message ) ; } ) ; ServiceElectron . updateMenu ( ) ; } private _ipc_TimestampFormatRecentRequest ( request , response ) { const stored = ServiceStorage . get ( ) . get ( ) ; response ( new IPCMessages . TimestampFormatRecentResponse ( { formats : stored . recentDateTimeFormats , } ) ) . catch ( ( error ) => { this . _logger . warn ( error . message ) ; } ) ; } private _ipc_TimestampFormatRecentAdd ( request , response ) { const req = request as IPCMessages . TimestampFormatRecentAdd ; if ( req . format . trim ( ) === '' ) { return ; } this . add ( req . format ) ; } } export default ( new ServiceTimestampFormatRecent ( ) ) ;