Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nanoevents

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nanoevents

Simple and tiny (101 bytes) event emitter library

  • 1.0.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
97K
decreased by-18.57%
Maintainers
1
Weekly downloads
 
Created
Source

Nano Events

Simple and tiny event emitter library for JavaScript.

  • No Node.js EventEmitter compatibility.
  • Only 101 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.
  • No aliases, just emit and on methods.
import NanoEvents from 'nanoevents'
const emitter = new NanoEvents()

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

function disable () {
  unbind()
}
Sponsored by Evil Martians

Usage

Mixing to Object

Because main Nano Events API has only 2 methods, you could just create proxy methods in your class:

class Ticker {
  constructor() {
    this.emitter = new NanoEvents()
  }
  on() {
    return this.emitter.on.apply(this.emitter, arguments)
  }
  tick() {
    this.emitter.emit('tick')
  }
}

Add Listener

Use on method to add listener for specific event:

emitter.on('tick', number => {
  console.log(number)
})

emitter.emit('tick', 1)
// Prints "1"
emitter.emit('tick', 2)
// Prints "2"

Remove Listener

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

const unbind = emitter.on('tick', number => {
  console.log('on ' + number)
})

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

unbind()
emitter.emit('tick', 2)
// Prints nothing

Execute Listeners

Method emit will execute all listeners. First argument is event name, others will 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 by events property.

const unbind = emitter.on('tick', () => { })
Object.keys(emitter.events) //=> ["tick"]

unbind()
Object.keys(emitter.events) //=> []

Helpers

Once

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

class Ticker {
  constructor() {
    this.emitter = new NanoEvents()
  }
  …
  once (event, callback) {
    const unbind = this.emitter.on(event, function () {
      unbind()
      callback.apply(this, arguments)
    })
    return unbind
  }
}
Remove all listeners

unbindAll method will remove all listeners to all events.

import unbindAll from 'nanoevents/unbind-all';

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

unbindAll(emitter);

Object.keys(emitter.events) //=> { }

Keywords

FAQs

Package last updated on 30 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc