Easy pattern to create events with subscriptions
.
npm i make-event
.
const onUpdate = Events.make<number>();
onUpdate((value) => {
console.log(value);
});
const onUpdate = Events.make();
// method 1
const unsubscribe = onUpdate(() => {
});
unsubscribe();
// method 2
const handler = () => {
console.log(true);
}
onUpdate(handler);
onUpdate.unsubscribe(handler);
const onUpdate = Events.make<number>();
onUpdate.invoke(10);
const onUpdate = Events.make();
onUpdate.clear();
.
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);