mirror of
https://github.com/MetinSeylan/Vue-Socket.io.git
synced 2025-04-16 15:21:28 +02:00
added vuex support
added actions & mutations prefix updated gitignore & added npmignore file
This commit is contained in:
parent
991cc956d2
commit
6de5990456
4 changed files with 79 additions and 17 deletions
24
.gitignore
vendored
24
.gitignore
vendored
|
@ -1,5 +1,23 @@
|
|||
.DS_Store
|
||||
node_modules/
|
||||
npm-debug.log
|
||||
.idea/
|
||||
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
|
17
.npmignore
Normal file
17
.npmignore
Normal file
|
@ -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/
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue