Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

a fully typed event emitter, in 253 bytes

NotificationsYou must be signed in to change notification settings

snuffyDev/lil-emit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

a typed 253 byte event emitter

Yes, another tiny event emitter library.

Install

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!

Guide

The following guide will reference this event handler function at different points:

functionhandler(a,b,c){console.log(a,b,c);}

.on(name, callback)

Registers an event handler for a given name.

Callback

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));

.dispatch(name, ...data)

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!");

.off(name, callback)

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.

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp