Giter Site home page Giter Site logo

nanoevents's Introduction

Nano Events

Simple and tiny event emitter library for JavaScript.

  • No Node.js EventEmitter compatibility.
  • Only 119 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.events, 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

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) //=> { }

nanoevents's People

Contributors

ai avatar subzey avatar ben-eb avatar

Watchers

James Cloos avatar Ryan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.