Initial commit
This commit is contained in:
commit
c310cd7943
19 changed files with 3939 additions and 0 deletions
42
shared/event.js
Normal file
42
shared/event.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
export class EventBus {
|
||||
constructor() {
|
||||
this.map = new Map()
|
||||
}
|
||||
|
||||
on(type, cb) {
|
||||
if (this.map.has(type)) {
|
||||
this.map.get(type).push(cb)
|
||||
} else {
|
||||
this.map.set(type, [cb])
|
||||
}
|
||||
}
|
||||
|
||||
off(type, cb) {
|
||||
if (type) {
|
||||
if (cb) {
|
||||
const cbs = this.map.get(type)
|
||||
|
||||
if (!cbs) return
|
||||
const index = cbs.findIndex((i) => i === cb)
|
||||
|
||||
if (index >= 0) {
|
||||
cbs.splice(index, 1)
|
||||
}
|
||||
} else {
|
||||
this.map.delete(type)
|
||||
}
|
||||
} else {
|
||||
this.map.clear()
|
||||
}
|
||||
}
|
||||
|
||||
emit(type, ...args) {
|
||||
for (let cb of this.map.get(type) ? this.map.get(type) : []) {
|
||||
cb && cb(...args)
|
||||
}
|
||||
}
|
||||
|
||||
count(type) {
|
||||
return this.map.get(type) ? this.map.get(type).length : 0
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue