mirror of https://github.com/status-im/codimd.git
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:
parent
b33c91f41d
commit
c4276e1c28
|
@ -773,6 +773,21 @@ class SocketClient {
|
|||
this.socket.on('cursor blur', this.cursorBlurEventHandlder.bind(this))
|
||||
// check version
|
||||
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 () {
|
||||
|
@ -785,6 +800,10 @@ class SocketClient {
|
|||
return getUserFromUserPool(this.socket.id)
|
||||
}
|
||||
|
||||
getCurrentNote () {
|
||||
return getNoteFromNotePool(this.socket.noteId)
|
||||
}
|
||||
|
||||
getNoteChannel () {
|
||||
return this.socket.broadcast.to(this.socket.noteId)
|
||||
}
|
||||
|
@ -990,23 +1009,6 @@ function connection (socket) {
|
|||
updateUserData(socket, user)
|
||||
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
|
||||
|
|
|
@ -446,7 +446,7 @@ describe('realtime', function () {
|
|||
|
||||
describe('socket event', function () {
|
||||
let realtime
|
||||
const noteId = "note123"
|
||||
const noteId = 'note123'
|
||||
let clientSocket
|
||||
const eventFuncMap = new Map()
|
||||
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 () {
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue