2
0
mirror of https://github.com/tenrok/vue-native-websocket.git synced 2026-06-14 17:12:26 +03:00
This commit is contained in:
metinseylan
2016-12-31 01:18:10 +03:00
parent c7491fc762
commit 8e0100be09
8 changed files with 143 additions and 86 deletions
Regular → Executable
+1
View File
@@ -45,4 +45,5 @@ export default new class {
}
return false;
}
}
Regular → Executable
+22 -10
View File
@@ -3,33 +3,43 @@ import Emitter from './Emitter'
export default {
install(Vue, connection){
install(Vue, connection, store){
if(!connection) throw new Error("[Vue-Socket.io] cannot locate connection")
let observer = new Observer(connection)
let observer = new Observer(connection, store)
Vue.prototype.$socket = observer.Socket;
Vue.mixin({
beforeCreate(){
let _this = this;
let sockets = this.$options['sockets']
this.$options.sockets = new Proxy({}, {
set: (target, key, value) => {
Emitter.addListener(key, value, this)
target[key] = value
return true;
},
deleteProperty: (target, key) => {
Emitter.removeListener(key, this.$options.sockets[key], this)
delete target.key;
return true
}
})
if(sockets){
Object.keys(sockets).forEach(function(key) {
Emitter.addListener(key, sockets[key], _this)
Object.keys(sockets).forEach((key) => {
this.$options.sockets[key] = sockets[key];
});
}
},
beforeDestroy(){
let _this = this;
let sockets = this.$options['sockets']
if(sockets){
Object.keys(sockets).forEach(function(key) {
Emitter.removeListener(key, sockets[key], _this)
Object.keys(sockets).forEach((key) => {
delete this.$options.sockets[key]
});
}
}
@@ -37,4 +47,6 @@ export default {
}
}
}
Regular → Executable
+22 -4
View File
@@ -3,7 +3,7 @@ import Socket from 'socket.io-client'
export default class{
constructor(connection) {
constructor(connection, store) {
if(typeof connection == 'string'){
this.Socket = Socket(connection);
@@ -11,23 +11,41 @@ export default class{
this.Socket = connection
}
if(store) this.store = store;
this.onEvent()
}
onEvent(){
this.Socket.onevent = (packet) => {
Emitter.emit(packet.data[0], packet.data[1])
}
Emitter.emit(packet.data[0], packet.data[1]);
if(this.store) this.commitStore('SOCKET_'+packet.data[0], packet.data[1])
};
let _this = this;
["connect", "error", "disconnect", "reconnect", "reconnect_attempt", "reconnecting", "reconnect_error", "reconnect_failed", "connect_error", "connect_timeout", "connecting", "ping", "pong"]
.forEach((value) => {
_this.Socket.on(value, (data) => {
Emitter.emit(value, data)
Emitter.emit(value, data);
if(_this.store) _this.commitStore('SOCKET_'+value.toUpperCase(), data)
})
})
}
commitStore(type, payload){
if(type.split('_')[0].toUpperCase() === 'SOCKET'){
if(this.store._mutations[type])
this.store.commit(type, payload)
}
}
}