-
Notifications
You must be signed in to change notification settings - Fork 0
Emitter
Harsh Singh edited this page Jul 22, 2022
·
4 revisions
EventEmitter is a simple Event Emitter Implementation that is designed to be lightweight and simple.
const emitter = new Bottlecap.Emitter();
let health = 100;
const handleDamage = ({ attacker, damageAmt }) => {
health -= damageAmt;
console.log(`You were attacked by ${attacker} and it caused -${damageAmt} damage to your health`);
};
emitter.on('damage', handleDamage); // subscribe to damage event
emitter.emit('damage', { attacker: "Goblin", damageAmt: 20 }); // emit damage event
emitter.once('damage', () => console.log('First Damage'));
emitter.off('damage', handleDamage); // unsubscribe to damage event
emitter.destroy();