- Notifications
You must be signed in to change notification settings - Fork0
snuffyDev/lil-emit
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
a typed 253 byte event emitter
Yes, another tiny event emitter library.
npm install --save lil-emit // npmpnpm install --save lil-emit // pnpmyarn add lil-emit // yarn
And then use!
import{emitter}from"lil-emit";interfaceMyEvents{click:[number,number];update:Record<string,unknown>;}// Create our event handlerconstee=emitter<MyEvents>();consthandler=(data)=>console.log(data);// Listen for our events to be fired// (It's best to avoid using lambda functions for the callback)ee.on("click",handler);ee.on("update",handler);// Dispatch our 'click' eventee.dispatch("click",[32,425]);// Stop listening to the 'click' eventee.off("click",handler);// ...or all eventsee.off("*");
That's all that's needed!
The following guide will reference this event handler function at different points:
functionhandler(a,b,c){console.log(a,b,c);}
Registers an event handler for a given name.
The callback has the type(...data) => void
, where(...data)
is the data being emitted.For example:
ee.on("test",handler);// or the leaky wayee.on("test",(a,b,c)=>console.log(a,b,c));
Invokes all listeners for the given event name.
To demonstrate, here's how you would dispatch to the example above:
ee.dispatch("test","One","Two","Three!");
Unregisters an event handler from an event.
// This won't work.ee.off("test",(a,b,c)=>console.log(a,b,c));// But this will!ee.off("test",handler);
You can also do.off("*")
to remove all event listeners, from all events.