function EventHub () {
this.events = {}
}
EventHub.prototype.$on = function(name, fn) {
(this.events[name] || (this.events[name] = [])).push(fn)
}
EventHub.prototype.$emit = function (name, ...args) {
this.events[name] && this.events[name].map(dn => fn.call(undefined, ...args))
}
EventHub.prototype.$off = function (name, fn) {
const events = this.events[name] && this.events[name].filter(v => v !== fn)
if (events) {
if (events.length === 0) {
delete this.events[name]
} else {
this.events[name] = events
}
}
}
网友评论