2
0
mirror of https://github.com/tenrok/vue-native-websocket.git synced 2026-06-12 15:02:25 +03:00

native websocket with json parsing, vuex integration and namespacing

This commit is contained in:
nathan
2017-05-16 22:58:01 -07:00
parent d44a480584
commit ace980d539
6 changed files with 227 additions and 192 deletions
+34 -39
View File
@@ -3,50 +3,45 @@ import Emitter from './Emitter'
export default {
install(Vue, connection, store){
install (Vue, connection, store, opts = {}) {
if (!connection) { throw new Error('[vue-native-socket] cannot locate connection') }
if(!connection) throw new Error("[Vue-Socket.io] cannot locate connection")
let observer = new Observer(connection, store, opts)
let observer = new Observer(connection, store)
Vue.prototype.$socket = observer.WebSocket
Vue.prototype.$socket = observer.Socket;
Vue.mixin({
created () {
let sockets = this.$options['sockets']
Vue.mixin({
created(){
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((key) => {
this.$options.sockets[key] = sockets[key];
});
}
},
beforeDestroy(){
let sockets = this.$options['sockets']
if(sockets){
Object.keys(sockets).forEach((key) => {
delete this.$options.sockets[key]
});
}
}
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((key) => {
this.$options.sockets[key] = sockets[key]
})
}
},
beforeDestroy () {
let sockets = this.$options['sockets']
if (sockets) {
Object.keys(sockets).forEach((key) => {
delete this.$options.sockets[key]
})
}
}
})
}
}