Repository navigation
hx data Event Emitter
github-actions[bot] edited this page Jun 18, 2026
·
1 revision
A lightweight, TypeScript-typed pub/sub event emitter. Used internally by the reactive system, but also exported as a standalone utility.
import { EventEmitter } from '@hx/data';Subscribe to an event. Returns this for chaining.
const emitter = new EventEmitter();
emitter.on('click', (x, y) => {
console.log(`click at ${x}, ${y}`);
});Fire an event. Returns true if there were listeners, false otherwise.
emitter.emit('click', 100, 200); // "click at 100, 200" → returns true
emitter.emit('unknown'); // no listeners → returns falseRemove a specific listener.
const handler = (msg: string) => console.log(msg);
emitter.on('log', handler);
emitter.emit('log', 'hello'); // fires
emitter.off('log', handler);
emitter.emit('log', 'world'); // does NOT fireRegister a one-shot listener. Fires once then auto-removes.
emitter.once('init', () => {
console.log('runs only once');
});
emitter.emit('init'); // fires
emitter.emit('init'); // no listener → returns falseRemove all listeners. If no event name, clear everything.
emitter.on('a', () => {});
emitter.on('a', () => {});
emitter.on('b', () => {});
emitter.offAll('a'); // removes 'a' listeners only
emitter.offAll(); // removes everythingtype Listener = (...args: any[]) => void;All mutation methods return this:
emitter
.on('start', () => console.log('started'))
.on('stop', () => console.log('stopped'))
.emit('start');The reactive system uses EventEmitter internally for path-based event routing. You can use it independently for any pub/sub needs:
class Store {
private events = new EventEmitter();
subscribe(listener: () => void) {
return this.events.on('change', listener);
}
unsubscribe(listener: () => void) {
this.events.off('change', listener);
}
protected notify() {
this.events.emit('change');
}
}- Overview
- Common Concepts
- I18n
- Data Attributes
- Form Components
- Layout Components
- Display Components
- Overlay Components
- Structure Components
- Validation
- Hooks
- Utilities