import Debug from 'debug' export type types = 'error' | 'warning' const debug = Debug('openapi-enforcer-middleware:events') const handlers: { [key: string]: Array } = {} export function emit (type: types, ...args: Array) { debug('Event emitted: ' + type, ...args) process.nextTick(() => { if (handlers[type]) { handlers[type].forEach(f => { f(...args) }) } }) } export function on (type: types, handler: Function) { if (!handlers[type]) handlers[type] = [] handlers[type].push(handler) debug('Subscribed to event: ' + type + ' ' + handler.toString()) }