-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.js
38 lines (31 loc) · 801 Bytes
/
client.js
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
const { State, loadActionsInfo } = require('./lib')
const socket = require('./socket')
class ClientState extends State {
addAction (action) {
super.addAction(action)
const actionsInfo = JSON.stringify([action])
socket.emit('client-actions', actionsInfo)
}
serverAddAction (action) {
this.addAction(action)
}
}
let clientState = null
socket.on('client-init', (info) => {
const { uid, timeStamp, actionsInfo } = info
clientState = new ClientState(timeStamp, uid)
const actions = loadActionsInfo(actionsInfo)
for (let action of actions) {
clientState.serverAddAction(action)
}
})
const clientInit = function () {
socket.emit('client-connect')
}
const getClientState = function () {
return clientState
}
module.exports = {
getClientState,
clientInit
}