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 Socket from 'socket.io-client'
|
||||
|
||||
export default class{
|
||||
|
||||
export default class {
|
||||
constructor(connection, store) {
|
||||
|
||||
if(typeof connection == 'string'){
|
||||
this.Socket = Socket(connection);
|
||||
}else{
|
||||
if (typeof connection === 'string') {
|
||||
this.Socket = Socket(connection)
|
||||
} else {
|
||||
this.Socket = connection
|
||||
}
|
||||
|
||||
if(store) this.store = store;
|
||||
if (store) this.store = store
|
||||
|
||||
this.onEvent()
|
||||
|
||||
}
|
||||
|
||||
onEvent(){
|
||||
onEvent() {
|
||||
this.Socket.onevent = (packet) => {
|
||||
Emitter.emit(packet.data[0], packet.data[1]);
|
||||
|
||||
if(this.store) this.commitStore('SOCKET_'+packet.data[0], packet.data[1])
|
||||
|
||||
};
|
||||
|
||||
let _this = this;
|
||||
Emitter.emit(packet.data[0], packet.data[1])
|
||||
if (this.store) this.commitStore('SOCKET_' + packet.data[0], packet.data[1])
|
||||
}
|
||||
|
||||
["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);
|
||||
if(_this.store) _this.commitStore('SOCKET_'+value.toUpperCase(), data)
|
||||
})
|
||||
const eventsArray = [
|
||||
'connect',
|
||||
'error',
|
||||
'disconnect',
|
||||
'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){
|
||||
|
||||
if(type.split('_')[0].toUpperCase() === 'SOCKET'){
|
||||
|
||||
if(this.store._mutations[type])
|
||||
this.store.commit(type, payload)
|
||||
|
||||
commitStore(type, payload) {
|
||||
if (type.split('_')[0].toUpperCase() === 'SOCKET') {
|
||||
if (this.store._mutations[type]) this.store.commit(type, payload)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue