https://github.com/egoist/eventstop
A minimal event library for Node.js and browser.
https://github.com/egoist/eventstop
Last synced: 7 months 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 (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-12-25T14:02:19.000Z (over 3 years ago)
- Last Synced: 2024-12-10T10:04:52.220Z (7 months 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
[](https://npmjs.com/package/eventstop) [](https://npmjs.com/package/eventstop) [](https://circleci.com/gh/egoist/eventstop) [](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)