1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import { dispatch } from '../store'
import types from '../types'
import { socket } from './socket.connection'
socket.on('api_res', (data) => {
// console.log('system response', data)
const type = types[data.datatype]
console.log('api_res', data.type, data.datatype)
if (! type) return console.error('socket:api_res bad datatype', data.datatype)
switch (data.type) {
case 'create':
return dispatch({
type: type.create,
source: 'socket',
data: parse(data.data),
})
case 'update':
return dispatch({
type: type.update,
source: 'socket',
data: parse(data.data),
})
case 'destroy':
return dispatch({
type: type.destroy,
source: 'socket',
data: parse(data.data),
})
default:
break
}
})
function parse (s) {
if (typeof s === 'string') {
try {
const d = JSON.parse(s)
return d
} catch (e) {
console.error('not valid json')
return s
}
}
return s
}
|