Skip to content

Commit 1ac6434

Browse files
committed
refactor(realtime): parseNoteIdFromSocket to async-await style function
Signed-off-by: BoHong Li <[email protected]>
1 parent 17e82c1 commit 1ac6434

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

lib/realtime.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,21 +358,20 @@ function extractNoteIdFromSocket (socket) {
358358
return false
359359
}
360360

361-
function parseNoteIdFromSocket (socket, callback) {
362-
var noteId = extractNoteIdFromSocket(socket)
361+
async function parseNoteIdFromSocketAsync (socket) {
362+
const noteId = extractNoteIdFromSocket(socket)
363363
if (!noteId) {
364-
return callback(null, null)
364+
return null
365365
}
366-
models.Note.parseNoteId(noteId, function (err, id) {
367-
if (err || !id) return callback(err, id)
368-
return callback(null, id)
369-
})
370-
}
371366

372-
function parseNoteIdFromSocketAsync (socket) {
373367
return new Promise((resolve, reject) => {
374-
parseNoteIdFromSocket(socket, (err, id) => {
375-
if (err) return reject(err)
368+
models.Note.parseNoteId(noteId, function (err, id) {
369+
if (err) {
370+
reject(err)
371+
}
372+
if (!id) {
373+
resolve(null)
374+
}
376375
resolve(id)
377376
})
378377
})
@@ -806,7 +805,6 @@ function terminate () {
806805

807806
exports = module.exports = realtime
808807
exports.extractNoteIdFromSocket = extractNoteIdFromSocket
809-
exports.parseNoteIdFromSocket = parseNoteIdFromSocket
810808
exports.updateNote = updateNote
811809
exports.failConnection = failConnection
812810
exports.updateUserData = updateUserData

test/realtime/connection.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('realtime#connection', function () {
4343
it('should fast return when server not start', () => {
4444
const mockSocket = makeMockSocket()
4545
realtime.maintenance = true
46-
const spy = sinon.spy(realtime, 'parseNoteIdFromSocket')
46+
const spy = sinon.spy(realtime, 'parseNoteIdFromSocketAsync')
4747
realtime.connection(mockSocket)
4848
assert(!spy.called)
4949
})

test/realtime/parseNoteIdFromSocket.test.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const sinon = require('sinon')
77

88
const { makeMockSocket, removeModuleFromRequireCache } = require('./utils')
99

10-
describe('realtime#parseNoteIdFromSocket', function () {
10+
describe('realtime#parseNoteIdFromSocketAsync', function () {
1111
let realtime
1212

1313
beforeEach(() => {
@@ -28,13 +28,15 @@ describe('realtime#parseNoteIdFromSocket', function () {
2828
mock.stopAll()
2929
})
3030

31-
it('should return null when socket not send noteId', function () {
31+
it('should return null when socket not send noteId', async function () {
3232
realtime = require('../../lib/realtime')
3333
const mockSocket = makeMockSocket()
34-
const fakeCallback = sinon.fake()
35-
realtime.parseNoteIdFromSocket(mockSocket, fakeCallback)
36-
assert(fakeCallback.called)
37-
assert.deepStrictEqual(fakeCallback.getCall(0).args, [null, null])
34+
try {
35+
const notes = await realtime.parseNoteIdFromSocketAsync(mockSocket)
36+
assert(notes === null)
37+
} catch (err) {
38+
assert.fail('should not occur any error')
39+
}
3840
})
3941

4042
describe('noteId exists', function () {
@@ -47,17 +49,20 @@ describe('realtime#parseNoteIdFromSocket', function () {
4749
}
4850
})
4951
})
50-
it('should return noteId when noteId exists', function () {
52+
it('should return noteId when noteId exists', async function () {
5153
realtime = require('../../lib/realtime')
5254
const noteId = '123456'
5355
const mockSocket = makeMockSocket(undefined, {
5456
noteId: noteId
5557
})
5658
realtime = require('../../lib/realtime')
57-
const fakeCallback = sinon.fake()
58-
realtime.parseNoteIdFromSocket(mockSocket, fakeCallback)
59-
assert(fakeCallback.called)
60-
assert.deepStrictEqual(fakeCallback.getCall(0).args, [null, noteId])
59+
let parsedNoteId
60+
try {
61+
parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
62+
} catch (err) {
63+
assert.fail(`should not occur any error ${err} `)
64+
}
65+
assert(parsedNoteId === noteId)
6166
})
6267
})
6368

@@ -71,17 +76,15 @@ describe('realtime#parseNoteIdFromSocket', function () {
7176
}
7277
})
7378
})
74-
it('should return null when noteId not exists', function () {
79+
it('should return null when noteId not exists', async function () {
7580
realtime = require('../../lib/realtime')
7681
const noteId = '123456'
7782
const mockSocket = makeMockSocket(undefined, {
7883
noteId: noteId
7984
})
8085
realtime = require('../../lib/realtime')
81-
const fakeCallback = sinon.fake()
82-
realtime.parseNoteIdFromSocket(mockSocket, fakeCallback)
83-
assert(fakeCallback.called)
84-
assert.deepStrictEqual(fakeCallback.getCall(0).args, [null, null])
86+
const parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
87+
assert(parsedNoteId === null)
8588
})
8689
})
8790

@@ -96,17 +99,18 @@ describe('realtime#parseNoteIdFromSocket', function () {
9699
}
97100
})
98101
})
99-
it('should return error when noteId parse error', function () {
102+
it('should return error when noteId parse error', async function () {
100103
realtime = require('../../lib/realtime')
101104
const noteId = '123456'
102105
const mockSocket = makeMockSocket(undefined, {
103106
noteId: noteId
104107
})
105108
realtime = require('../../lib/realtime')
106-
const fakeCallback = sinon.fake()
107-
realtime.parseNoteIdFromSocket(mockSocket, fakeCallback)
108-
assert(fakeCallback.called)
109-
assert.deepStrictEqual(fakeCallback.getCall(0).args, ['error', null])
109+
try {
110+
await realtime.parseNoteIdFromSocketAsync(mockSocket)
111+
} catch (err) {
112+
assert(err === 'error')
113+
}
110114
})
111115
})
112116
})

0 commit comments

Comments
 (0)