feat(): Multiple namespaced connections
parent
2f63eebb70
commit
35b3591b45
File diff suppressed because one or more lines are too long
@ -1,136 +1,113 @@
|
|||||||
import Logger from './logger';
|
import Logger from './logger';
|
||||||
|
|
||||||
export default class EventEmitter{
|
export default class EventEmitter {
|
||||||
|
constructor(vuex = {}) {
|
||||||
constructor(vuex = {}){
|
Logger.info(vuex ? `Vuex adapter enabled` : `Vuex adapter disabled`);
|
||||||
Logger.info(vuex ? `Vuex adapter enabled` : `Vuex adapter disabled`);
|
Logger.info(vuex.mutationPrefix ? `Vuex socket mutations enabled` : `Vuex socket mutations disabled`);
|
||||||
Logger.info(vuex.mutationPrefix ? `Vuex socket mutations enabled` : `Vuex socket mutations disabled`);
|
Logger.info(vuex ? `Vuex socket actions enabled` : `Vuex socket actions disabled`);
|
||||||
Logger.info(vuex ? `Vuex socket actions enabled` : `Vuex socket actions disabled`);
|
this.store = vuex.store;
|
||||||
this.store = vuex.store;
|
this.actionPrefix = vuex.actionPrefix ? vuex.actionPrefix : 'SOCKET_';
|
||||||
this.actionPrefix = vuex.actionPrefix ? vuex.actionPrefix : 'SOCKET_';
|
this.mutationPrefix = vuex.mutationPrefix ? vuex.mutationPrefix : 'SOCKET_';
|
||||||
this.mutationPrefix = vuex.mutationPrefix;
|
this.listeners = new Map();
|
||||||
this.listeners = new Map();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* register new event listener with vuejs component instance
|
||||||
|
* @param event
|
||||||
|
* @param callback
|
||||||
|
* @param component
|
||||||
|
*/
|
||||||
|
addListener(event, callback, component) {
|
||||||
|
if (typeof callback === 'function') {
|
||||||
|
if (!this.listeners.has(event)) this.listeners.set(event, []);
|
||||||
|
this.listeners.get(event).push({ callback, component });
|
||||||
|
|
||||||
|
Logger.info(`#${event} subscribe, component: ${component.$options.name}`);
|
||||||
|
} else {
|
||||||
|
throw new Error(`callback must be a function`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* register new event listener with vuejs component instance
|
/**
|
||||||
* @param event
|
* remove a listenler
|
||||||
* @param callback
|
* @param event
|
||||||
* @param component
|
* @param component
|
||||||
*/
|
*/
|
||||||
addListener(event, callback, component){
|
removeListener(event, component) {
|
||||||
|
if (this.listeners.has(event)) {
|
||||||
if(typeof callback === 'function'){
|
const listeners = this.listeners.get(event).filter(listener => listener.component !== component);
|
||||||
|
|
||||||
if (!this.listeners.has(event)) this.listeners.set(event, []);
|
if (listeners.length > 0) {
|
||||||
this.listeners.get(event).push({ callback, component });
|
this.listeners.set(event, listeners);
|
||||||
|
} else {
|
||||||
Logger.info(`#${event} subscribe, component: ${component.$options.name}`);
|
this.listeners.delete(event);
|
||||||
|
}
|
||||||
} else {
|
|
||||||
|
Logger.info(`#${event} unsubscribe, component: ${component.$options.name}`);
|
||||||
throw new Error(`callback must be a function`);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* remove a listenler
|
/**
|
||||||
* @param event
|
* broadcast incoming event to components
|
||||||
* @param component
|
* @param event
|
||||||
*/
|
* @param args
|
||||||
removeListener(event, component){
|
*/
|
||||||
|
emit(event, args) {
|
||||||
if(this.listeners.has(event)){
|
if (this.listeners.has(event)) {
|
||||||
|
Logger.info(`Broadcasting: #${event}, Data:`, args);
|
||||||
const listeners = this.listeners.get(event).filter(listener => (
|
|
||||||
listener.component !== component
|
this.listeners.get(event).forEach(listener => {
|
||||||
));
|
listener.callback.call(listener.component, args);
|
||||||
|
});
|
||||||
if (listeners.length > 0) {
|
|
||||||
this.listeners.set(event, listeners);
|
|
||||||
} else {
|
|
||||||
this.listeners.delete(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.info(`#${event} unsubscribe, component: ${component.$options.name}`);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (event !== 'ping' && event !== 'pong') {
|
||||||
* broadcast incoming event to components
|
this.dispatchStore(event, args);
|
||||||
* @param event
|
}
|
||||||
* @param args
|
}
|
||||||
*/
|
|
||||||
emit(event, args){
|
/**
|
||||||
|
* dispatching vuex actions
|
||||||
if(this.listeners.has(event)){
|
* @param event
|
||||||
|
* @param args
|
||||||
Logger.info(`Broadcasting: #${event}, Data:`, args);
|
*/
|
||||||
|
dispatchStore(event, args) {
|
||||||
this.listeners.get(event).forEach((listener) => {
|
if (this.store && this.store._actions) {
|
||||||
listener.callback.call(listener.component, args);
|
let prefixedEvent;
|
||||||
});
|
if (typeof this.actionPrefix === 'function') {
|
||||||
|
prefixedEvent = this.actionPrefix(event);
|
||||||
}
|
} else {
|
||||||
|
prefixedEvent = this.actionPrefix + event;
|
||||||
if(event !== 'ping' && event !== 'pong') {
|
}
|
||||||
this.dispatchStore(event, args);
|
|
||||||
|
for (let key in this.store._actions) {
|
||||||
|
let action = key.split('/').pop();
|
||||||
|
|
||||||
|
if (action === prefixedEvent) {
|
||||||
|
Logger.info(`Dispatching Action: ${key}, Data:`, args);
|
||||||
|
|
||||||
|
this.store.dispatch(key, args);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.store && this.store._mutations) {
|
||||||
|
let prefixedEvent;
|
||||||
|
if (typeof this.mutationPrefix === 'function') {
|
||||||
|
prefixedEvent = this.mutationPrefix(event);
|
||||||
|
} else {
|
||||||
|
prefixedEvent = (this.mutationPrefix || '') + event;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
for (let key in this.store._mutations) {
|
||||||
* dispatching vuex actions
|
let mutation = key.split('/').pop();
|
||||||
* @param event
|
|
||||||
* @param args
|
|
||||||
*/
|
|
||||||
dispatchStore(event, args){
|
|
||||||
|
|
||||||
if(this.store && this.store._actions){
|
|
||||||
|
|
||||||
let prefixed_event = this.actionPrefix + event;
|
|
||||||
|
|
||||||
for (let key in this.store._actions) {
|
|
||||||
|
|
||||||
let action = key.split('/').pop();
|
|
||||||
|
|
||||||
if(action === prefixed_event) {
|
|
||||||
|
|
||||||
Logger.info(`Dispatching Action: ${key}, Data:`, args);
|
|
||||||
|
|
||||||
this.store.dispatch(key, args);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(this.mutationPrefix) {
|
if (mutation === prefixedEvent) {
|
||||||
|
Logger.info(`Commiting Mutation: ${key}, Data:`, args);
|
||||||
let prefixed_event = this.mutationPrefix + event;
|
|
||||||
|
|
||||||
for (let key in this.store._mutations) {
|
|
||||||
|
|
||||||
let mutation = key.split('/').pop();
|
|
||||||
|
|
||||||
if(mutation === prefixed_event) {
|
|
||||||
|
|
||||||
Logger.info(`Commiting Mutation: ${key}, Data:`, args);
|
|
||||||
|
|
||||||
this.store.commit(key, args);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
this.store.commit(key, args);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,27 @@
|
|||||||
/**
|
/**
|
||||||
* shitty logger class
|
* shitty logger class
|
||||||
*/
|
*/
|
||||||
export default new class VueSocketIOLogger {
|
export default new (class VueSocketIOLogger {
|
||||||
|
constructor() {
|
||||||
constructor() {
|
this.debug = false;
|
||||||
this.debug = false;
|
this.prefix = '%cVue-Socket.io: ';
|
||||||
this.prefix = '%cVue-Socket.io: ';
|
}
|
||||||
}
|
|
||||||
|
info(text, data = '') {
|
||||||
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);
|
||||||
error() {
|
}
|
||||||
|
|
||||||
if(this.debug) window.console.error(this.prefix, ...arguments);
|
warn() {
|
||||||
|
if (this.debug) window.console.warn(this.prefix, ...arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
warn() {
|
event(text, data = '') {
|
||||||
|
if (this.debug)
|
||||||
if(this.debug) window.console.warn(this.prefix, ...arguments);
|
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 {
|
export default {
|
||||||
|
/**
|
||||||
/**
|
* Assign runtime callbacks
|
||||||
* Assign runtime callbacks
|
*/
|
||||||
*/
|
beforeCreate() {
|
||||||
beforeCreate(){
|
if (!this.sockets) this.sockets = {};
|
||||||
|
|
||||||
if(!this.sockets) this.sockets = {};
|
this.sockets.subscribe = (event, callback) => {
|
||||||
|
this.$vueSocketIo.emitter.addListener(event, callback, this);
|
||||||
this.sockets.subscribe = (event, callback) => {
|
};
|
||||||
this.$vueSocketIo.emitter.addListener(event, callback, this);
|
|
||||||
};
|
this.sockets.unsubscribe = event => {
|
||||||
|
this.$vueSocketIo.emitter.removeListener(event, this);
|
||||||
this.sockets.unsubscribe = (event) => {
|
};
|
||||||
this.$vueSocketIo.emitter.removeListener(event, this);
|
},
|
||||||
};
|
|
||||||
|
/**
|
||||||
},
|
* Register all socket events
|
||||||
|
*/
|
||||||
/**
|
mounted() {
|
||||||
* Register all socket events
|
const { sockets } = this.$options;
|
||||||
*/
|
const { emitter } = this.$vueSocketIo;
|
||||||
mounted(){
|
|
||||||
|
if (sockets) {
|
||||||
if(this.$options.sockets){
|
Object.keys(sockets).forEach(eventOrNamespace => {
|
||||||
|
const functionOrObject = sockets[eventOrNamespace];
|
||||||
Object.keys(this.$options.sockets).forEach(event => {
|
if (
|
||||||
|
typeof functionOrObject === 'function' &&
|
||||||
if(event !== 'subscribe' && event !== 'unsubscribe') {
|
eventOrNamespace !== 'subscribe' &&
|
||||||
this.$vueSocketIo.emitter.addListener(event, this.$options.sockets[event], this);
|
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
|
/**
|
||||||
*/
|
* unsubscribe when component unmounting
|
||||||
beforeDestroy(){
|
*/
|
||||||
|
beforeDestroy() {
|
||||||
if(this.$options.sockets){
|
const { sockets } = this.$options;
|
||||||
|
const { emitter } = this.$vueSocketIo;
|
||||||
Object.keys(this.$options.sockets).forEach(event => {
|
|
||||||
|
if (sockets) {
|
||||||
this.$vueSocketIo.emitter.removeListener(event, this);
|
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