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

Simple and tiny (107 bytes) event emitter library for JavaScript

License

NotificationsYou must be signed in to change notification settings

ai/nanoevents

Repository files navigation

Simple and tiny event emitter library for JavaScript.

  • Only108 bytes (minified and brotlied).It usesSize Limit to control size.
  • Theon method returnsunbind function. You don’t need to savecallback to variable forremoveListener.
  • TypeScript and ES modules support.
  • No aliases, justemit 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

npm install nanoevents

TypeScript

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)

Mixing to Object

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

Add Listener

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.

Remove Listener

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

Execute Listeners

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'

Events List

You can get used events list byevents property.

constunbind=emitter.on('tick',()=>{})emitter.events//=> { tick: [ [Function] ] }

Once

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}}

Remove All Listeners

emitter.on('event1',()=>{})emitter.on('event2',()=>{})emitter.events={}

About

Simple and tiny (107 bytes) event emitter library for JavaScript

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

[8]ページ先頭

©2009-2025 Movatter.jp