import * as events from 'events' ; import { inject , injectable } from 'inversify' ; import * as apid from '../../../api' ; import ILogger from '../ILogger' ; import ILoggerModel from '../ILoggerModel' ; import IRuleEvent from './IRuleEvent' ; @ injectable ( ) class RuleEvent implements IRuleEvent { private log : ILogger ; private emitter : events . EventEmitter = new events . EventEmitter ( ) ; constructor ( @ inject ( 'ILoggerModel' ) logger ) { this . log = logger . getLogger ( ) ; } param ruleId public emitAdded ( ruleId ) { this . emitter . emit ( RuleEvent . ADDED_EVENT , ruleId ) ; } param ruleId public emitUpdated ( ruleId ) { this . emitter . emit ( RuleEvent . UPDATED_EVENT , ruleId ) ; } param ruleId public emitEnabled ( ruleId ) { this . emitter . emit ( RuleEvent . ENABLED_EVENT , ruleId ) ; } param ruleId public emitDisabled ( ruleId ) { this . emitter . emit ( RuleEvent . DISABLED_EVENT , ruleId ) ; } param ruleId public emitDeleted ( ruleId ) { this . emitter . emit ( RuleEvent . DELETED_EVENT , ruleId ) ; } param callback public setAdded ( callback ) { this . emitter . on ( RuleEvent . ADDED_EVENT , async ( ruleId ) => { try { await callback ( ruleId ) ; } catch ( err ) { this . log . system . error ( err ) ; } } ) ; } param callback public setUpdated ( callback ) { this . emitter . on ( RuleEvent . UPDATED_EVENT , async ( ruleId ) => { try { await callback ( ruleId ) ; } catch ( err ) { this . log . system . error ( err ) ; } } ) ; } param callback public setEnabled ( callback ) { this . emitter . on ( RuleEvent . ENABLED_EVENT , async ( ruleId ) => { try { await callback ( ruleId ) ; } catch ( err ) { this . log . system . error ( err ) ; } } ) ; } param callback public setDisabled ( callback ) { this . emitter . on ( RuleEvent . DISABLED_EVENT , async ( ruleId ) => { try { await callback ( ruleId ) ; } catch ( err ) { this . log . system . error ( err ) ; } } ) ; } param callback public setDeleted ( callback ) { this . emitter . on ( RuleEvent . DELETED_EVENT , async ( ruleId ) => { try { await callback ( ruleId ) ; } catch ( err ) { this . log . system . error ( err ) ; } } ) ; } } namespace RuleEvent { export const ADDED_EVENT = 'Added' ; export const UPDATED_EVENT = 'Updated' ; export const ENABLED_EVENT = 'Enabled' ; export const DISABLED_EVENT = 'Disabled_Event' ; export const DELETED_EVENT = 'Deleted_Event' ; } export default RuleEvent ;