From 6de599045634f76b4f56e96af2ce6cf6ab84865a Mon Sep 17 00:00:00 2001 From: Metin Seylan Date: Sun, 28 Oct 2018 18:56:16 +0300 Subject: [PATCH] added vuex support added actions & mutations prefix updated gitignore & added npmignore file --- .gitignore | 26 ++++++++++++++++++++++---- .npmignore | 17 +++++++++++++++++ src/emitter.js | 41 +++++++++++++++++++++++++++++++---------- src/listenler.js | 12 +++++++++--- 4 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 .npmignore diff --git a/.gitignore b/.gitignore index c059e24..d0ca08e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,23 @@ .DS_Store -node_modules/ -npm-debug.log -.idea/ -package-lock.json \ No newline at end of file +node_modules +# dist + +# local env files +.env.local +.env.*.local +*.map + +# Log files +npm-debug.log* +yarn-debug.log* +yarn-error.log* +package-lock.json + +# Editor directories and files +.bitmap +.idea +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..4aa3d61 --- /dev/null +++ b/.npmignore @@ -0,0 +1,17 @@ +.DS_Store +src/ +node_modules/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* +*.map + +# Editor directories and files +.idea +.vscode +.bitmap +*.suo +*.ntvs* +*.njsproj +*.sln +.github/ \ No newline at end of file diff --git a/src/emitter.js b/src/emitter.js index bd3c4e7..312e67d 100644 --- a/src/emitter.js +++ b/src/emitter.js @@ -3,9 +3,12 @@ import Logger from './logger'; export default class EventEmitter{ constructor(vuex = {}){ - Logger.info(vuex ? `Vuex adapter active` : `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 ? `Vuex socket actions enabled` : `Vuex socket actions disabled`); this.store = vuex.store; - this.prefix = vuex.prefix ? vuex.prefix : 'SOCKET_'; + this.actionPrefix = vuex.actionPrefix ? vuex.actionPrefix : 'SOCKET_'; + this.mutationPrefix = vuex.mutationPrefix; this.listeners = new Map(); } @@ -62,19 +65,19 @@ export default class EventEmitter{ * @param event * @param args */ - emit(event, ...args){ + emit(event, args){ if(this.listeners.has(event)){ - Logger.info(`Broadcasting: #${event}, Data:`, ...args); + Logger.info(`Broadcasting: #${event}, Data:`, args); this.listeners.get(event).forEach((listener) => { - listener.callback.call(listener.component, ...args) + listener.callback.call(listener.component, args); }); } - this.dispatchStore(event, ...args) + this.dispatchStore(event, args); } @@ -84,7 +87,7 @@ export default class EventEmitter{ * @param event * @param args */ - dispatchStore(event, ...args){ + dispatchStore(event, args){ if(this.store && this.store._actions){ @@ -92,11 +95,29 @@ export default class EventEmitter{ let action = key.split('/').pop(); - if(action === this.prefix+event) { + if(action === this.actionPrefix+event) { - Logger.info(`Dispatching Action: ${action}, Data:`, ...args); + Logger.info(`Dispatching Action: ${action}, Data:`, args); - this.store.dispatch(action, ...args); + this.store.dispatch(action, args); + + } + + } + + if(this.mutationPrefix) { + + for (let key in this.store._mutations) { + + let mutation = key.split('/').pop(); + + if(mutation === this.mutationPrefix+event) { + + Logger.info(`Commiting Mutation: ${mutation}, Data:`, args); + + this.store.commit(mutation, args); + + } } diff --git a/src/listenler.js b/src/listenler.js index 218e9e9..92a562d 100644 --- a/src/listenler.js +++ b/src/listenler.js @@ -31,15 +31,21 @@ export default class VueSocketIOListenler { * Listening all socket.io events */ register(){ - this.io.onevent = (packet) => this.onEvent(packet.data[0], packet.data[1]); + this.io.onevent = (packet) => { + let [event, ...args] = packet.data; + + if(args.length === 1) args = args[0]; + + this.onEvent(event, args) + }; VueSocketIOListenler.staticEvents.forEach(event => this.io.on(event, () => this.onEvent(event))) } /** * Broadcast all events to vuejs environment */ - onEvent(event, data = {}){ - this.emitter.emit(event, data); + onEvent(event, args){ + this.emitter.emit(event, args); } }