Home

Awesome

dush npm version github tags mit license

Microscopic & functional event emitter in ~350 bytes, extensible through plugins

You might also be interested in mitt - a 200 bytes event emitter. It has strict policy to stay exactly below 200b with no compromises, so has lack of support for few things and that's why dush exists.

Quality 👌

By using commitizen and conventional commit messages, maintaining meaningful ChangeLog and commit history based on global conventions, following StandardJS code style through ESLint and having always up-to-date dependencies through integrations like GreenKeeper and David-DM service, this package has top quality.

code climate code style commitizen friendly greenkeeper friendly dependencies

<!-- uncomment when need --> <!-- [![develop deps][daviddm-devdeps-img]][daviddm-devdeps-url] -->

Stability 💯

By following Semantic Versioning through standard-version releasing tool, this package is very stable and its tests are passing both on Windows (AppVeyor) and Linux (CircleCI) with results from 100% to 400% test coverage, reported respectively by CodeCov and nyc (istanbul).

following semver semantic releases linux build windows build code coverage nyc coverage

Support :clap:

If you have any problems, consider opening an issue, ping me on twitter (@tunnckoCore), join the support chat room or queue a live session on CodeMentor with me. If you don't have any problems, you're using it somewhere or you just enjoy this product, then please consider donating some cash at PayPal, since this is OPEN Open Source project made with love at Sofia, Bulgaria 🇧🇬.

tunnckoCore support code mentor paypal donate NPM monthly downloads npm total downloads

Highlights :sparkles:

Plugins

Table of Contents

(TOC generated by verb using markdown-toc)

Install

Install with npm

$ npm install dush --save

or install using yarn

$ yarn add dush

or using unpkg CDN

<script src="https://unpkg.com/dush/dist/dush.umd.js"></script>

Note: Don't use Unpkg's short-hand endpoint https://unpkg.com/dush, since it points to CommonJS bundle.

or using jsDelivr CDN

<script src="https://cdn.jsdelivr.net/npm/dush/dist/dush.umd.js">

Usage

Modern importing, using rollup or webpack bundler

import dush from 'dush'

Node.js require as CommonJS module

var dush = require('dush')

Old school in browsers, available at global scope

<script>
  var emitter = dush()
</script>

API

dush()

A constructor function that returns an object with a few methods.

See JSBin Example.

Example

const dush = require('dush')
const emitter = dush()

console.log(emitter._allEvents) // => {}
console.log(emitter.on) // => Function
console.log(emitter.once) // => Function
console.log(emitter.off) // => Function
console.log(emitter.emit) // => Function

._allEvents

An listeners map of all registered events and their listeners. A key/value store, where 1) value is an array of event listeners for the key and 2) key is the name of the event.

See JSBin Example.

Example

const emitter = dush()

emitter.on('foo', () => {})
emitter.on('foo', () => {})
emitter.on('bar', () => {})

console.log(emitter._allEvents)
// => { foo: [Function, Function], bar: [Functon] }

console.log(emitter._allEvents.foo.length) // => 2
console.log(emitter._allEvents.bar.length) // => 1

.use

Invokes plugin function immediately, which is passed with app instance. You can use it for adding more methods or properties to the instance. Useful if you want to make dush to work with DOM for example.

Params

Example

const app = dush()

app.on('hi', (str) => {
  console.log(str) // => 'Hello World!!'
})

app.use((app) => {
  app.foo = 'bar'
  app.hello = (place) => app.emit('hi', `Hello ${place}!!`)
})

console.log(app.foo) // => 'bar'
app.hello('World')

.on

Add handler for name event.

See JSBin Example.

Params

Example

const emitter = dush()

emitter
  .on('hi', (place) => {
    console.log(`hello ${place}!`) // => 'hello world!'
  })
  .on('hi', (place) => {
    console.log(`hi ${place}, yeah!`) // => 'hi world, yeah!'
  })

emitter.emit('hi', 'world')

.once

Add handler for name event that will be called only one time.

See JSBin Example.

Params

Example

const emitter = dush()
let called = 0

emitter.once('foo', () => {
  console.log('called only once')
  called++
})

emitter
  .emit('foo', 111)
  .emit('foo', 222)
  .emit('foo', 333)

console.log(called) // => 1

.off

Remove handler for name event. If handler not passed will remove all listeners for that name event.

See JSBin Example.

Params

Example

const emitter = dush()

const handler = () => {
  console.log('not called')
}

emitter.on('foo', handler)
emitter.off('foo', handler)

emitter.on('foo', (abc) => {
  console.log('called', abc) // => 'called 123'
})
emitter.emit('foo', 123)

// or removing all listeners of `foo`
emitter.off('foo')
emitter.emit('foo')

.emit

Invoke all handlers for given name event. If present, '*' listeners are invoked too with (type, ...rest) signature, where the type argument is a string representing the name of the called event; and all of the rest arguments.

See JSBin Example.

Params

Example

const emitter = dush()

emitter.on('foo', (a, b, c) => {
  console.log(`${a}, ${b}, ${c}`) // => 1, 2, 3
})

emitter.on('*', (name, a, b, c) => {
  console.log(`name is: ${name}`)
  console.log(`rest args are: ${a}, ${b}, ${c}`)
})

emitter.emit('foo', 1, 2, 3)
emitter.emit('bar', 555)

Related

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guidelines for advice on opening issues, pull requests, and coding standards.
If you need some help and can spent some cash, feel free to contact me at CodeMentor.io too.

In short: If you want to contribute to that project, please follow these things

  1. Please DO NOT edit README.md, CHANGELOG.md and .verb.md files. See "Building docs" section.
  2. Ensure anything is okey by installing the dependencies and run the tests. See "Running tests" section.
  3. Always use npm run commit to commit changes instead of git commit, because it is interactive and user-friendly. It uses commitizen behind the scenes, which follows Conventional Changelog idealogy.
  4. Do NOT bump the version in package.json. For that we use npm run release, which is standard-version and follows Conventional Changelog idealogy.

Thanks a lot! :)

Building docs

Documentation and that readme is generated using verb-generate-readme, which is a verb generator, so you need to install both of them and then run verb command like that

$ npm install verbose/verb#dev verb-generate-readme --global && verb

Please don't edit the README directly. Any changes to the readme must be made in .verb.md.

Running tests

Clone repository and run the following in that cloned directory

$ npm install && npm test

Author

Charlike Mike Reagent

License

Copyright © 2015, 2017, Charlike Mike Reagent. Released under the MIT License.


This file was generated by verb-generate-readme, v0.4.3, on April 02, 2017.
Project scaffolded using charlike cli.