From ff72d99269cc64074ef17c652f0989b074bbd6ba Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Fri, 24 May 2019 18:05:16 +0800 Subject: [PATCH] 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 --- lib/realtime.js | 64 +++++++++++++++++++++++-------------------- test/realtime.test.js | 54 +++++++++++++++++++++++++++++++++--- 2 files changed, 85 insertions(+), 33 deletions(-) diff --git a/lib/realtime.js b/lib/realtime.js index 2c3bd973..fef1de2c 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -775,6 +775,40 @@ class SocketClient { this.socket.on('version', this.checkVersionEventHandler.bind(this)) // received sync of online users request this.socket.on('online users', this.onlineUsersEventHandler.bind(this)) + // reveiced when user logout or changed + this.socket.on('user changed', this.userChangedEventHandler.bind(this)) + } + + userChangedEventHandler () { + logger.info('user changed') + + const note = this.getCurrentNote() + if (!note) return + const user = note.users[this.socket.id] + if (!user) return + + exports.updateUserData(this.socket, user) + exports.emitOnlineUsers(this.socket) + } + + getCurrentUser () { + if (!this.socket.id) return + return getUserFromUserPool(this.socket.id) + } + + getCurrentNote () { + if (!this.socket.noteId) return + return getNoteFromNotePool(this.socket.noteId) + } + + getNoteChannel () { + return this.socket.broadcast.to(this.socket.noteId) + } + + isNoteAndUserExists () { + const note = getNoteFromNotePool(this.socket.noteId) + const user = getUserFromUserPool(this.socket.id) + return note && user } onlineUsersEventHandler () { @@ -790,24 +824,6 @@ class SocketClient { }) } - isNoteAndUserExists () { - const note = getNoteFromNotePool(this.socket.noteId) - const user = getUserFromUserPool(this.socket.id) - return note && user - } - - getCurrentUser () { - return getUserFromUserPool(this.socket.id) - } - - getCurrentNote () { - return getNoteFromNotePool(this.socket.noteId) - } - - getNoteChannel () { - return this.socket.broadcast.to(this.socket.noteId) - } - cursorFocusEventHandler (data) { if (!this.isNoteAndUserExists()) return const user = this.getCurrentUser() @@ -998,17 +1014,6 @@ function connection (socket) { } } }) - - // reveiced when user logout or changed - socket.on('user changed', function () { - logger.info('user changed') - var noteId = socket.noteId - if (!noteId || !notes[noteId]) return - var user = notes[noteId].users[socket.id] - if (!user) return - updateUserData(socket, user) - emitOnlineUsers(socket) - }) } exports = module.exports = realtime @@ -1023,6 +1028,7 @@ exports.startConnection = startConnection exports.emitRefresh = emitRefresh exports.emitUserStatus = emitUserStatus exports.disconnect = disconnect +exports.emitOnlineUsers = emitOnlineUsers exports.notes = notes exports.users = users exports.disconnectSocketQueue = disconnectSocketQueue diff --git a/test/realtime.test.js b/test/realtime.test.js index f9e0091d..e3d00f5d 100644 --- a/test/realtime.test.js +++ b/test/realtime.test.js @@ -452,7 +452,8 @@ describe('realtime', function () { beforeEach(() => { mock('../lib/logger', { error: () => { - } + }, + info: () => {} }) mock('../lib/history', {}) mock('../lib/models', { @@ -476,10 +477,16 @@ describe('realtime', function () { /* eslint-disable-next-line */ callback(null, noteId) }) - sinon.stub(realtime, 'failConnection') - sinon.stub(realtime, 'updateUserData') - sinon.stub(realtime, 'startConnection') + const wrappedFuncs = [] + wrappedFuncs.push(sinon.stub(realtime, 'failConnection')) + wrappedFuncs.push(sinon.stub(realtime, 'updateUserData')) + wrappedFuncs.push(sinon.stub(realtime, 'startConnection')) realtime.connection(clientSocket) + + wrappedFuncs.forEach((wrappedFunc) => { + wrappedFunc.restore() + }) + }) afterEach(() => { @@ -655,5 +662,44 @@ describe('realtime', function () { }) }) + describe('user changed', function () { + it('should call updateUserData', () => { + const userChangedFunc = eventFuncMap.get('user changed') + realtime.notes[noteId] = { + users: { + [clientSocket.id]: {} + } + } + const updateUserDataStub = sinon.stub(realtime, 'updateUserData') + const emitOnlineUsersStub = sinon.stub(realtime, 'emitOnlineUsers') + userChangedFunc() + assert(updateUserDataStub.calledOnce) + assert(emitOnlineUsersStub.calledOnce) + }) + + it('should direct return when note not exists', () => { + const userChangedFunc = eventFuncMap.get('user changed') + const updateUserDataStub = sinon.stub(realtime, 'updateUserData') + const emitOnlineUsersStub = sinon.stub(realtime, 'emitOnlineUsers') + userChangedFunc() + assert(updateUserDataStub.called === false) + assert(emitOnlineUsersStub.called === false) + }) + + it('should direct return when note not exists', () => { + const userChangedFunc = eventFuncMap.get('user changed') + realtime.notes[noteId] = { + users: { + } + } + delete realtime.users[clientSocket.id] + const updateUserDataStub = sinon.stub(realtime, 'updateUserData') + const emitOnlineUsersStub = sinon.stub(realtime, 'emitOnlineUsers') + userChangedFunc() + assert(updateUserDataStub.called === false) + assert(emitOnlineUsersStub.called === false) + }) + }) + }) })