Merge pull request #125 from lastmirage/master

Support multiple packet data argument
pull/118/head^2
Metin Seylan 6 years ago committed by GitHub
commit 99810e131a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,9 +4,9 @@ export default new class {
}
addListener(label, callback, vm) {
if(typeof callback == 'function'){
if (typeof callback == 'function') {
this.listeners.has(label) || this.listeners.set(label, []);
this.listeners.get(label).push({callback: callback, vm: vm});
this.listeners.get(label).push({ callback: callback, vm: vm });
return true
}
@ -39,7 +39,7 @@ export default new class {
if (listeners && listeners.length) {
listeners.forEach((listener) => {
listener.callback.call(listener.vm,...args)
listener.callback.call(listener.vm, ...args)
});
return true;
}

@ -3,16 +3,16 @@ import Emitter from './Emitter'
export default {
install(Vue, connection, store){
install(Vue, connection, store) {
if(!connection) throw new Error("[Vue-Socket.io] cannot locate connection")
if (!connection) throw new Error("[Vue-Socket.io] cannot locate connection")
let observer = new Observer(connection, store)
Vue.prototype.$socket = observer.Socket;
Vue.mixin({
created(){
created() {
let sockets = this.$options['sockets']
this.$options.sockets = new Proxy({}, {
@ -28,16 +28,16 @@ export default {
}
})
if(sockets){
if (sockets) {
Object.keys(sockets).forEach((key) => {
this.$options.sockets[key] = sockets[key];
});
}
},
beforeDestroy(){
beforeDestroy() {
let sockets = this.$options['sockets']
if(sockets){
if (sockets) {
Object.keys(sockets).forEach((key) => {
delete this.$options.sockets[key]
});

@ -1,30 +1,30 @@
import Emitter from './Emitter'
import Socket from 'socket.io-client'
export default class{
export default class {
constructor(connection, store) {
if(typeof connection == 'string'){
if (typeof connection == 'string') {
this.Socket = Socket(connection);
}else{
} else {
this.Socket = connection
}
if(store) this.store = store;
if (store) this.store = store;
this.onEvent()
}
onEvent(){
onEvent() {
var super_onevent = this.Socket.onevent;
this.Socket.onevent = (packet) => {
super_onevent.call(this.Socket, packet);
Emitter.emit(packet.data[0], packet.data[1]);
Emitter.emit.apply(Emitter, packet.data);
if(this.store) this.passToStore('SOCKET_'+packet.data[0], [ ...packet.data.slice(1)])
if (this.store) this.passToStore('SOCKET_' + packet.data[0], [...packet.data.slice(1)])
};
let _this = this;
@ -33,30 +33,30 @@ export default class{
.forEach((value) => {
_this.Socket.on(value, (data) => {
Emitter.emit(value, data);
if(_this.store) _this.passToStore('SOCKET_'+value, data)
if (_this.store) _this.passToStore('SOCKET_' + value, data)
})
})
}
passToStore(event, payload){
if(!event.startsWith('SOCKET_')) return
passToStore(event, payload) {
if (!event.startsWith('SOCKET_')) return
for(let namespaced in this.store._mutations) {
for (let namespaced in this.store._mutations) {
let mutation = namespaced.split('/').pop()
if(mutation === event.toUpperCase()) this.store.commit(namespaced, payload)
if (mutation === event.toUpperCase()) this.store.commit(namespaced, payload)
}
for(let namespaced in this.store._actions) {
for (let namespaced in this.store._actions) {
let action = namespaced.split('/').pop()
if(!action.startsWith('socket_')) continue
if (!action.startsWith('socket_')) continue
let camelcased = 'socket_'+event.toLowerCase()
.replace('SOCKET_', '')
.replace(/[\W\s_]+(\w)/g, (match, p1) => p1.toUpperCase())
let camelcased = 'socket_' + event.toLowerCase()
.replace('SOCKET_', '')
.replace(/[\W\s_]+(\w)/g, (match, p1) => p1.toUpperCase())
if(action === camelcased) this.store.dispatch(namespaced, payload)
if (action === camelcased) this.store.dispatch(namespaced, payload)
}
}
}

Loading…
Cancel
Save