Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
feat: new event to update the guest name
Browse files Browse the repository at this point in the history
also: refactor the callbacks, and fix a bug (now -> Date.now)
  • Loading branch information
severo committed Jan 16, 2020
1 parent 45b0622 commit fb948e1
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ const timestamp = guest => {
const t = Date.now()
setGuest({ ...guest, updatedDate: t, expirationDate: t + 1000 * 60 * 10 })
}
const onNewGuest = socket => (guest, ack) => {
guest.sId = socket.id
guest.name = guest.name || `Guest_${Date.now()}`
guest.color = guest.color || rnd({ luminosity: 'dark' })
setGuest(guest)
timestamp(guest)
emitListGuests()
ack(guest)
}
const onUpdateGuest = socket => (guest, ack) => {
// Used to update the name or the color
// For now, there is no difference with onNewGuest
onNewGuest(socket)(guest, ack)
}
const onByeBye = socket => _ => {
deleteGuest(socket.id)
emitListGuests()
}
const emitListGuests = () => {
// Clean the list, pruning guests that are inactive for more than 10min
const now = Date.now()
Expand All @@ -35,19 +53,9 @@ const emitListGuests = () => {
}
const occupappBeta = io.of('/occupapp-beta').on('connection', socket => {
console.log('New occupapp user connected')
socket.on('new-guest', (guest, ack) => {
guest.name = guest.name || `Guest_${now()}`
guest.sId = socket.id
guest.color = rnd({ luminosity: 'dark' })
setGuest(guest)
timestamp(guest)
emitListGuests()
ack(guest)
})
socket.on('bye-bye', _ => {
deleteGuest(socket.id)
emitListGuests()
})
socket.on('new-guest', onNewGuest(socket))
socket.on('update-guest', onUpdateGuest(socket))
socket.on('bye-bye', onByeBye(socket))
})

http.listen(port, () => console.log('listening on port ' + port))

0 comments on commit fb948e1

Please sign in to comment.