Files

37 lines
657 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @name: emitter
* @author: kahu4
* @date: 2024-01-19 18:18
* @description事件派发、监听器
* @update: 2024-01-19 18:18
* */
class Emitter {
lister = {}
constructor() {
this.lister = {}
}
on(name, func) {
this.lister[name] ? this.lister[name].push(func) : this.lister[name] = [func];
}
emit(name, ...args) {
if (!this.lister[name]) return
this.lister[name].forEach(func => func(...args))
}
clear(name) {
this.lister[name] = []
}
clearAll() {
this.lister = {}
}
}
// 只暴露一个实例全局共用
export const emitter = new Emitter()