ioredis-auto-pipeline
Advanced tools
Comparing version 0.1.0 to 1.0.0
67
index.js
@@ -6,2 +6,4 @@ 'use strict' | ||
const notAllowedCommands = ['subscribe', 'psubscribe'] | ||
function auto (client) { | ||
@@ -11,10 +13,34 @@ let pipeline | ||
function build () { | ||
if (pipeline === undefined) { | ||
pipeline = new Pipeline(client) | ||
const obj = {} | ||
for (const cmd of client.getBuiltinCommands()) { | ||
if (!notAllowedCommands.includes(cmd)) { | ||
obj[cmd] = buildWrap(cmd) | ||
} | ||
return pipeline | ||
} | ||
Object.defineProperty(obj, 'queued', { | ||
get () { | ||
if (pipeline === undefined) { | ||
return 0 | ||
} | ||
return pipeline.queued | ||
} | ||
}) | ||
Object.defineProperty(obj, kPipeline, { | ||
get () { | ||
if (pipeline === undefined) { | ||
pipeline = client.pipeline() | ||
pipeline[kExec] = false | ||
pipeline.queued = 0 | ||
} | ||
return pipeline | ||
} | ||
}) | ||
return obj | ||
function exec () { | ||
@@ -26,3 +52,3 @@ if (running) { | ||
running = true | ||
pipeline[kPipeline].exec(function () { | ||
pipeline.exec(function () { | ||
running = false | ||
@@ -37,19 +63,15 @@ | ||
function Pipeline () { | ||
this[kPipeline] = client.pipeline() | ||
this[kExec] = false | ||
this.queued = 0 | ||
} | ||
function buildWrap (key) { | ||
return function (...args) { | ||
const pipeline = this[kPipeline] | ||
function addMethod (key) { | ||
Pipeline.prototype[key] = function wrap (...args) { | ||
if (!this[kExec]) { | ||
this[kExec] = true | ||
if (!pipeline[kExec]) { | ||
pipeline[kExec] = true | ||
process.nextTick(exec) | ||
} | ||
this.queued++ | ||
pipeline.queued++ | ||
return new Promise((resolve, reject) => { | ||
this[kPipeline][key](...args, function (err, value) { | ||
return new Promise(function (resolve, reject) { | ||
pipeline[key](...args, function (err, value) { | ||
if (err) { | ||
@@ -64,11 +86,2 @@ reject(err) | ||
} | ||
const notAllowedCommands = ['subscribe', 'psubscribe'] | ||
for (const cmd of client.getBuiltinCommands()) { | ||
if (!notAllowedCommands.includes(cmd)) { | ||
addMethod(cmd) | ||
} | ||
} | ||
return build | ||
} | ||
@@ -75,0 +88,0 @@ |
{ | ||
"name": "ioredis-auto-pipeline", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "automatic redis pipeline", | ||
@@ -30,3 +30,6 @@ "main": "index.js", | ||
"tap": "^14.10.2" | ||
}, | ||
"dependencies": { | ||
"to-fast-properties": "^3.0.0" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # ioredis-auto-pipeline | ||
All builtin commands | ||
All builtin commands are supported, minus subscribe and psubscribe. | ||
@@ -23,6 +23,5 @@ ## Install | ||
async function run () { | ||
const redis = new Redis() | ||
const pipeline = auto(redis) | ||
const redis = auto(new Redis()) | ||
console.log(pipeline().queued) // number of ops in the queue | ||
console.log(redis.queued) // number of ops in the queue | ||
@@ -33,7 +32,7 @@ // In any part of your code, call pipeline() | ||
const results = await Promise.all([ | ||
pipeline().get('foo'), | ||
pipeline().get('foo'), | ||
pipeline().get('foo'), | ||
pipeline().get('foo'), | ||
pipeline().get('foo') | ||
redis.get('foo'), | ||
redis.get('foo'), | ||
redis.get('foo'), | ||
redis.get('foo'), | ||
redis.get('foo') | ||
]) | ||
@@ -40,0 +39,0 @@ |
46
test.js
@@ -15,4 +15,4 @@ 'use strict' | ||
const pipeline = auto(redis) | ||
await pipeline().set('foo', 'bar') | ||
is(await pipeline().get('foo'), 'bar') | ||
await pipeline.set('foo', 'bar') | ||
is(await pipeline.get('foo'), 'bar') | ||
}) | ||
@@ -22,10 +22,10 @@ | ||
const pipeline = auto(redis) | ||
await pipeline().set('foo', 'bar') | ||
await pipeline.set('foo', 'bar') | ||
deepEqual(await Promise.all([ | ||
pipeline().get('foo'), | ||
pipeline().get('foo'), | ||
pipeline().get('foo'), | ||
pipeline().get('foo'), | ||
pipeline().get('foo') | ||
pipeline.get('foo'), | ||
pipeline.get('foo'), | ||
pipeline.get('foo'), | ||
pipeline.get('foo'), | ||
pipeline.get('foo') | ||
]), [ | ||
@@ -42,5 +42,5 @@ 'bar', | ||
const pipeline = auto(redis) | ||
await pipeline().set('foo', 'bar') | ||
await pipeline.set('foo', 'bar') | ||
pipeline()[auto.kPipeline].get = (key, cb) => { | ||
pipeline[auto.kPipeline].get = (key, cb) => { | ||
is(key, 'foo') | ||
@@ -50,7 +50,7 @@ process.nextTick(cb, new Error('kaboom')) | ||
pipeline()[auto.kPipeline].exec = (cb) => { | ||
pipeline[auto.kPipeline].exec = (cb) => { | ||
process.nextTick(cb) | ||
} | ||
await rejects(pipeline().get('foo')) | ||
await rejects(pipeline.get('foo')) | ||
}) | ||
@@ -60,19 +60,17 @@ | ||
const pipeline = auto(redis) | ||
const first = pipeline() | ||
is(first.queued, 0) | ||
const promise1 = first.set('foo', 'bar') | ||
is(first.queued, 1) | ||
is(pipeline.queued, 0) | ||
const promise1 = pipeline.set('foo', 'bar') | ||
is(pipeline.queued, 1) | ||
await promise1 | ||
const second = pipeline() | ||
is(second.queued, 0) | ||
is(pipeline.queued, 0) | ||
const promise2 = Promise.all([ | ||
second.get('foo'), | ||
second.get('foo'), | ||
second.get('foo'), | ||
second.get('foo'), | ||
second.get('foo') | ||
pipeline.get('foo'), | ||
pipeline.get('foo'), | ||
pipeline.get('foo'), | ||
pipeline.get('foo'), | ||
pipeline.get('foo') | ||
]) | ||
is(second.queued, 5) | ||
is(pipeline.queued, 5) | ||
await promise2 | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
7486
1
7
170
47
1
+ Addedto-fast-properties@^3.0.0
+ Addedto-fast-properties@3.0.1(transitive)