-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (95 loc) · 3.16 KB
/
index.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const app = require('express')()
const cors = require('cors')
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const { randomId, randomGuid, getDelay, stringify } = require('./utils')
const argv = require('yargs')
.alias('v', 'verbose')
.option('port', {
default: 3000,
alias: 'p'
}).option('delay', {
default: 2000,
alias: 'd'
}).argv;
const todomvcSvc = require('./todomvc-svc').getStore()
const initialTodos = require('./default.json')
todomvcSvc.init(initialTodos)
const clients = []
app.use(cors())
app.get('/todos', function (req, res) {
// res.sendFile(__dirname + '/index.html');
res.json(todomvcSvc.getAllItems())
});
const delay = getDelay(argv.delay)
console.info(`TodoMVC service listening on port ${argv.port}`)
console.info(`with delay: ${argv.delay}ms`)
console.info()
io.on('connection', (socket) => {
const logClients = () => console.info(`CONNECTION > Total number of clients: ${clients.length}`)
// const subscribe = () => {
let id = randomGuid()
const client = { id, socket }
clients.push(client)
console.info(`CONNECTION > Client id:${id} connected`)
logClients()
// }
// subscribe()
const unsubscribe = (id) => {
let idx = clients.findIndex(c => c.id === id)
clients.splice(idx, 1)
console.info(`CONNECTION > Client id:${id} disconnected`)
logClients()
}
socket.on('cmd', (cmd, ackFn) => {
const requestID = randomId();
console.info(`Request with local ID:${requestID}, received: ${stringify(cmd)}`)
let item
try {
switch(cmd.type){
case "DELETE":
item = todomvcSvc.deleteItem(cmd.payload.id)
delay(() => {
if (ackFn) {
ackFn(item)
console.info(`Responding to sender, request ID:${requestID}: ${stringify(item)}`)
}
console.info(`Broadcasting request ID:${requestID} starting`)
socket.broadcast.emit('cmd', cmd)
console.info(`Broadcasting request ID:${requestID} completed`)
})
break
case "ADD":
item = todomvcSvc.addItem(cmd.payload.title)
delay(() => {
if (ackFn) {
ackFn(item)
console.info(`Responding to sender: ${stringify(item)}`)
}
console.info(`Broadcasting request ID:${requestID} starting`)
socket.broadcast.emit('cmd', { ...cmd, payload: item })
console.info(`Broadcasting request ID:${requestID} completed`)
})
break
case "UPDATE":
item = todomvcSvc.updateItem(cmd.payload.id, cmd.payload.data)
delay(() => {
if (ackFn) {
ackFn(item)
console.info(`Responding to sender: ${stringify(item)}`)
}
console.info(`Broadcasting request ID:${requestID} starting`)
socket.broadcast.emit('cmd', cmd)
console.info(`Broadcasting request ID:${requestID} completed`)
})
break
}
} catch (e) {
console.error('Error!', e)
}
})
socket.on('disconnect', function () {
unsubscribe(id)
});
})
server.listen(argv.port)