You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

50 lines
1.3 KiB

import Vue from 'vue';
class EventHub {
constructor (vm) {
this.vm = vm;
this.curVm = null;
this.events = {};
this.eventMapUid = {};
}
$register (vm) {
this.curVm = vm;
}
setEventMapUid (uid, type) {
if (!this.eventMapUid[uid]) {
this.eventMapUid[uid] = [];
}
this.eventMapUid[uid].push(type);
}
$on (type, fn) {
if (!this.events[type]) {
this.events[type] = [];
}
this.events[type].push(fn);
if (this.curVm instanceof this.vm) {
this.setEventMapUid(this.curVm._uid, type);
}
}
$emit (type, ...args) {
if (this.events[type]) {
this.events[type].forEach(fn => fn(...args));
}
}
$off (type, fn) {
if (fn && this.events[type]) {
const index = this.events[type].findIndex(f => f === fn);
if (index !== -1) {
this.events[type].splice(index, 1);
}
return;
}
delete this.events[type];
}
$offAll (uid) {
const curAllTypes = this.eventMapUid[uid] || [];
curAllTypes.forEach(type => this.$off(type));
delete this.eventMapUid[uid];
}
}
export default new EventHub(Vue);;