refactor(realtime): extract user event "online users" to SocketClient

1. extract user event "online users" to SocketClient
2. add test case for that

Signed-off-by: BoHong Li <a60814billy@gmail.com>
This commit is contained in:
BoHong Li 2019-05-03 22:20:30 +08:00
parent b33c91f41d
commit c4276e1c28
No known key found for this signature in database
GPG Key ID: 9696D5590D58290F
2 changed files with 50 additions and 19 deletions

View File

@ -773,6 +773,21 @@ class SocketClient {
this.socket.on('cursor blur', this.cursorBlurEventHandlder.bind(this)) this.socket.on('cursor blur', this.cursorBlurEventHandlder.bind(this))
// check version // check version
this.socket.on('version', this.checkVersionEventHandler.bind(this)) this.socket.on('version', this.checkVersionEventHandler.bind(this))
// received sync of online users request
this.socket.on('online users', this.onlineUsersEventHandler.bind(this))
}
onlineUsersEventHandler () {
if (!this.isNoteAndUserExists()) return
const currentNote = this.getCurrentNote()
const currentNoteOnlineUserList = Object.keys(currentNote.users)
.map(key => buildUserOutData(currentNote.users[key]))
this.socket.emit('online users', {
users: currentNoteOnlineUserList
})
} }
isNoteAndUserExists () { isNoteAndUserExists () {
@ -785,6 +800,10 @@ class SocketClient {
return getUserFromUserPool(this.socket.id) return getUserFromUserPool(this.socket.id)
} }
getCurrentNote () {
return getNoteFromNotePool(this.socket.noteId)
}
getNoteChannel () { getNoteChannel () {
return this.socket.broadcast.to(this.socket.noteId) return this.socket.broadcast.to(this.socket.noteId)
} }
@ -990,23 +1009,6 @@ function connection (socket) {
updateUserData(socket, user) updateUserData(socket, user)
emitOnlineUsers(socket) emitOnlineUsers(socket)
}) })
// received sync of online users request
socket.on('online users', function () {
var noteId = socket.noteId
if (!noteId || !notes[noteId]) return
var users = []
Object.keys(notes[noteId].users).forEach(function (key) {
var user = notes[noteId].users[key]
if (user) {
users.push(buildUserOutData(user))
}
})
var out = {
users: users
}
socket.emit('online users', out)
})
} }
exports = module.exports = realtime exports = module.exports = realtime

View File

@ -446,7 +446,7 @@ describe('realtime', function () {
describe('socket event', function () { describe('socket event', function () {
let realtime let realtime
const noteId = "note123" const noteId = 'note123'
let clientSocket let clientSocket
const eventFuncMap = new Map() const eventFuncMap = new Map()
beforeEach(() => { beforeEach(() => {
@ -572,7 +572,7 @@ describe('realtime', function () {
}) })
}) })
;['cursor focus', 'cursor activity', 'cursor blur'].forEach( (event) => { ;['cursor focus', 'cursor activity', 'cursor blur'].forEach((event) => {
describe(event, function () { describe(event, function () {
let cursorFocusFunc let cursorFocusFunc
@ -626,5 +626,34 @@ describe('realtime', function () {
}) })
}) })
describe('online users', function () {
it('should return online user list', () => {
const onlineUsersFunc = eventFuncMap.get('online users')
realtime.notes[noteId] = {
users: {
10: {
id: 10
},
20: {
id: 20
}
}
}
onlineUsersFunc()
assert(clientSocket.emit.called)
assert(clientSocket.emit.lastCall.args[0] === 'online users')
let returnUserList = clientSocket.emit.lastCall.args[1].users
assert(returnUserList.length === 2)
assert(returnUserList[0].id === 10)
assert(returnUserList[1].id === 20)
})
it('should not return user list when note not exists', () => {
const onlineUsersFunc = eventFuncMap.get('online users')
onlineUsersFunc()
assert(clientSocket.emit.called === false)
})
})
}) })
}) })