Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/egoist/eventstop
A minimal event library for Node.js and browser.
https://github.com/egoist/eventstop
Last synced: 13 days ago
JSON representation
A minimal event library for Node.js and browser.
- Host: GitHub
- URL: https://github.com/egoist/eventstop
- Owner: egoist
- License: mit
- Created: 2017-01-04T15:05:51.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-12-25T14:02:19.000Z (almost 3 years ago)
- Last Synced: 2024-10-23T07:51:27.331Z (21 days ago)
- Language: JavaScript
- Homepage:
- Size: 45.9 KB
- Stars: 115
- Watchers: 6
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# eventstop
[![NPM version](https://img.shields.io/npm/v/eventstop.svg?style=flat)](https://npmjs.com/package/eventstop) [![NPM downloads](https://img.shields.io/npm/dm/eventstop.svg?style=flat)](https://npmjs.com/package/eventstop) [![Build Status](https://img.shields.io/circleci/project/egoist/eventstop/master.svg?style=flat)](https://circleci.com/gh/egoist/eventstop) [![donate](https://img.shields.io/badge/$-donate-ff69b4.svg?maxAge=2592000&style=flat)](https://github.com/egoist/donate)
## Features
- Modern
- For Node.js and browsers
- Support wildcard listener: `on('*', (type, ...payload) => {})`
- Support all major browsers including IE8
- 300 bytes minified and gzipped## Install
```bash
yarn add eventstop
```You can also use the UMD version via https://unpkg.com/eventstop
## Usage
```js
const eventstop = require('eventstop')const {on, emit} = eventstop()
// subscribe an event
const off = on('ready', msg => {
console.log('message:', msg)
})emit('ready', 'hola')
//=> hola// unsubscribe
off()
```## API
```js
const event = eventstop()
// or
const {on, off, emit, once} = eventstop()
```### .on(event, handler)
Return a function which would execute `off(event, handler)` when you call it.
You can use `*` as event type to listen all types:
```js
on('*', (type, foo, bar) => {
console.log(type, foo, 'and', bar)
//=> hello you and me
})
emit('hello', 'you', 'me')
```In wildcard listener, the first argument of handler function is its **type**.
### .once(event, handler)
Like `.on` but only trigger `handler` once.
#### event
Type: `string`
#### handler
Type: `function`
### .emit(event, ...args)
#### event
Type: `string`
### .off(event, handler)
Same args to `.on`
## Contributing
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D## Author
**eventstop** © [egoist](https://github.com/egoist), Released under the [MIT](./LICENSE) License.
Authored and maintained by egoist with help from contributors ([list](https://github.com/egoist/eventstop/contributors)).> [egoistian.com](https://egoistian.com) · GitHub [@egoist](https://github.com/egoist) · Twitter [@_egoistlily](https://twitter.com/_egoistlily)