-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New backoff and batch implementations (#12)
* New backoff and batch implementations * Lint picking * Add spy * Downgrade joi to be a peer dependency instead * Limit max backoff tries to 10 * Share promises with dupe args to only batch uniqs * Test that validate is safe if joi is missing
- Loading branch information
1 parent
7b124ab
commit aac9555
Showing
10 changed files
with
358 additions
and
36 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
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
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,62 @@ | ||
const call = require('ramda/src/call') | ||
const curry = require('ramda/src/curry') | ||
const juxt = require('ramda/src/juxt') | ||
const max = require('ramda/src/max') | ||
const zipWith = require('ramda/src/zipWith') | ||
|
||
// batch :: { k: v } -> ([a] -> Promise [b]) -> a -> Promise b | ||
const batch = (opts={}, f) => { | ||
const { | ||
limit = Infinity, | ||
wait = 32 | ||
} = opts | ||
|
||
let args = [] | ||
let last = 0 | ||
let rejects = [] | ||
let resolves = [] | ||
let timeout = 0 | ||
let uniq = new Map() | ||
|
||
const batched = arg => { | ||
if (uniq.has(arg)) { | ||
return uniq.get(arg) | ||
} else { | ||
const promise = new Promise((res, rej) => { | ||
args.push(arg) | ||
rejects.push(rej) | ||
resolves.push(res) | ||
}) | ||
|
||
uniq.set(arg, promise) | ||
|
||
if (args.length >= limit) run() | ||
|
||
else if (!timeout) { | ||
const delta = new Date() - last | ||
timeout = setTimeout(run, max(0, wait - delta)) | ||
} | ||
|
||
return promise | ||
} | ||
} | ||
|
||
const run = () => { | ||
Promise.resolve(args) | ||
.then(f) | ||
.then(zipWith(call, resolves)) | ||
.catch(juxt(rejects)) | ||
|
||
clearTimeout(timeout) | ||
args = [] | ||
last = +new Date() | ||
rejects = [] | ||
resolves = [] | ||
timeout = 0 | ||
uniq.clear() | ||
} | ||
|
||
return batched | ||
} | ||
|
||
module.exports = curry(batch) |
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,14 +1,21 @@ | ||
const curryN = require('ramda/src/curryN') | ||
const Joi = require('joi') | ||
|
||
const promisify = require('./promisify') | ||
let validate | ||
|
||
const defaults = { abortEarly: false } | ||
try { | ||
const Joi = require('joi') | ||
const promisify = require('./promisify') | ||
const _validate = promisify(Joi.validate, Joi) | ||
const defaults = { abortEarly: false } | ||
|
||
const _validate = promisify(Joi.validate, Joi) | ||
validate = (schema, x, opts=defaults) => | ||
_validate(x, schema, opts) | ||
} | ||
|
||
// validate :: Schema -> a -> Promise a | ||
const validate = (schema, x, opts=defaults) => | ||
_validate(x, schema, opts) | ||
catch (e) { | ||
validate = (_, x) => | ||
Promise.resolve(x) | ||
} | ||
|
||
// validate :: Schema -> a -> Promise a | ||
module.exports = curryN(2, validate) |
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
Oops, something went wrong.