Skip to content

Commit c4276e1

Browse files
committed
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 <[email protected]>
1 parent b33c91f commit c4276e1

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

lib/realtime.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,21 @@ class SocketClient {
773773
this.socket.on('cursor blur', this.cursorBlurEventHandlder.bind(this))
774774
// check version
775775
this.socket.on('version', this.checkVersionEventHandler.bind(this))
776+
// received sync of online users request
777+
this.socket.on('online users', this.onlineUsersEventHandler.bind(this))
778+
}
779+
780+
onlineUsersEventHandler () {
781+
if (!this.isNoteAndUserExists()) return
782+
783+
const currentNote = this.getCurrentNote()
784+
785+
const currentNoteOnlineUserList = Object.keys(currentNote.users)
786+
.map(key => buildUserOutData(currentNote.users[key]))
787+
788+
this.socket.emit('online users', {
789+
users: currentNoteOnlineUserList
790+
})
776791
}
777792

778793
isNoteAndUserExists () {
@@ -785,6 +800,10 @@ class SocketClient {
785800
return getUserFromUserPool(this.socket.id)
786801
}
787802

803+
getCurrentNote () {
804+
return getNoteFromNotePool(this.socket.noteId)
805+
}
806+
788807
getNoteChannel () {
789808
return this.socket.broadcast.to(this.socket.noteId)
790809
}
@@ -990,23 +1009,6 @@ function connection (socket) {
9901009
updateUserData(socket, user)
9911010
emitOnlineUsers(socket)
9921011
})
993-
994-
// received sync of online users request
995-
socket.on('online users', function () {
996-
var noteId = socket.noteId
997-
if (!noteId || !notes[noteId]) return
998-
var users = []
999-
Object.keys(notes[noteId].users).forEach(function (key) {
1000-
var user = notes[noteId].users[key]
1001-
if (user) {
1002-
users.push(buildUserOutData(user))
1003-
}
1004-
})
1005-
var out = {
1006-
users: users
1007-
}
1008-
socket.emit('online users', out)
1009-
})
10101012
}
10111013

10121014
exports = module.exports = realtime

test/realtime.test.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ describe('realtime', function () {
446446

447447
describe('socket event', function () {
448448
let realtime
449-
const noteId = "note123"
449+
const noteId = 'note123'
450450
let clientSocket
451451
const eventFuncMap = new Map()
452452
beforeEach(() => {
@@ -572,7 +572,7 @@ describe('realtime', function () {
572572
})
573573
})
574574

575-
;['cursor focus', 'cursor activity', 'cursor blur'].forEach( (event) => {
575+
;['cursor focus', 'cursor activity', 'cursor blur'].forEach((event) => {
576576
describe(event, function () {
577577
let cursorFocusFunc
578578

@@ -626,5 +626,34 @@ describe('realtime', function () {
626626
})
627627
})
628628

629+
describe('online users', function () {
630+
it('should return online user list', () => {
631+
const onlineUsersFunc = eventFuncMap.get('online users')
632+
realtime.notes[noteId] = {
633+
users: {
634+
10: {
635+
id: 10
636+
},
637+
20: {
638+
id: 20
639+
}
640+
}
641+
}
642+
onlineUsersFunc()
643+
assert(clientSocket.emit.called)
644+
assert(clientSocket.emit.lastCall.args[0] === 'online users')
645+
let returnUserList = clientSocket.emit.lastCall.args[1].users
646+
assert(returnUserList.length === 2)
647+
assert(returnUserList[0].id === 10)
648+
assert(returnUserList[1].id === 20)
649+
})
650+
651+
it('should not return user list when note not exists', () => {
652+
const onlineUsersFunc = eventFuncMap.get('online users')
653+
onlineUsersFunc()
654+
assert(clientSocket.emit.called === false)
655+
})
656+
})
657+
629658
})
630659
})

0 commit comments

Comments
 (0)