event-emitter
Eventsβ
CancellableEventEmitterβ
Defined in: packages/core/src/event-emitter.ts:34
Custom EventEmitter that supports cancellable events with sequential handler execution.
Features:
- Executes async handlers sequentially (one after another)
- Collects errors without stopping handler execution
- Supports stopPropagation to halt handler chain
- Returns execution results to emitters
Extendsβ
EventEmitter
Constructorsβ
Constructorβ
new CancellableEventEmitter(options?): CancellableEventEmitter;
Defined in: node_modules/.bun/@types+node@26.6.0/node_modules/@types/node/events.d.ts:54
Parametersβ
options?β
EventEmitterOptions
Returnsβ
Inherited fromβ
EventEmitter.constructor;
Methodsβ
emitAsync()β
emitAsync(eventName, event): Promise<EmitResult>;
Defined in: packages/core/src/event-emitter.ts:77
Emit an event asynchronously with cancellable event support.
Handlers are executed sequentially in registration order. If a handler throws an error, it's collected but execution continues. If event.propagationStopped becomes true, remaining handlers are skipped.
After all handlers execute, if the event has a defaultAction function and preventDefault() was not called, the default action is automatically executed.
Parametersβ
eventNameβ
string
Name of the event to emit
eventβ
ICancellableEvent
Cancellable event object with preventDefault() and stopPropagation() methods
Returnsβ
Promise<EmitResult>
Promise resolving to EmitResult with errors and cancellation status
Exampleβ
const events = getEvents();
const event = new SchemaLoadEvent({
schemaLocation: "/path/to/schema",
defaultAction: async () => {
// This runs automatically if not prevented
await loadSchema("/path/to/schema");
},
});
const { errors, defaultPrevented } = await events.emitAsync(
"beforeLoadSchema",
event,
);
if (errors.length > 0) {
console.error("Errors occurred:", errors);
}
EmitResultβ
Defined in: packages/core/src/event-emitter.ts:9
Result object returned when emitting a cancellable event.
Propertiesβ
defaultPreventedβ
defaultPrevented: boolean;
Defined in: packages/core/src/event-emitter.ts:20
Whether any handler called preventDefault() on the event. Only applicable if the event is cancellable.
errorsβ
errors: Error[];
Defined in: packages/core/src/event-emitter.ts:14
Array of errors that occurred during handler execution. Handlers continue executing even if previous handlers throw errors.
getEvents()β
function getEvents(): CancellableEventEmitter;
Defined in: packages/core/src/event-emitter.ts:172
Get the singleton event emitter instance.
Creates the instance on first call, then returns the same instance on subsequent calls. This ensures all parts of the application share the same event bus.
Returnsβ
The singleton CancellableEventEmitter instance
Exampleβ
// In generator.ts
const events = getEvents();
events.on("beforeLoadSchema", handler);
// In renderer.ts - same instance!
const events = getEvents();
await events.emitAsync("beforeLoadSchema", event);
resetEvents()β
function resetEvents(): void;
Defined in: packages/core/src/event-emitter.ts:196
Reset the event emitter singleton.
Removes all event listeners and clears the instance. The next call to getEvents() will create a fresh instance.
Important: This should only be used in tests to ensure test isolation.
Returnsβ
void
Exampleβ
// In test setup
afterEach(() => {
resetEvents();
jest.restoreAllMocks();
});