https://github.com/raineorshine/emitter20
A small event emitter with no dependencies.
https://github.com/raineorshine/emitter20
Last synced: about 1 month ago
JSON representation
A small event emitter with no dependencies.
- Host: GitHub
- URL: https://github.com/raineorshine/emitter20
- Owner: raineorshine
- License: isc
- Created: 2016-01-06T21:45:24.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-07-21T22:17:44.000Z (about 1 year ago)
- Last Synced: 2025-08-30T00:05:02.569Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 126 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# emitter20
[](https://npmjs.org/package/emitter20)
[](https://travis-ci.org/metaraine/emitter20)An event emitter that used to be 20 lines of code but is still quite small. No dependencies.
## Install
```sh
$ npm install --save emitter20
```## Methods
- `on: (eventName: string, callback: Function) => void` - Subscribe a callback to the given event type.
- `off: (eventName: string, callback: Function) => void` - Remove a callback from the given event type.
- `trigger: (eventName: string, callback: Function) => void` - Trigger an event, invoking all subscribers.
- `clear: (eventName?: string) => void` - Remove all subscribers from the given event type, or all subscribers if no event type is specified.## Usage
```js
var Emitter = require('emitter20')var emitter = new Emitter()
emitter.on('karate-chop', function() {
console.log('Haiaaaaaa!')
})emitter.trigger('karate-chop') // Haiaaaaa!'
```Pass arbitrary data to the event handler:
```js
var emitter = new Emitter()emitter.on('welcome', function(name) {
console.log(`Welcome {name}!`)
})emitter.trigger('welcome', 'bob') // Welcome bob!
```Can be used as a mixin:
```js
var assign = require('lodash.assign')var obj = { a: 1, b: 2 }
assign(obj, new Emitter())obj.on('karate-chop', function() {
console.log('Haiaaaaaa!')
})obj.trigger('karate-chop') // Haiaaaaa!'
```## License
ISC © [Raine Revere](https://github.com/raineorshine)