Skip to content

Commit ff72d99

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 c4276e1 commit ff72d99

File tree

2 files changed

+82
-30
lines changed

2 files changed

+82
-30
lines changed

lib/realtime.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -775,39 +775,55 @@ class SocketClient {
775775
this.socket.on('version', this.checkVersionEventHandler.bind(this))
776776
// received sync of online users request
777777
this.socket.on('online users', this.onlineUsersEventHandler.bind(this))
778+
// reveiced when user logout or changed
779+
this.socket.on('user changed', this.userChangedEventHandler.bind(this))
778780
}
779781

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]))
782+
userChangedEventHandler () {
783+
logger.info('user changed')
787784

788-
this.socket.emit('online users', {
789-
users: currentNoteOnlineUserList
790-
})
791-
}
785+
const note = this.getCurrentNote()
786+
if (!note) return
787+
const user = note.users[this.socket.id]
788+
if (!user) return
792789

793-
isNoteAndUserExists () {
794-
const note = getNoteFromNotePool(this.socket.noteId)
795-
const user = getUserFromUserPool(this.socket.id)
796-
return note && user
790+
exports.updateUserData(this.socket, user)
791+
exports.emitOnlineUsers(this.socket)
797792
}
798793

799794
getCurrentUser () {
795+
if (!this.socket.id) return
800796
return getUserFromUserPool(this.socket.id)
801797
}
802798

803799
getCurrentNote () {
800+
if (!this.socket.noteId) return
804801
return getNoteFromNotePool(this.socket.noteId)
805802
}
806803

807804
getNoteChannel () {
808805
return this.socket.broadcast.to(this.socket.noteId)
809806
}
810807

808+
isNoteAndUserExists () {
809+
const note = getNoteFromNotePool(this.socket.noteId)
810+
const user = getUserFromUserPool(this.socket.id)
811+
return note && user
812+
}
813+
814+
onlineUsersEventHandler () {
815+
if (!this.isNoteAndUserExists()) return
816+
817+
const currentNote = this.getCurrentNote()
818+
819+
const currentNoteOnlineUserList = Object.keys(currentNote.users)
820+
.map(key => buildUserOutData(currentNote.users[key]))
821+
822+
this.socket.emit('online users', {
823+
users: currentNoteOnlineUserList
824+
})
825+
}
826+
811827
cursorFocusEventHandler (data) {
812828
if (!this.isNoteAndUserExists()) return
813829
const user = this.getCurrentUser()
@@ -998,17 +1014,6 @@ function connection (socket) {
9981014
}
9991015
}
10001016
})
1001-
1002-
// reveiced when user logout or changed
1003-
socket.on('user changed', function () {
1004-
logger.info('user changed')
1005-
var noteId = socket.noteId
1006-
if (!noteId || !notes[noteId]) return
1007-
var user = notes[noteId].users[socket.id]
1008-
if (!user) return
1009-
updateUserData(socket, user)
1010-
emitOnlineUsers(socket)
1011-
})
10121017
}
10131018

10141019
exports = module.exports = realtime
@@ -1023,6 +1028,7 @@ exports.startConnection = startConnection
10231028
exports.emitRefresh = emitRefresh
10241029
exports.emitUserStatus = emitUserStatus
10251030
exports.disconnect = disconnect
1031+
exports.emitOnlineUsers = emitOnlineUsers
10261032
exports.notes = notes
10271033
exports.users = users
10281034
exports.disconnectSocketQueue = disconnectSocketQueue

test/realtime.test.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ describe('realtime', function () {
452452
beforeEach(() => {
453453
mock('../lib/logger', {
454454
error: () => {
455-
}
455+
},
456+
info: () => {}
456457
})
457458
mock('../lib/history', {})
458459
mock('../lib/models', {
@@ -476,10 +477,16 @@ describe('realtime', function () {
476477
/* eslint-disable-next-line */
477478
callback(null, noteId)
478479
})
479-
sinon.stub(realtime, 'failConnection')
480-
sinon.stub(realtime, 'updateUserData')
481-
sinon.stub(realtime, 'startConnection')
480+
const wrappedFuncs = []
481+
wrappedFuncs.push(sinon.stub(realtime, 'failConnection'))
482+
wrappedFuncs.push(sinon.stub(realtime, 'updateUserData'))
483+
wrappedFuncs.push(sinon.stub(realtime, 'startConnection'))
482484
realtime.connection(clientSocket)
485+
486+
wrappedFuncs.forEach((wrappedFunc) => {
487+
wrappedFunc.restore()
488+
})
489+
483490
})
484491

485492
afterEach(() => {
@@ -655,5 +662,44 @@ describe('realtime', function () {
655662
})
656663
})
657664

665+
describe('user changed', function () {
666+
it('should call updateUserData', () => {
667+
const userChangedFunc = eventFuncMap.get('user changed')
668+
realtime.notes[noteId] = {
669+
users: {
670+
[clientSocket.id]: {}
671+
}
672+
}
673+
const updateUserDataStub = sinon.stub(realtime, 'updateUserData')
674+
const emitOnlineUsersStub = sinon.stub(realtime, 'emitOnlineUsers')
675+
userChangedFunc()
676+
assert(updateUserDataStub.calledOnce)
677+
assert(emitOnlineUsersStub.calledOnce)
678+
})
679+
680+
it('should direct return when note not exists', () => {
681+
const userChangedFunc = eventFuncMap.get('user changed')
682+
const updateUserDataStub = sinon.stub(realtime, 'updateUserData')
683+
const emitOnlineUsersStub = sinon.stub(realtime, 'emitOnlineUsers')
684+
userChangedFunc()
685+
assert(updateUserDataStub.called === false)
686+
assert(emitOnlineUsersStub.called === false)
687+
})
688+
689+
it('should direct return when note not exists', () => {
690+
const userChangedFunc = eventFuncMap.get('user changed')
691+
realtime.notes[noteId] = {
692+
users: {
693+
}
694+
}
695+
delete realtime.users[clientSocket.id]
696+
const updateUserDataStub = sinon.stub(realtime, 'updateUserData')
697+
const emitOnlineUsersStub = sinon.stub(realtime, 'emitOnlineUsers')
698+
userChangedFunc()
699+
assert(updateUserDataStub.called === false)
700+
assert(emitOnlineUsersStub.called === false)
701+
})
702+
})
703+
658704
})
659705
})

0 commit comments

Comments
 (0)