make-event
v1.0.1
Published
Easy pattern to create events with subscriptions
Downloads
163
Maintainers
Readme
🧩 Make event
Easy pattern to create events with subscriptions
.
Install
npm i make-event
.
Usage
Subscribe to event
const onUpdate = Events.make<number>();
onUpdate((value) => {
console.log(value);
});
Unsubcsribe from event
const onUpdate = Events.make();
// method 1
const unsubscribe = onUpdate(() => {
});
unsubscribe();
// method 2
const handler = () => {
console.log(true);
}
onUpdate(handler);
onUpdate.unsubscribe(handler);
Invoke event
const onUpdate = Events.make<number>();
onUpdate.invoke(10);
Remove all handlers
const onUpdate = Events.make();
onUpdate.clear();
.
Example
import { Events } from 'make-event';
class Player {
public readonly onJump = Events.make<humber>();
public jump(height: number) {
this.onJump.invoke(height);
}
}
// ...
const player = new Player();
player.onJump((height) => {
console.log('onJump1', height);
});
const unsubscribe = player.onJump((height) => {
console.log('onJump2', height);
});
unsubscribe();
player.jump(10);