refactor(realtime): parseNoteIdFromSocket to async-await style function

Signed-off-by: BoHong Li <a60814billy@gmail.com>
This commit is contained in:
BoHong Li 2019-05-23 18:40:07 +08:00
parent 17e82c11c9
commit 1ac6434ab7
No known key found for this signature in database
GPG Key ID: 9696D5590D58290F
3 changed files with 36 additions and 34 deletions

View File

@ -358,21 +358,20 @@ function extractNoteIdFromSocket (socket) {
return false return false
} }
function parseNoteIdFromSocket (socket, callback) { async function parseNoteIdFromSocketAsync (socket) {
var noteId = extractNoteIdFromSocket(socket) const noteId = extractNoteIdFromSocket(socket)
if (!noteId) { if (!noteId) {
return callback(null, null) return null
} }
models.Note.parseNoteId(noteId, function (err, id) {
if (err || !id) return callback(err, id)
return callback(null, id)
})
}
function parseNoteIdFromSocketAsync (socket) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
parseNoteIdFromSocket(socket, (err, id) => { models.Note.parseNoteId(noteId, function (err, id) {
if (err) return reject(err) if (err) {
reject(err)
}
if (!id) {
resolve(null)
}
resolve(id) resolve(id)
}) })
}) })
@ -806,7 +805,6 @@ function terminate () {
exports = module.exports = realtime exports = module.exports = realtime
exports.extractNoteIdFromSocket = extractNoteIdFromSocket exports.extractNoteIdFromSocket = extractNoteIdFromSocket
exports.parseNoteIdFromSocket = parseNoteIdFromSocket
exports.updateNote = updateNote exports.updateNote = updateNote
exports.failConnection = failConnection exports.failConnection = failConnection
exports.updateUserData = updateUserData exports.updateUserData = updateUserData

View File

@ -43,7 +43,7 @@ describe('realtime#connection', function () {
it('should fast return when server not start', () => { it('should fast return when server not start', () => {
const mockSocket = makeMockSocket() const mockSocket = makeMockSocket()
realtime.maintenance = true realtime.maintenance = true
const spy = sinon.spy(realtime, 'parseNoteIdFromSocket') const spy = sinon.spy(realtime, 'parseNoteIdFromSocketAsync')
realtime.connection(mockSocket) realtime.connection(mockSocket)
assert(!spy.called) assert(!spy.called)
}) })

View File

@ -7,7 +7,7 @@ const sinon = require('sinon')
const { makeMockSocket, removeModuleFromRequireCache } = require('./utils') const { makeMockSocket, removeModuleFromRequireCache } = require('./utils')
describe('realtime#parseNoteIdFromSocket', function () { describe('realtime#parseNoteIdFromSocketAsync', function () {
let realtime let realtime
beforeEach(() => { beforeEach(() => {
@ -28,13 +28,15 @@ describe('realtime#parseNoteIdFromSocket', function () {
mock.stopAll() mock.stopAll()
}) })
it('should return null when socket not send noteId', function () { it('should return null when socket not send noteId', async function () {
realtime = require('../../lib/realtime') realtime = require('../../lib/realtime')
const mockSocket = makeMockSocket() const mockSocket = makeMockSocket()
const fakeCallback = sinon.fake() try {
realtime.parseNoteIdFromSocket(mockSocket, fakeCallback) const notes = await realtime.parseNoteIdFromSocketAsync(mockSocket)
assert(fakeCallback.called) assert(notes === null)
assert.deepStrictEqual(fakeCallback.getCall(0).args, [null, null]) } catch (err) {
assert.fail('should not occur any error')
}
}) })
describe('noteId exists', function () { describe('noteId exists', function () {
@ -47,17 +49,20 @@ describe('realtime#parseNoteIdFromSocket', function () {
} }
}) })
}) })
it('should return noteId when noteId exists', function () { it('should return noteId when noteId exists', async function () {
realtime = require('../../lib/realtime') realtime = require('../../lib/realtime')
const noteId = '123456' const noteId = '123456'
const mockSocket = makeMockSocket(undefined, { const mockSocket = makeMockSocket(undefined, {
noteId: noteId noteId: noteId
}) })
realtime = require('../../lib/realtime') realtime = require('../../lib/realtime')
const fakeCallback = sinon.fake() let parsedNoteId
realtime.parseNoteIdFromSocket(mockSocket, fakeCallback) try {
assert(fakeCallback.called) parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
assert.deepStrictEqual(fakeCallback.getCall(0).args, [null, noteId]) } catch (err) {
assert.fail(`should not occur any error ${err} `)
}
assert(parsedNoteId === noteId)
}) })
}) })
@ -71,17 +76,15 @@ describe('realtime#parseNoteIdFromSocket', function () {
} }
}) })
}) })
it('should return null when noteId not exists', function () { it('should return null when noteId not exists', async function () {
realtime = require('../../lib/realtime') realtime = require('../../lib/realtime')
const noteId = '123456' const noteId = '123456'
const mockSocket = makeMockSocket(undefined, { const mockSocket = makeMockSocket(undefined, {
noteId: noteId noteId: noteId
}) })
realtime = require('../../lib/realtime') realtime = require('../../lib/realtime')
const fakeCallback = sinon.fake() const parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
realtime.parseNoteIdFromSocket(mockSocket, fakeCallback) assert(parsedNoteId === null)
assert(fakeCallback.called)
assert.deepStrictEqual(fakeCallback.getCall(0).args, [null, null])
}) })
}) })
@ -96,17 +99,18 @@ describe('realtime#parseNoteIdFromSocket', function () {
} }
}) })
}) })
it('should return error when noteId parse error', function () { it('should return error when noteId parse error', async function () {
realtime = require('../../lib/realtime') realtime = require('../../lib/realtime')
const noteId = '123456' const noteId = '123456'
const mockSocket = makeMockSocket(undefined, { const mockSocket = makeMockSocket(undefined, {
noteId: noteId noteId: noteId
}) })
realtime = require('../../lib/realtime') realtime = require('../../lib/realtime')
const fakeCallback = sinon.fake() try {
realtime.parseNoteIdFromSocket(mockSocket, fakeCallback) await realtime.parseNoteIdFromSocketAsync(mockSocket)
assert(fakeCallback.called) } catch (err) {
assert.deepStrictEqual(fakeCallback.getCall(0).args, ['error', null]) assert(err === 'error')
}
}) })
}) })
}) })