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

chore: add print helper again and refactor some code

Expose the RedisClient directly instead of only being a property
This commit is contained in:
Ruben Bridgewater
2017-05-29 18:23:24 +02:00
parent 4e593587cb
commit 0276e15f04
14 changed files with 169 additions and 104 deletions

View File

@@ -46,25 +46,29 @@ function replyToStrings (reply) {
* @description 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)
* and array will be passed by reference (e.g. Buffers, Maps, Functions).
*
* @param {any} obj
* @returns any
*/
function clone (obj) {
var copy
if (Array.isArray(obj)) {
copy = new Array(obj.length)
const copy = new Array(obj.length)
for (var i = 0; i < obj.length; i++) {
copy[i] = clone(obj[i])
}
return copy
}
if (Object.prototype.toString.call(obj) === '[object Object]') {
copy = {}
const elements = Object.keys(obj)
for (var elem = elements.pop(); elem !== undefined; elem = elements.pop()) {
copy[elem] = clone(obj[elem])
const copy = {}
const keys = Object.keys(obj)
while (keys.length) {
const key = keys.pop()
if (key === 'stream') {
copy[key] = obj[key]
} else {
copy[key] = clone(obj[key])
}
}
return copy
}
@@ -96,12 +100,10 @@ function convenienceClone (obj) {
* @param {Denque} queue
*/
function replyInOrder (client, callback, err, res, queue) {
var commandObj
if (queue) {
commandObj = queue.peekBack()
} else {
commandObj = client.offlineQueue.peekBack() || client.commandQueue.peekBack()
}
const commandObj = queue
? queue.peekBack()
: (client.offlineQueue.peekBack() || client.commandQueue.peekBack())
if (!commandObj) {
process.nextTick(callback, err, res)
} else {
@@ -149,6 +151,21 @@ function handleReply (client, reply, command) {
return reply
}
/**
* @description Callback result print helper
*
* @param {Error|null} err
* @param {any} reply
*/
function print (err, reply) {
if (err) {
// A error always begins with Error:
console.error(err.toString())
} else {
console.log('Reply: ' + reply)
}
}
/**
* @description Set default reconnect variables
*
@@ -171,5 +188,6 @@ module.exports = {
replyInOrder,
warn,
handleReply,
setReconnectDefaults
setReconnectDefaults,
print
}