https://github.com/tur-nr/node-tiny-emit
Another event emitter, only tiny.
https://github.com/tur-nr/node-tiny-emit
Last synced: 4 months ago
JSON representation
Another event emitter, only tiny.
- Host: GitHub
- URL: https://github.com/tur-nr/node-tiny-emit
- Owner: tur-nr
- License: mit
- Created: 2017-03-13T10:20:27.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-13T10:53:14.000Z (over 8 years ago)
- Last Synced: 2025-02-01T03:16:18.503Z (5 months ago)
- Language: JavaScript
- Size: 45.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tiny-emit

[](https://travis-ci.org/tur-nr/node-tiny-emit)
[](https://coveralls.io/github/tur-nr/node-tiny-emit?branch=master)
[](https://github.com/sindresorhus/xo)Another event emitter, only tiny.
## Install
_Yarn_
```sh
yarn add tiny-emit
```_NPM_
```sh
npm install --save-dev tiny-emit
```## Usage
```javascript
import tiny from 'tiny-emit';const emitter = tiny();
emitter.on('foo', (bar) => {
console.log(bar); // bar
});emitter.emit('foo', 'bar');
```### Once
```javascript
let i = 0;emitter.once('incr', () => (i += 1));
emitter.emit('incr');
emitter.emit('incr');console.log(i); // 1
```### Off
There is many ways to switch off a listener for an event. Call the listeners off function returned whenever the listener was added. This works for both `.on()` and `.once()` methods.
```javascript
const off = emitter.on('foo', () => {});
off();
```Or use the `.off()` method on the emitter.
```javascript
emitter.off('foo', listener); // specific event and listener
emitter.off('foo'); // all listeners for given event
emitter.off(); // every listener for every event
```## API
#### `#tiny()`
_Returns an emitter object._
### Emitter Object
#### `.on(, )`
- `event` _String_ The event name.
- `fn` _Function_ The listener function._Returns an off Function._
#### `.once(, )`
- `event` _String_ The event name.
- `fn` _Function_ The listener function._Returns an off Function._
#### `.off([event, [fn]])`
- `event` _String_ The event name.
- `fn` _Function_ The listener function._Returns void._
#### `.emit(, [...args])`
- `event` _String_ The event name.
- `...args` _Any_ Optional arguments to pass._Returns void._
## License
[MIT](LICENSE)
Copyright (c) 2017 [Christopher Turner](https://github.com/tur-nr)