1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

chore: refactor codebase to promises

This commit is contained in:
Ruben Bridgewater
2017-05-19 06:14:29 +02:00
parent b2613b2270
commit 6be5575c5b
85 changed files with 2081 additions and 3690 deletions

View File

@@ -15,10 +15,13 @@ function replyToObject (reply) {
}
function replyToStrings (reply) {
if (reply instanceof Buffer) {
if (reply === null) {
return null
}
if (typeof reply.inspect === 'function') { // instanceof Buffer
return reply.toString()
}
if (reply instanceof Array) {
if (typeof reply.map === 'function') { // instanceof Array
const res = new Array(reply.length)
for (let i = 0; i < reply.length; i++) {
// Recursively call the function as slowlog returns deep nested replies
@@ -33,7 +36,7 @@ function replyToStrings (reply) {
// Deep clone arbitrary objects with arrays. Can't handle cyclic structures (results in a range error)
// Any attribute with a non primitive value besides object and array will be passed by reference (e.g. Buffers, Maps, Functions)
function clone (obj) {
let copy
var copy
if (Array.isArray(obj)) {
copy = new Array(obj.length)
for (let i = 0; i < obj.length; i++) {
@@ -56,18 +59,10 @@ function convenienceClone (obj) {
return clone(obj) || {}
}
function callbackOrEmit (self, callback, err, res) {
if (callback) {
callback(err, res)
} else if (err) {
self.emit('error', err)
}
}
function replyInOrder (self, callback, err, res, queue) {
// If the queue is explicitly passed, use that, otherwise fall back to the offline queue first,
// as there might be commands in both queues at the same time
let commandObj
var commandObj
if (queue) {
commandObj = queue.peekBack()
} else {
@@ -75,21 +70,15 @@ function replyInOrder (self, callback, err, res, queue) {
}
if (!commandObj) {
process.nextTick(() => {
callbackOrEmit(self, callback, err, res)
callback(err, res)
})
} else {
// TODO: Change this to chain promises instead
const tmp = commandObj.callback
commandObj.callback = tmp
? function (e, r) {
tmp(e, r)
callbackOrEmit(self, callback, err, res)
}
: function (e) {
if (e) {
self.emit('error', e)
}
callbackOrEmit(self, callback, err, res)
}
commandObj.callback = function (e, r) {
tmp(e, r)
callback(err, res)
}
}
}
@@ -99,6 +88,5 @@ module.exports = {
errCode: /^([A-Z]+)\s+(.+)$/,
monitorRegex: /^[0-9]{10,11}\.[0-9]+ \[[0-9]+ .+]( ".+?")+$/,
clone: convenienceClone,
callbackOrEmit,
replyInOrder
}