Skip to content

Commit 8171860

Browse files
committed
refactor(realtime): extract user event "disconnect" to ClientSocket
1. extract user event "disconnect" to ClientSocket 2. add test case Signed-off-by: BoHong Li <[email protected]>
1 parent 48cebc0 commit 8171860

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

lib/realtime.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,10 @@ class SocketClient {
763763
this.socket.on('refresh', this.refreshEventHandler.bind(this))
764764
// received user status
765765
this.socket.on('user status', this.userStatusEventHandler.bind(this))
766+
767+
// when a new client disconnect
768+
this.socket.on('disconnect', this.disconnectEventHandler.bind(this))
769+
766770
}
767771

768772
refreshEventHandler () {
@@ -781,6 +785,12 @@ class SocketClient {
781785
}
782786
exports.emitUserStatus(this.socket)
783787
}
788+
789+
disconnectEventHandler () {
790+
if (isDuplicatedInSocketQueue(disconnectSocketQueue, this.socket)) return
791+
disconnectSocketQueue.push(this.socket)
792+
exports.disconnect(this.socket)
793+
}
784794
}
785795

786796
function connection (socket) {
@@ -983,13 +993,6 @@ function connection (socket) {
983993
}
984994
socket.broadcast.to(noteId).emit('cursor blur', out)
985995
})
986-
987-
// when a new client disconnect
988-
socket.on('disconnect', function () {
989-
if (isDuplicatedInSocketQueue(socket, disconnectSocketQueue)) return
990-
disconnectSocketQueue.push(socket)
991-
disconnect(socket)
992-
})
993996
}
994997

995998
exports = module.exports = realtime
@@ -1003,5 +1006,7 @@ exports.updateUserData = updateUserData
10031006
exports.startConnection = startConnection
10041007
exports.emitRefresh = emitRefresh
10051008
exports.emitUserStatus = emitUserStatus
1009+
exports.disconnect = disconnect
10061010
exports.notes = notes
10071011
exports.users = users
1012+
exports.disconnectSocketQueue = disconnectSocketQueue

test/realtime.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,25 @@ describe('realtime', function () {
537537

538538
})
539539

540+
describe('disconnect', function () {
541+
it('should push socket to disconnect queue and call disconnect function', () => {
542+
const disconnectFunc = eventFuncMap.get('disconnect')
543+
const disconnectStub = sinon.stub(realtime, 'disconnect')
544+
disconnectFunc()
545+
assert(realtime.disconnectSocketQueue.length === 1)
546+
assert(disconnectStub.calledOnce)
547+
})
548+
549+
it('should quick return when socket is in disconnect queue', () => {
550+
const disconnectFunc = eventFuncMap.get('disconnect')
551+
const disconnectStub = sinon.stub(realtime, 'disconnect')
552+
realtime.disconnectSocketQueue.push(clientSocket)
553+
disconnectFunc()
554+
assert(disconnectStub.called === false)
555+
})
556+
557+
})
558+
540559
})
541560

542561
})

0 commit comments

Comments
 (0)