add: support namespace listeners in components

This commit is contained in:
Leonardo Dominguez 2019-05-05 01:47:01 -04:00
parent 54a8ff8f8c
commit 4943c1aca2
2 changed files with 50 additions and 15 deletions

View file

@ -37,16 +37,19 @@ export default class VueSocketIO {
...Vue.prototype.$socket,
[namespace]: this.io
};
Vue.prototype.$vueSocketIo = {...Vue.prototype.$vueSocketIo, [namespace]: this};
} else {
Vue.prototype.$socket = {
[namespace]: this.io
};
Vue.prototype.$vueSocketIo = { [namespace]: this};
}
} else {
Vue.prototype.$socket = this.io;
Vue.prototype.$vueSocketIo = this;
}
Vue.prototype.$vueSocketIo = this;
Vue.mixin(Mixin);
Logger.info('Vue-Socket.io plugin enabled');

View file

@ -7,14 +7,21 @@ export default {
if(!this.sockets) this.sockets = {};
this.sockets.subscribe = (event, callback) => {
if (typeof this.$vueSocketIo === 'object') {
for (const namespace of Object.keys(this.$vueSocketIo)) {
this.sockets[namespace] = {
subscribe: (event, callback) => {
this.$vueSocketIo[namespace].emitter.addListener(event, callback, this);
},
unsubscribe: (event) => {
this.$vueSocketIo[namespace].emitter.removeListener(event, this);
}
}
}
} else {
this.$vueSocketIo.emitter.addListener(event, callback, this);
};
this.sockets.unsubscribe = (event) => {
this.$vueSocketIo.emitter.removeListener(event, this);
};
}
},
/**
@ -24,14 +31,27 @@ export default {
if(this.$options.sockets){
Object.keys(this.$options.sockets).forEach(event => {
if (typeof this.$vueSocketIo === 'object') {
for (const namespace of Object.keys(this.$vueSocketIo)) {
if (this.$options.sockets[namespace]) {
Object.keys(this.$options.sockets[namespace]).forEach(event => {
if(event !== 'subscribe' && event !== 'unsubscribe') {
this.$vueSocketIo.emitter.addListener(event, this.$options.sockets[event], this);
if(event !== 'subscribe' && event !== 'unsubscribe') {
this.$vueSocketIo[namespace].emitter.addListener(event, this.$options.sockets[namespace][event], this);
}
});
}
}
} else {
Object.keys(this.$options.sockets).forEach(event => {
});
if(event !== 'subscribe' && event !== 'unsubscribe') {
this.$vueSocketIo.emitter.addListener(event, this.$options.sockets[event], this);
}
});
}
}
},
@ -43,11 +63,23 @@ export default {
if(this.$options.sockets){
Object.keys(this.$options.sockets).forEach(event => {
if (typeof this.$vueSocketIo === 'object') {
for (const namespace of Object.keys(this.$vueSocketIo)) {
if (this.$options.sockets[namespace]) {
Object.keys(this.$options.sockets[namespace]).forEach(event => {
this.$vueSocketIo.emitter.removeListener(event, this);
this.$vueSocketIo[namespace].emitter.removeListener(event, this);
});
}
}
} else {
Object.keys(this.$options.sockets).forEach(event => {
});
this.$vueSocketIo.emitter.removeListener(event, this);
});
}
}