Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork49
Simple and tiny (107 bytes) event emitter library for JavaScript
License
ai/nanoevents
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Simple and tiny event emitter library for JavaScript.
- Only108 bytes (minified and brotlied).It usesSize Limit to control size.
- The
on
method returnsunbind
function. You don’t need to savecallback to variable forremoveListener
. - TypeScript and ES modules support.
- No aliases, just
emit
andon
methods.No Node.jsEventEmitter compatibility.
import{createNanoEvents}from'nanoevents'constemitter=createNanoEvents()constunbind=emitter.on('tick',volume=>{summary+=volume})emitter.emit('tick',2)summary//=> 2unbind()emitter.emit('tick',2)summary//=> 2
Made atEvil Martians, product consulting fordeveloper tools.
- Table of Contents
- Install
- TypeScript
- Mixing to Object
- Add Listener
- Remove Listener
- Execute Listeners
- Events List
- Once
- Remove All Listeners
npm install nanoevents
Nano Events accepts interface with event nameto listener argument types mapping.
interfaceEvents{set:(name:string,count:number)=>void,tick:()=>void}constemitter=createNanoEvents<Events>()// Correct calls:emitter.emit('set','prop',1)emitter.emit('tick')// Compilation errors:emitter.emit('set','prop','1')emitter.emit('tick',2)
Because Nano Events API has only just 2 methods,you could just create proxy methods in your classor encapsulate them entirely.
classTicker{constructor(){this.emitter=createNanoEvents()this.internal=setInterval(()=>{this.emitter.emit('tick')},100)}stop(){clearInterval(this.internal)this.emitter.emit('stop')}on(event,callback){returnthis.emitter.on(event,callback)}}
With Typescript:
import{createNanoEvents,Emitter}from"nanoevents"interfaceEvents{start:(startedAt:number)=>void}classTicker{emitter:Emitterconstructor(){this.emitter=createNanoEvents<Events>()}on<EextendskeyofEvents>(event:E,callback:Events[E]){returnthis.emitter.on(event,callback)}}
Useon
method to add listener for specific event:
emitter.on('tick',number=>{console.log(number)})emitter.emit('tick',1)// Prints 1emitter.emit('tick',5)// Prints 5
In case of your listener relies on some particular context(if it usesthis
within itself) you have to bind requiredcontext explicitly before passing function in as a callback.
varapp={userId:1,getListener(){return()=>{console.log(this.userId)}}}emitter.on('print',app.getListener())
Note: binding with use of the.bind()
method won’t work as you might expectand therefore is not recommended.
Methodson
returnsunbind
function. Call it and this listenerwill be removed from event.
constunbind=emitter.on('tick',number=>{console.log('on '+number)})emitter.emit('tick',1)// Prints "on 1"unbind()emitter.emit('tick',2)// Prints nothing
Methodemit
will execute all listeners. First argument is event name, otherswill be passed to listeners.
emitter.on('tick',(a,b)=>{console.log(a,b)})emitter.emit('tick',1,'one')// Prints 1, 'one'
You can get used events list byevents
property.
constunbind=emitter.on('tick',()=>{})emitter.events//=> { tick: [ [Function] ] }
If you need add event listener only for first event dispatch,you can use this snippet:
classTicker{constructor(){this.emitter=createNanoEvents()}…once(event,callback){constunbind=this.emitter.on(event,(...args)=>{unbind()callback(...args)})returnunbind}}
emitter.on('event1',()=>{})emitter.on('event2',()=>{})emitter.events={}
About
Simple and tiny (107 bytes) event emitter library for JavaScript
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.