async () => { const remote = await emitter.waitFor('connect'); emitter.defineStates('connect'); emitter.emit('connect', remote); // ... somewhere else or in another file... };
import * as NgEmitter from 'nextgen-events'; import * as WebSocket from 'ws'; const emitter = new NgEmitter(); // Normal listener emitter.on('message', (message: any) => {}); // One time listener: emitter.once('close', () => {}); // The error listener: if it is not defined, the error event will throw an exception emitter.on('error', (error: any) => {}); emitter.emit('message', 'Hello world!'); // ... emitter.on('connection', { context: 'ctx', fn: (stream: any) => {}, }); emitter.on('close', { context: 'ctx', fn: () => { // Destroy the context and all listeners tied to it: emitter.destroyListenerContext('ctx'); }, }); emitter.on('erroHr', { context: 'ctx',