mirror of
https://github.com/MetinSeylan/Vue-Socket.io.git
synced 2025-04-16 15:21:28 +02:00
Merge branch 'master' into master
This commit is contained in:
commit
f26cbed056
6 changed files with 31 additions and 29 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
|||
.DS_Store
|
||||
node_modules/
|
||||
npm-debug.log
|
||||
.idea/
|
||||
.idea/
|
||||
package-lock.json
|
2
dist/build.js
vendored
2
dist/build.js
vendored
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "vue-socket.io",
|
||||
"version": "2.1.1-a",
|
||||
"version": "2.1.1-b",
|
||||
"description": "socket.io implemantation for vuejs and vuex",
|
||||
"main": "dist/build.js",
|
||||
"scripts": {
|
||||
|
@ -29,7 +29,7 @@
|
|||
},
|
||||
"homepage": "https://github.com/MetinSeylan/Vue-Socket.io#readme",
|
||||
"dependencies": {
|
||||
"socket.io-client": "^1.4.6"
|
||||
"socket.io-client": "^2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.11.4",
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
12
src/Main.js
12
src/Main.js
|
@ -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,31 @@ 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.replace(/\W/g, '_').toUpperCase()) this.store.commit(namespaced, payload)
|
||||
if (mutation === event.replace(/\W/g, '_').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
|
||||
.replace('SOCKET_', '')
|
||||
.replace(/^([A-Z])|[\W\s_]+(\w)/g, (match, p1, p2) => p2 ? p2.toUpperCase() : p1.toLowerCase())
|
||||
.toLowerCase()
|
||||
.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…
Add table
Reference in a new issue