Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bhznjns/simple-eventbus
A simple implementation of TypeScript / JavaScript EventBus.
https://github.com/bhznjns/simple-eventbus
Last synced: 7 days ago
JSON representation
A simple implementation of TypeScript / JavaScript EventBus.
- Host: GitHub
- URL: https://github.com/bhznjns/simple-eventbus
- Owner: BHznJNs
- License: mit
- Created: 2023-01-02T15:26:02.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T07:32:08.000Z (about 2 years ago)
- Last Synced: 2023-10-11T07:13:29.695Z (over 1 year ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simple-eventbus
A simple implementation of TypeScript / JavaScript EventBus, which has dev version for argument / event checking.
* * *
## Usage
Import package:
```shell
npm i -s @bhznjns/simple-eventbus
``````JavaScript
// For common version:
import EventBus from "@bhznjns/simple-eventbus"
// For dev version,
// dev version has more error messages and the usage are the same
import EventBusDev from "@bhznjns/simple-eventbus/src/dev.js"const eventBus = new EventBus()
const eventBusDev = new EventBusDev()
```Add event:
```JavaScript
eventBus.on("event-name", eventHandler)
eventBusDev.on("event-name", eventHandler)// Support multi-arg
eventBus.on("multi-arg-event", (data1, data2) => {
// do something...
})
eventBusDev.on("multi-arg-event", (data1, data2) => {
// do something...
})
```Emit event:
```JavaScript
eventBus.emit("event-name", someData)
eventBusDev.emit("event-name", someData)// Support multi-arg
eventBus.emit("multi-arg-event", data1, data2)
eventBusDev.emit("multi-arg-event", data1, data2)
```Remove event:
```JavaScript
eventBus.off("event-name", eventHandler)
eventBusDev.off("event-name", eventHandler)
```* * *
## Warning messages for dev version
When you emitted an event that is unadded:
```JavaScript
eventBusDev.emit("unadded-event", someData)
// Warning: unadded event name: "unadded-event"
```When you emitted an event with wrong argument number:
```JavaScript
eventBusDev.on("test-event", (data) => {
// do something...
})
eventBusDev.emit("test-event", data1, data2)
// Warning: expected argument number: 1, actual argument number: 2
```When you added an event handler whose number of arguments is not equal to the existing event handler for the existing event:
```JavaScript
eventBusDev.on("test-event", (data) => {
// do something...
})
eventBusDev.on("test-event", (data1, data2) => {
// do something...
})
// Warning: expected argument number: 1, actual argument number: 2
```When you removed an event that is unadded
```JavaScript
eventBusDev.off("unadded-event", eventHandler)
// Warning: unadded event name: "unadded-event"
```When you removed an event handler that is unadded:
```JavaScript
eventBusDev.off("event-name", unaddedEventHandler)
// Warning: unadded event handler: "unaddedEventHandler"
```## TODO
- Add test codes
- Type check feature