feat(): Multiple namespaced connections
parent
2f63eebb70
commit
35b3591b45
File diff suppressed because one or more lines are too long
@ -1,35 +1,27 @@
|
||||
/**
|
||||
* shitty logger class
|
||||
*/
|
||||
export default new class VueSocketIOLogger {
|
||||
|
||||
export default new (class VueSocketIOLogger {
|
||||
constructor() {
|
||||
this.debug = false;
|
||||
this.prefix = '%cVue-Socket.io: ';
|
||||
}
|
||||
|
||||
info(text, data = '') {
|
||||
|
||||
if(this.debug) window.console.info(this.prefix+`%c${text}`, 'color: blue; font-weight: 600', 'color: #333333', data);
|
||||
|
||||
if (this.debug)
|
||||
window.console.info(this.prefix + `%c${text}`, 'color: blue; font-weight: 600', 'color: #333333', data);
|
||||
}
|
||||
|
||||
error() {
|
||||
|
||||
if(this.debug) window.console.error(this.prefix, ...arguments);
|
||||
|
||||
if (this.debug) window.console.error(this.prefix, ...arguments);
|
||||
}
|
||||
|
||||
warn() {
|
||||
|
||||
if(this.debug) window.console.warn(this.prefix, ...arguments);
|
||||
|
||||
if (this.debug) window.console.warn(this.prefix, ...arguments);
|
||||
}
|
||||
|
||||
event(text, data = ''){
|
||||
|
||||
if(this.debug) window.console.info(this.prefix+`%c${text}`, 'color: blue; font-weight: 600', 'color: #333333', data);
|
||||
|
||||
event(text, data = '') {
|
||||
if (this.debug)
|
||||
window.console.info(this.prefix + `%c${text}`, 'color: blue; font-weight: 600', 'color: #333333', data);
|
||||
}
|
||||
|
||||
}
|
||||
})();
|
||||
|
@ -1,56 +1,64 @@
|
||||
export default {
|
||||
|
||||
/**
|
||||
* Assign runtime callbacks
|
||||
*/
|
||||
beforeCreate(){
|
||||
|
||||
if(!this.sockets) this.sockets = {};
|
||||
beforeCreate() {
|
||||
if (!this.sockets) this.sockets = {};
|
||||
|
||||
this.sockets.subscribe = (event, callback) => {
|
||||
this.$vueSocketIo.emitter.addListener(event, callback, this);
|
||||
};
|
||||
|
||||
this.sockets.unsubscribe = (event) => {
|
||||
this.sockets.unsubscribe = event => {
|
||||
this.$vueSocketIo.emitter.removeListener(event, this);
|
||||
};
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Register all socket events
|
||||
*/
|
||||
mounted(){
|
||||
|
||||
if(this.$options.sockets){
|
||||
|
||||
Object.keys(this.$options.sockets).forEach(event => {
|
||||
|
||||
if(event !== 'subscribe' && event !== 'unsubscribe') {
|
||||
this.$vueSocketIo.emitter.addListener(event, this.$options.sockets[event], this);
|
||||
mounted() {
|
||||
const { sockets } = this.$options;
|
||||
const { emitter } = this.$vueSocketIo;
|
||||
|
||||
if (sockets) {
|
||||
Object.keys(sockets).forEach(eventOrNamespace => {
|
||||
const functionOrObject = sockets[eventOrNamespace];
|
||||
if (
|
||||
typeof functionOrObject === 'function' &&
|
||||
eventOrNamespace !== 'subscribe' &&
|
||||
eventOrNamespace !== 'unsubscribe'
|
||||
) {
|
||||
emitter.addListener(eventOrNamespace, functionOrObject, this);
|
||||
} else {
|
||||
Object.keys(functionOrObject).forEach(event => {
|
||||
if (event !== 'subscribe' && event !== 'unsubscribe') {
|
||||
emitter.addListener(`${eventOrNamespace}_${event}`, functionOrObject[event], this);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* unsubscribe when component unmounting
|
||||
*/
|
||||
beforeDestroy(){
|
||||
|
||||
if(this.$options.sockets){
|
||||
|
||||
Object.keys(this.$options.sockets).forEach(event => {
|
||||
|
||||
this.$vueSocketIo.emitter.removeListener(event, this);
|
||||
|
||||
beforeDestroy() {
|
||||
const { sockets } = this.$options;
|
||||
const { emitter } = this.$vueSocketIo;
|
||||
|
||||
if (sockets) {
|
||||
Object.keys(sockets).forEach(eventOrNamespace => {
|
||||
const functionOrObject = sockets[eventOrNamespace];
|
||||
if (typeof functionOrObject === 'function') {
|
||||
emitter.removeListener(eventOrNamespace, functionOrObject, this);
|
||||
} else {
|
||||
Object.keys(functionOrObject).forEach(event => {
|
||||
emitter.removeListener(event, functionOrObject[event], this);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
};
|
||||
|
Loading…
Reference in New Issue