拼板印
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

48 lines
838 B

/**
* eventBus
*/
class EventBus {
constructor() {
this.events = {}
}
/**
* @description: on
*/
on(event, callback) {
if (!this.events[event]) {
this.events[event] = []
}
this.events[event].push(callback)
}
/**
* @description: once
*/
once(event, callback) {
const fn = (...args) => {
callback(...args)
this.off(event, fn)
}
this.on(event, fn)
}
/**
* @description: off
*/
off(event, callback) {
if (!this.events[event]) {
return
}
this.events[event] = this.events[event].filter((cb) => cb !== callback)
}
/**
* @description: emit
*/
emit(event, ...args) {
if (!this.events[event]) {
return
}
this.events[event].forEach((cb) => cb(...args))
}
}
const eventBus = new EventBus()
export default eventBus