Coder Social home page Coder Social logo

better-emitter's Introduction

A very simple event emitter with a better API than all the others.

It's better because:

  1. on and once return an unsubscribe function
  2. It doesn't use this, which means you can pass around the on or emit functions
  3. It can take in any existing object and turn it into an emitter

Also, it's pretty simple. Check out the source code.

npm i better-emitter
const createEmitter = require('better-emitter')

API

const emitter = createEmitter()

const unsubscribe = emitter.on('thing happened', (really, { stopPropagation }) => {
	really // => true
	typeof stopPropagation // => 'function'
})

emitter.emit('thing happened', true)

unsubscribe()

emitter = createEmitter([obj])

This is the function exported by the module. It creates a new event emitter object. You can also pass in any object you want, and event emitter functions will be added to it.

unsubscribe = emitter.on(eventString, listenerFunction)

Adds an event listener function. Returns an unsubscribe function that, when called, prevents the listener from firing any more.

The listener function will be passed the optional argument that you passed into emit, as well as an object with a stopPropagation function that you can call to prevent any other listeners from getting the message.

unsubscribe = emitter.once(eventString, listenerFunction)

Just like the on function, except the listener is automatically unsubscribed after the first time the event is emitted.

emitter.emit(eventString, [arg])

Calls all listeners of the given event string, with one optional argument.

emitter.removeAllListeners()

Unsubscribes all current event listeners.

License

WTFPL

better-emitter's People

Contributors

artskydj avatar benstevinson avatar tehshrike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.