import { PulsarService } from '../network/pulsar.service' ; import { Database } from './Database' ; import { IndexdbDatabase } from './IndexdbDatabase' ; import { Pulsar } from './Pulsar' ; import { RabbitMq } from './RabbitMq' ; import { ILogDatabase } from './ILogDatabase.model' ; export abstract class LogsStrategy { protected dbDistant : Pulsar ; protected dbLocal : Database ; protected docKey : string ; protected logId : number ; constructor ( docKey ) { this . docKey = docKey ; this . logId = parseInt ( window . localStorage . getItem ( 'logid-' + this . docKey ) , 10 ) ; if ( isNaN ( this . logId ) ) { this . logId = 0 ; window . localStorage . setItem ( 'logid-' + this . docKey , '0' ) ; } this . dbLocal = new IndexdbDatabase ( ) ; this . dbLocal . init ( 'muteLogs-' + this . docKey ) ; this . dbDistant = new Pulsar ( this . docKey ) ; } setStreamLogsPulsar ( pulsarService ) { this . dbDistant . subscribeToWs ( pulsarService ) ; } public setShareLogs ( share , state ) { const stateVector = { } ; state . forEach ( ( v , k ) => { stateVector [ k ] = v ; } ) ; if ( share ) { window . localStorage . setItem ( 'shareLogs-on-' + this . docKey , JSON . stringify ( stateVector ) ) ; } else { window . localStorage . setItem ( 'shareLogs-off-' + this . docKey , JSON . stringify ( stateVector ) ) ; } } public getLocalLogs ( ) { return new Promise ( ( resolve , reject ) => { this . dbLocal . get ( ) . then ( ( obj ) => { resolve ( obj as object [ ] ) ; } ) . catch ( ( err ) => reject ( err ) ) ; } ) ; } protected useLogId ( ) { this . logId ++ ; window . localStorage . setItem ( 'logid-' + this . docKey , this . logId + '' ) ; return this . logId - 1 ; } abstract sendLogs ( obj , share ) ; }