mirror of
https://github.com/MetinSeylan/Vue-Socket.io.git
synced 2025-04-16 15:21:28 +02:00
added a few changes and added support for custom events
This commit is contained in:
parent
d44a480584
commit
a6dd6643b6
3 changed files with 47 additions and 32 deletions
|
@ -5,8 +5,11 @@ export default new class {
|
|||
|
||||
addListener(label, callback, vm) {
|
||||
if (typeof callback == 'function') {
|
||||
this.listeners.has(label) || this.listeners.set(label, []);
|
||||
this.listeners.get(label).push({callback: callback, vm: vm});
|
||||
if (!this.listeners.has(label)) {
|
||||
this.listeners.set(label, []);
|
||||
}
|
||||
|
||||
this.listeners.get(label).push({ callback: callback, vm: vm })
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -28,11 +28,11 @@ export default {
|
|||
}
|
||||
})
|
||||
|
||||
if(sockets){
|
||||
/*if(sockets){
|
||||
Object.keys(sockets).forEach((key) => {
|
||||
this.$options.sockets[key] = sockets[key];
|
||||
});
|
||||
}
|
||||
}*/
|
||||
},
|
||||
beforeDestroy() {
|
||||
let sockets = this.$options['sockets']
|
||||
|
|
|
@ -19,9 +19,17 @@ export default class{
|
|||
|
||||
onEvent() {
|
||||
this.Socket.onevent = (packet) => {
|
||||
Emitter.emit(packet.data[0], packet.data[1]);
|
||||
let custom = Emitter.emit(packet.data[0], packet.data[1]);
|
||||
|
||||
if(this.store) this.passToStore('SOCKET_'+packet.data[0], [ ...packet.data.slice(1)])
|
||||
// user has not registered a handler for this event
|
||||
// perhaps its needed elsewhere, eg. a library
|
||||
// so emit the event locally
|
||||
if (!custom) {
|
||||
let onevent = this.Socket.onevent;
|
||||
onevent.call(this, packet);
|
||||
}
|
||||
|
||||
if (this.store) this.passToStore(packet.data[0], [...packet.data.slice(1)])
|
||||
};
|
||||
|
||||
let _this = this;
|
||||
|
@ -30,30 +38,34 @@ 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(value, data)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
passToStore(event, payload) {
|
||||
if(!event.startsWith('SOCKET_')) return
|
||||
|
||||
for(let namespaced in this.store._mutations) {
|
||||
let mutation = namespaced.split('/').pop()
|
||||
if(mutation === event.toUpperCase()) this.store.commit(namespaced, payload)
|
||||
this.passToMutation(event, payload);
|
||||
this.passToAction(event, payload);
|
||||
}
|
||||
|
||||
passToMutation(event, payload) {
|
||||
for (let namespaced in this.store._mutations) {
|
||||
let mutation = namespaced.split('/').pop()
|
||||
|
||||
if (mutation === `SOCKET_ ${event}`.toUpperCase()) {
|
||||
this.store.commit(namespaced, payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
passToAction(event, payload) {
|
||||
for (let namespaced in this.store._actions) {
|
||||
let action = namespaced.split('/').pop()
|
||||
|
||||
if(!action.startsWith('socket_')) continue
|
||||
|
||||
let camelcased = 'socket_'+event
|
||||
.replace('SOCKET_', '')
|
||||
.replace(/^([A-Z])|[\W\s_]+(\w)/g, (match, p1, p2) => p2 ? p2.toUpperCase() : p1.toLowerCase())
|
||||
|
||||
if(action === camelcased) this.store.dispatch(namespaced, payload)
|
||||
if (action === `socket_${event}`) {
|
||||
this.store.dispatch(namespaced, payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue