初始化
This commit is contained in:
18
utils/eventEmitter.js
Normal file
18
utils/eventEmitter.js
Normal file
@@ -0,0 +1,18 @@
|
||||
export class EventEmitter {
|
||||
constructor() {
|
||||
this._events = Object.create(null)
|
||||
}
|
||||
on(type, fn) {
|
||||
(this._events[type] || (this._events[type] = [])).push(fn)
|
||||
}
|
||||
emit(type, ...args) {
|
||||
(this._events[type] || []).forEach(fn => fn(...args))
|
||||
}
|
||||
off(type, fn) {
|
||||
if (!fn) this._events[type] = []
|
||||
else {
|
||||
const idx = (this._events[type] || []).indexOf(fn)
|
||||
if (idx > -1) this._events[type].splice(idx, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user