1.0.1 • Published 6 months ago
hmah-event-dispatcher v1.0.1
Lightweight event dispatcher with strict types
How to use
- Create an event map for the dispatcher in named-array Record
type TSomeClassEventMap = {
"nameChange": [name: string];
};
- Inherit your class from
EventDispatcher
class and provide yourTSomeClassEventMap
as generic type
import { EventDispatcher } from 'hmah-event-dispatcher';
// ...
class SomeClass extends EventDispatcher<TSomeClassEventMap> {
public _name: string;
public constructor(name: string) {
super()
this._name = name;
}
public get name() {
return this._name;
}
public set name(value: string) {
this._name = value;
// Look! We dispatch a primitive, not an object!
this.emit('nameChange', value);
}
}
- You can also compose event dispatcher (composition over inheritance principle)
class SomeClass {
// ...
private readonly _observer = new EventDispatcher<TSomeClassEventMap>();
public readonly on = this._observer.on;
public readonly off = this._observer.off;
private readonly emit = this._observer.emit;
}
- And use it in your code
const obj = new SomeClass("InitialName");
const onNameChange = (name: string) => console.log("Name changed to:", name);
obj.on("nameChange", onNameChange);
obj.name = "ChangedName"; // logs: Name changed to: ChangedName
obj.off("nameChange", onNameChange);
obj.name = "NotFiredName"; // No logs
Testing
Bun
required
bun test
See tests (src/tests/cat.test.ts)
Types
See types (src/EventDispatcher.d.ts)
License
MIT