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: about 1 year 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 (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-05-22T14:59:48.000Z (about 3 years ago)
- Last Synced: 2025-02-27T05:02:05.834Z (over 1 year ago)
- Topics: emit, event, event-bus, events
- Language: TypeScript
- Homepage:
- Size: 707 KB
- Stars: 13
- Watchers: 2
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# EventHub
[](https://www.npmjs.com/package/@resreq/event-hub) [](https://deno.land/x/event_hub) [](https://www.npmjs.com/package/@resreq/event-hub) [](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