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
c4276e1c28
commit
ff72d99269
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue