Nano Events

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

README

Nano Events


Simple and tiny event emitter library for JavaScript.

Only 152 bytes (minified and gzipped).
  It uses [Size Limit] to control size.
on method returns unbind function. You don’t need to save
  callback to variable for removeListener.
TypeScript and ES modules support.
No aliases, just emit and on methods.
  No Node.js [EventEmitter] compatibility.

  1. ``` js
  2. import { createNanoEvents } from 'nanoevents'

  3. const emitter = createNanoEvents()

  4. const unbind = emitter.on('tick', volume => {
  5.   summary += volume
  6. })

  7. emitter.emit('tick', 2)
  8. summary //=> 2

  9. unbind()
  10. emitter.emit('tick', 2)
  11. summary //=> 2
  12. ```

[EventEmitter]: https://nodejs.org/api/events.html
[Size Limit]:   https://github.com/ai/size-limit

  <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
       alt="Sponsored by Evil Martians" width="236" height="54">


Table of Contents




Install


  1. ```sh
  2. npm install nanoevents
  3. ```


TypeScript


Nano Events accepts interface with event name
to listener argument types mapping.

  1. ```ts
  2. interface Events {
  3.   set: (name: string, count: number) => void,
  4.   tick: () => void
  5. }

  6. const emitter = createNanoEvents<Events>()

  7. // Correct calls:
  8. emitter.emit('set', 'prop', 1)
  9. emitter.emit('tick')

  10. // Compilation errors:
  11. emitter.emit('set', 'prop', '1')
  12. emitter.emit('tick', 2)
  13. ```


Mixing to Object


Because Nano Events API has only just 2 methods,
you could just create proxy methods in your class
or encapsulate them entirely.

  1. ``` js
  2. class Ticker {
  3.   constructor () {
  4.     this.emitter = createNanoEvents()
  5.     this.internal = setInterval(() => {
  6.       this.emitter.emit('tick')
  7.     }, 100)
  8.   }

  9.   stop () {
  10.     clearInterval(this.internal)
  11.     this.emitter.emit('stop')
  12.   }

  13.   on (event, callback) {
  14.     return this.emitter.on(event, callback)
  15.   }
  16. }
  17. ```

With Typescript:

  1. ```ts
  2. import { createNanoEvents, Emitter } from "nanoevents"

  3. interface Events {
  4.   start: (startedAt: number) => void
  5. }

  6. class Ticker {
  7.   emitter: Emitter

  8.   constructor () {
  9.     this.emitter = createNanoEvents<Events>()
  10.   }

  11.   on<E extends keyof Events>(event: E, callback: Events[E]) {
  12.     return this.emitter.on(event, callback)
  13.   }
  14. }
  15. ```


Add Listener


Use on method to add listener for specific event:

  1. ``` js
  2. emitter.on('tick', number => {
  3.   console.log(number)
  4. })

  5. emitter.emit('tick', 1)
  6. // Prints 1
  7. emitter.emit('tick', 5)
  8. // Prints 5
  9. ```

In case of your listener relies on some particular context
(if it uses this within itself) you have to bind required
context explicitly before passing function in as a callback.

  1. ``` js
  2. var app = {
  3.   userId: 1,
  4.   getListener () {
  5.     return () => {
  6.       console.log(this.userId)
  7.     }
  8.   }
  9. }
  10. emitter.on('print', app.getListener())
  11. ```

Note: binding with use of the .bind() method won’t work as you might expect
and therefore is not recommended.


Remove Listener


Methods on returns unbind function. Call it and this listener
will be removed from event.

  1. ``` js
  2. const unbind = emitter.on('tick', number => {
  3.   console.log('on ' + number)
  4. })

  5. emitter.emit('tick', 1)
  6. // Prints "on 1"

  7. unbind()
  8. emitter.emit('tick', 2)
  9. // Prints nothing
  10. ```


Execute Listeners


Method emit will execute all listeners. First argument is event name, others
will be passed to listeners.

  1. ``` js
  2. emitter.on('tick', (a, b) => {
  3.   console.log(a, b)
  4. })
  5. emitter.emit('tick', 1, 'one')
  6. // Prints 1, 'one'
  7. ```


Events List


You can get used events list by events property.

  1. ``` js
  2. const unbind = emitter.on('tick', () => { })
  3. emitter.events //=> { tick: [ [Function] ] }
  4. ```


Once


If you need add event listener only for first event dispatch,
you can use this snippet:

  1. ``` js
  2. class Ticker {
  3.   constructor () {
  4.     this.emitter = createNanoEvents()
  5.   }
  6.   
  7.   once (event, callback) {
  8.     const unbind = this.emitter.on(event, (...args) => {
  9.       unbind()
  10.       callback(...args)
  11.     })
  12.     return unbind
  13.   }
  14. }
  15. ```


Remove All Listeners


  1. ``` js
  2. emitter.on('event1', () => { })
  3. emitter.on('event2', () => { })

  4. emitter.events = { }
  5. ```