Clean and standardize code
parent
2f17ac9a17
commit
4d320484e0
File diff suppressed because one or more lines are too long
@ -1,51 +1,52 @@
|
|||||||
import Emitter from './Emitter'
|
import Emitter from './Emitter'
|
||||||
import Socket from 'socket.io-client'
|
import Socket from 'socket.io-client'
|
||||||
|
|
||||||
export default class{
|
export default class {
|
||||||
|
|
||||||
constructor(connection, store) {
|
constructor(connection, store) {
|
||||||
|
if (typeof connection === 'string') {
|
||||||
if(typeof connection == 'string'){
|
this.Socket = Socket(connection)
|
||||||
this.Socket = Socket(connection);
|
} else {
|
||||||
}else{
|
|
||||||
this.Socket = connection
|
this.Socket = connection
|
||||||
}
|
}
|
||||||
|
|
||||||
if(store) this.store = store;
|
if (store) this.store = store
|
||||||
|
|
||||||
this.onEvent()
|
this.onEvent()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onEvent(){
|
onEvent() {
|
||||||
this.Socket.onevent = (packet) => {
|
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])
|
||||||
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"]
|
const eventsArray = [
|
||||||
.forEach((value) => {
|
'connect',
|
||||||
_this.Socket.on(value, (data) => {
|
'error',
|
||||||
Emitter.emit(value, data);
|
'disconnect',
|
||||||
if(_this.store) _this.commitStore('SOCKET_'+value.toUpperCase(), data)
|
'reconnect',
|
||||||
})
|
'reconnect_attempt',
|
||||||
|
'reconnecting',
|
||||||
|
'reconnect_error',
|
||||||
|
'reconnect_failed',
|
||||||
|
'connect_error',
|
||||||
|
'connect_timeout',
|
||||||
|
'connecting',
|
||||||
|
'ping',
|
||||||
|
'pong'
|
||||||
|
]
|
||||||
|
|
||||||
|
eventsArray.forEach((value) => {
|
||||||
|
this.Socket.on(value, (data) => {
|
||||||
|
Emitter.emit(value, data);
|
||||||
|
if (this.store) _this.commitStore('SOCKET_' + value.toUpperCase(), data)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commitStore(type, payload) {
|
||||||
commitStore(type, payload){
|
if (type.split('_')[0].toUpperCase() === 'SOCKET') {
|
||||||
|
if (this.store._mutations[type]) this.store.commit(type, payload)
|
||||||
if(type.split('_')[0].toUpperCase() === 'SOCKET'){
|
|
||||||
|
|
||||||
if(this.store._mutations[type])
|
|
||||||
this.store.commit(type, payload)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue