Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/molvqingtai/event-hub
π This is a minimalist event hub.
https://github.com/molvqingtai/event-hub
emit event event-bus events
Last synced: 2 months ago
JSON representation
π This is a minimalist event hub.
- Host: GitHub
- URL: https://github.com/molvqingtai/event-hub
- Owner: molvqingtai
- License: mit
- Created: 2021-05-17T18:04:34.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-22T14:59:48.000Z (over 1 year ago)
- Last Synced: 2024-10-16T15:35:40.345Z (3 months ago)
- Topics: emit, event, event-bus, events
- Language: TypeScript
- Homepage:
- Size: 707 KB
- Stars: 14
- Watchers: 2
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# EventHub
[![version](https://img.shields.io/github/v/release/molvqingtai/event-hub)](https://www.npmjs.com/package/@resreq/event-hub) [![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno&labelColor=black&color=blue)](https://deno.land/x/event_hub) [![download](https://img.shields.io/npm/dt/@resreq/event-hub)](https://www.npmjs.com/package/@resreq/event-hub) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
π This is a minimalist event hub.
## Install
```shell
npm i @resreq/event-hub
```**or**
```shell
yarn add @resreq/event-hub
```**If you use the [Deno](https://deno.land/)**
Please add to `import_map.json`.
```js
import EventHub from 'https://esm.sh/@resreq/event-hub'
```## Documentation
### Get Started
```js
import EventHub from '@resreq/event-hub'const hub = new EventHub()
hub.on('custom-send', (message) => {
console.log(message) // => Hello, EventHub!
})hub.emit('custom-send', 'Hello, EventHub!')
```### Instance Methods
**Event**
Event name type.
```typescript
type Event = string | symbol
```#### on(event,handler)
- **Arguments:**
- `{Event | Event[]} event`
- `{Function} handler`* **Usage:**
Listen for a custom event on the current Instance. Events can be triggered by `instance.emit`. The handler will receive all the additional arguments passed into these event-triggering methods.
#### once(event,handler)
- **Arguments:**
- `{Event} event`
- `{Function} handler`* **Usage:**
Listen for a custom event, but only once. The listener will be removed once it triggers for the first time.
#### off([event,handler])
- **Arguments:**
- `{Event | Event[]} event`
- `{Function} [handler]`* **Usage:**
Remove custom event listener(s).
- If no arguments are provided, remove all event listeners;
- If only the event is provided, remove all listeners for that event;
- If both event and handler are given, remove the listener for that specific handler only.#### emit(event,...args)
- **Arguments:**
- `{Event} event`
- `{any} ...args`
* **Usage:**
Trigger an event on the current instance. Any additional arguments will be passed into the listenerβs handler function.
## License
This project is licensed under the MIT License - see the [LICENSE](https://github.com/molvqingtai/event-hub/blob/main/LICENSE) file for details