1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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 } } }
|