In order to achieve faster event publishing and subscription in my project, I redeveloped an event bus component. I feel good about it, so I plan to share it with you. Welcome your test and advice.
The event bus is based on Typescript decorators, so it’s currently only available in Typescript, but it’s pretty handy to use.
I’m just going to put it up here and use the code, project link at the bottom
// Import Hi-Bus, you can use any word instead 'HiBus'
import HiBus, {Bus, Publish, Subscribe} from 'hi-bus'
// First we must create a class with class '@bus'
// greet '@bus' is used to collect subscribe functions
// it will not work without '@Bus’ when you subscribe message in class
@Bus
class Test {
// Subscribe topic "Handshake" with wow @Subscribe
// You only focus the function logic
@Subscribe("handshake")
handshake() {
console.log (" Handshake "); }// When you call function 'pulbishHandshake'
// it will publish topic ‘handshake’ automatic
// the function must return something to trigger publish
// if return nothing or return void, it will not trigger publish
@Publish("handshake")
publishHandshake() {
return{}; }}const test = new Test();
test.publishHandshake();
// console output: Handshake
Copy the code
Hi-bus GitHub project address