You've already forked node-redis
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:
175
lib/multi.js
175
lib/multi.js
@@ -4,10 +4,12 @@ const Queue = require('double-ended-queue')
|
||||
const utils = require('./utils')
|
||||
const Command = require('./command')
|
||||
|
||||
// TODO: Remove support for the non chaining way of using this
|
||||
// It's confusing and has no benefit
|
||||
function Multi (client, args) {
|
||||
this._client = client
|
||||
this.queue = new Queue()
|
||||
let command, tmpArgs
|
||||
var command, tmpArgs
|
||||
if (args) { // Either undefined or an array. Fail hard if it's not an array
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
command = args[i][0]
|
||||
@@ -25,163 +27,116 @@ function pipelineTransactionCommand (self, commandObj, index) {
|
||||
// Queueing is done first, then the commands are executed
|
||||
const tmp = commandObj.callback
|
||||
commandObj.callback = function (err, reply) {
|
||||
// Ignore the multi command. This is applied by nodeRedis and the user does not benefit by it
|
||||
if (err && index !== -1) {
|
||||
if (tmp) {
|
||||
tmp(err)
|
||||
}
|
||||
if (err) {
|
||||
tmp(err)
|
||||
err.position = index
|
||||
self.errors.push(err)
|
||||
return
|
||||
}
|
||||
// Keep track of who wants buffer responses:
|
||||
// By the time the callback is called the commandObj got the bufferArgs attribute attached
|
||||
self.wantsBuffers[index] = commandObj.bufferArgs
|
||||
commandObj.callback = tmp
|
||||
tmp(null, reply)
|
||||
}
|
||||
self._client.internalSendCommand(commandObj)
|
||||
return self._client.internalSendCommand(commandObj)
|
||||
}
|
||||
|
||||
Multi.prototype.execAtomic = function execAtomic (callback) {
|
||||
Multi.prototype.execAtomic = function execAtomic () {
|
||||
if (this.queue.length < 2) {
|
||||
return this.execBatch(callback)
|
||||
return this.execBatch()
|
||||
}
|
||||
return this.exec(callback)
|
||||
return this.exec()
|
||||
}
|
||||
|
||||
function multiCallback (self, err, replies) {
|
||||
let i = 0
|
||||
|
||||
if (err) {
|
||||
err.errors = self.errors
|
||||
if (self.callback) {
|
||||
self.callback(err)
|
||||
// Exclude connection errors so that those errors won't be emitted twice
|
||||
} else if (err.code !== 'CONNECTION_BROKEN') {
|
||||
self._client.emit('error', err)
|
||||
}
|
||||
return
|
||||
}
|
||||
function multiCallback (self, replies) {
|
||||
var i = 0
|
||||
|
||||
if (replies) {
|
||||
for (let commandObj = self.queue.shift(); commandObj !== undefined; commandObj = self.queue.shift()) {
|
||||
if (replies[i] instanceof Error) {
|
||||
if (replies[i].message) { // instanceof Error
|
||||
const match = replies[i].message.match(utils.errCode)
|
||||
// LUA script could return user errors that don't behave like all other errors!
|
||||
if (match) {
|
||||
replies[i].code = match[1]
|
||||
}
|
||||
replies[i].command = commandObj.command.toUpperCase()
|
||||
if (typeof commandObj.callback === 'function') {
|
||||
commandObj.callback(replies[i])
|
||||
}
|
||||
commandObj.callback(replies[i])
|
||||
} else {
|
||||
// If we asked for strings, even in detectBuffers mode, then return strings:
|
||||
replies[i] = self._client.handleReply(replies[i], commandObj.command, self.wantsBuffers[i])
|
||||
if (typeof commandObj.callback === 'function') {
|
||||
commandObj.callback(null, replies[i])
|
||||
}
|
||||
commandObj.callback(null, replies[i])
|
||||
}
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
if (self.callback) {
|
||||
self.callback(null, replies)
|
||||
}
|
||||
return replies
|
||||
}
|
||||
|
||||
Multi.prototype.execTransaction = function execTransaction (callback) {
|
||||
Multi.prototype.execTransaction = function execTransaction () {
|
||||
if (this.monitoring || this._client.monitoring) {
|
||||
const err = new RangeError(
|
||||
'Using transaction with a client that is in monitor mode does not work due to faulty return values of Redis.'
|
||||
)
|
||||
err.command = 'EXEC'
|
||||
err.code = 'EXECABORT'
|
||||
return utils.replyInOrder(this._client, callback, err)
|
||||
return new Promise((resolve, reject) => {
|
||||
utils.replyInOrder(this._client, (err, res) => {
|
||||
if (err) return reject(err)
|
||||
resolve(res)
|
||||
}, null, [])
|
||||
})
|
||||
}
|
||||
const self = this
|
||||
const len = self.queue.length
|
||||
self.errors = []
|
||||
self.callback = callback
|
||||
self._client.cork()
|
||||
self.wantsBuffers = new Array(len)
|
||||
pipelineTransactionCommand(self, new Command('multi', []), -1)
|
||||
const len = this.queue.length
|
||||
this.errors = []
|
||||
this._client.cork()
|
||||
this.wantsBuffers = new Array(len)
|
||||
// Silently ignore this error. We'll receive the error for the exec as well
|
||||
const promises = [this._client.internalSendCommand(new Command('multi', [])).catch(() => {})]
|
||||
// Drain queue, callback will catch 'QUEUED' or error
|
||||
for (let index = 0; index < len; index++) {
|
||||
// The commands may not be shifted off, since they are needed in the result handler
|
||||
pipelineTransactionCommand(self, self.queue.get(index), index)
|
||||
promises.push(pipelineTransactionCommand(this, this.queue.get(index), index).catch((e) => e))
|
||||
}
|
||||
|
||||
self._client.internalSendCommand(new Command('exec', [], (err, replies) => {
|
||||
multiCallback(self, err, replies)
|
||||
}))
|
||||
self._client.uncork()
|
||||
return
|
||||
}
|
||||
|
||||
function batchCallback (self, cb, i) {
|
||||
return function batchCallback (err, res) {
|
||||
if (err) {
|
||||
self.results[i] = err
|
||||
// Add the position to the error
|
||||
self.results[i].position = i
|
||||
} else {
|
||||
self.results[i] = res
|
||||
}
|
||||
cb(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
Multi.prototype.exec = Multi.prototype.execBatch = function execBatch (callback) {
|
||||
const main = this._client.internalSendCommand(new Command('exec', []))
|
||||
this._client.uncork()
|
||||
const self = this
|
||||
const len = self.queue.length
|
||||
let index = 0
|
||||
let commandObj
|
||||
if (len === 0) {
|
||||
utils.replyInOrder(self._client, callback, null, [])
|
||||
return
|
||||
return Promise.all(promises).then(() => main.then((replies) => multiCallback(self, replies)).catch((err) => {
|
||||
err.errors = self.errors
|
||||
return Promise.reject(err)
|
||||
}))
|
||||
}
|
||||
|
||||
Multi.prototype.exec = Multi.prototype.execBatch = function execBatch () {
|
||||
if (this.queue.length === 0) {
|
||||
// TODO: return an error if not "ready"
|
||||
return new Promise((resolve) => {
|
||||
utils.replyInOrder(this._client, (e, res) => {
|
||||
resolve(res)
|
||||
}, null, [])
|
||||
})
|
||||
}
|
||||
self._client.cork()
|
||||
if (!callback) {
|
||||
for (commandObj = self.queue.shift(); commandObj !== undefined; commandObj = self.queue.shift()) {
|
||||
self._client.internalSendCommand(commandObj)
|
||||
}
|
||||
self._client.uncork()
|
||||
return
|
||||
var error = false
|
||||
this._client.cork()
|
||||
const promises = []
|
||||
while (this.queue.length) {
|
||||
const commandObj = this.queue.shift()
|
||||
promises.push(this._client.internalSendCommand(commandObj).catch((e) => {
|
||||
error = true
|
||||
return e
|
||||
}))
|
||||
}
|
||||
const callbackWithoutOwnCb = function (err, res) {
|
||||
if (err) {
|
||||
self.results.push(err)
|
||||
// Add the position to the error
|
||||
const i = self.results.length - 1
|
||||
self.results[i].position = i
|
||||
} else {
|
||||
self.results.push(res)
|
||||
this._client.uncork()
|
||||
return Promise.all(promises).then((res) => {
|
||||
if (error) {
|
||||
const err = new Error('bla failed')
|
||||
err.code = 'foo'
|
||||
err.replies = res
|
||||
return Promise.reject(err)
|
||||
}
|
||||
// Do not emit an error here. Otherwise each error would result in one emit.
|
||||
// The errors will be returned in the result anyway
|
||||
}
|
||||
const lastCallback = function (cb) {
|
||||
return function (err, res) {
|
||||
cb(err, res)
|
||||
callback(null, self.results)
|
||||
}
|
||||
}
|
||||
self.results = []
|
||||
for (commandObj = self.queue.shift(); commandObj !== undefined; commandObj = self.queue.shift()) {
|
||||
if (typeof commandObj.callback === 'function') {
|
||||
commandObj.callback = batchCallback(self, commandObj.callback, index)
|
||||
} else {
|
||||
commandObj.callback = callbackWithoutOwnCb
|
||||
}
|
||||
if (typeof callback === 'function' && index === len - 1) {
|
||||
commandObj.callback = lastCallback(commandObj.callback)
|
||||
}
|
||||
this._client.internalSendCommand(commandObj)
|
||||
index++
|
||||
}
|
||||
self._client.uncork()
|
||||
return
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = Multi
|
||||
|
Reference in New Issue
Block a user