import { AppMasterEmitterType,AppMasterEmitterConfig,TypedEvent } from "./types"; import * as ob from "@onebro/oba-common"; import { EventEmitter } from "events"; export interface AppMasterEmitter extends AppMasterEmitterType {} export class AppMasterEmitter { get history(){return this._history;} get values(){return this._values;} get listeners(){return this._emitter.eventNames();} print(s?:k){ob.info(s?({[s]:this[s]}):this);} get(name?:k){return name?this._values[name]:this.values;} constructor(config:AppMasterEmitterConfig){ this._history = []; this._values = {}; this._emitter = new EventEmitter(); this.on = (s,l) => this._emitter.on(s as string,l); this.emit = (s,v) => { const event:TypedEvent = {[s]:v}; this._history.unshift({event,time:new Date()}); this._values[s] = v; this._emitter.emit(s as string,v);}; process.on("SIGUSR2",() => ob.warn("SIGUSR2") && this.emit("shutdown",true)); process.on("SIGINT",() => ob.warn("SIGINT") && this.emit("shutdown",true)); process.on("SIGTERM",() => ob.warn("SIGTERM") && this.emit("shutdown",true)); process.on("exit",() => ob.warn("exit") && this.emit("shutdown",false));}} export default AppMasterEmitter;