Skip to content

Commit b33c91f

Browse files
committed
refactor(realtime): extract user event "version" to SocketClient
1. extract user event "version" to SocketClient 2. add test case for that Signed-off-by: BoHong Li <[email protected]>
1 parent 3259bd0 commit b33c91f

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

lib/realtime.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ class SocketClient {
771771
this.socket.on('cursor activity', this.cursorActivityEventHandler.bind(this))
772772
// received cursor blur
773773
this.socket.on('cursor blur', this.cursorBlurEventHandlder.bind(this))
774+
// check version
775+
this.socket.on('version', this.checkVersionEventHandler.bind(this))
774776
}
775777

776778
isNoteAndUserExists () {
@@ -812,6 +814,13 @@ class SocketClient {
812814
})
813815
}
814816

817+
checkVersionEventHandler () {
818+
this.socket.emit('version', {
819+
version: config.fullversion,
820+
minimumCompatibleVersion: config.minimumCompatibleVersion
821+
})
822+
}
823+
815824
refreshEventHandler () {
816825
exports.emitRefresh(this.socket)
817826
}
@@ -998,14 +1007,6 @@ function connection (socket) {
9981007
}
9991008
socket.emit('online users', out)
10001009
})
1001-
1002-
// check version
1003-
socket.on('version', function () {
1004-
socket.emit('version', {
1005-
version: config.fullversion,
1006-
minimumCompatibleVersion: config.minimumCompatibleVersion
1007-
})
1008-
})
10091010
}
10101011

10111012
exports = module.exports = realtime

test/realtime.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function makeMockSocket (headers, query) {
1515
query: Object.assign({}, query)
1616
},
1717
on: sinon.fake(),
18+
emit: sinon.fake(),
1819
broadCastChannelCache: {},
1920
broadcast: {
2021
to: (channel) => {
@@ -459,7 +460,10 @@ describe('realtime', function () {
459460
parseNoteTitle: (data) => (data)
460461
}
461462
})
462-
mock('../lib/config', {})
463+
mock('../lib/config', {
464+
fullversion: '1.5.0',
465+
minimumCompatibleVersion: '1.0.0'
466+
})
463467
realtime = require('../lib/realtime')
464468

465469
// get all socket event handler
@@ -609,5 +613,18 @@ describe('realtime', function () {
609613
})
610614
})
611615

616+
describe('version', function () {
617+
it('should emit server version ', () => {
618+
const versionFunc = eventFuncMap.get('version')
619+
versionFunc()
620+
assert(clientSocket.emit.called)
621+
assert(clientSocket.emit.lastCall.args[0], 'version')
622+
assert.deepStrictEqual(clientSocket.emit.lastCall.args[1], {
623+
version: '1.5.0',
624+
minimumCompatibleVersion: '1.0.0'
625+
})
626+
})
627+
})
628+
612629
})
613630
})

0 commit comments

Comments
 (0)