Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add pipelining with method chaining (#312 @funnisimo )
Added a 'pipeline' method which allows chaining of methods like ioredis. Updated discard and multi to work with the pipeline.
- Loading branch information
Showing
6 changed files
with
101 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export function discard() { | ||
this.batch.length = 0; | ||
|
||
if (!this.batch) { | ||
throw new Error('ERR DISCARD without MULTI'); | ||
} | ||
this.batch = undefined; | ||
return 'OK'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Promise from 'bluebird'; | ||
|
||
import * as commands from './commands'; | ||
|
||
function createCommand(pipeline, emulate) { | ||
return (...args) => { | ||
const lastArgIndex = args.length - 1; | ||
let callback = args[lastArgIndex]; | ||
if (typeof callback !== 'function') { | ||
callback = undefined; | ||
} else { | ||
args.length = lastArgIndex; // eslint-disable-line no-param-reassign | ||
} | ||
|
||
// transform non-buffer arguments to strings to simulate real ioredis behavior | ||
const stringArgs = args.map(arg => // eslint-disable-line no-confusing-arrow | ||
arg instanceof Buffer ? arg : arg.toString() | ||
); | ||
|
||
pipeline.batch.push(() => emulate(...stringArgs)); | ||
return pipeline; | ||
}; | ||
} | ||
|
||
class Pipeline { | ||
constructor(redis) { | ||
this.batch = []; | ||
|
||
Object.keys(commands).forEach((command) => { | ||
this[command] = createCommand(this, commands[command].bind(redis)); | ||
}); | ||
} | ||
exec(callback) { | ||
const batch = this.batch; | ||
this.batch = []; | ||
return Promise.resolve(batch.map(cmd => [null, cmd()])).asCallback(callback); | ||
} | ||
} | ||
|
||
export default Pipeline; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters