Comparing version 1.0.0 to 1.0.1
@@ -6,3 +6,3 @@ 'use strict'; | ||
const countBased = new Bunchie({minBunchedCount: 3}); | ||
const countBased = new Bunchie({minBunchedCount: 3, type: 'set'}); | ||
@@ -24,5 +24,15 @@ | ||
const timeBased = new Bunchie({minWaitTime: 1000}); | ||
const timeBased = new Bunchie({minWaitTime: 1000, type: 'set'}); | ||
timeBased.setBunchHandler(x => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
x.example = 'I handled this.'; | ||
resolve(x); | ||
}, 1500); | ||
}); | ||
}); | ||
timeBased.bunch('1') | ||
@@ -46,1 +56,6 @@ .then(x => console.log('1:', x)); | ||
}, 1200); | ||
timeBased.onFlush(bunch => { | ||
console.log('Flushed', bunch); | ||
}); |
27
index.js
@@ -36,4 +36,8 @@ 'use strict'; | ||
p(this).callbacks = []; | ||
p(this).callbacks = { | ||
onFlush: [] | ||
}; | ||
p(this).bunchHandler; | ||
if (config.Promise) Promise = config.Promise; | ||
@@ -73,3 +77,3 @@ | ||
onFlush(callback) { | ||
p(this).callbacks.push(callback); | ||
p(this).callbacks.onFlush.push(callback); | ||
} | ||
@@ -81,2 +85,11 @@ | ||
*/ | ||
setBunchHandler(callback) { | ||
p(this).bunchHandler = (result) => Promise.resolve(callback(result)) | ||
.then(handled => handled === result ? result : Object.assign(result, {handled})); | ||
} | ||
/** | ||
* | ||
*/ | ||
bunch(item) { | ||
@@ -100,3 +113,3 @@ const promise = p(this).promise; | ||
return promise | ||
.then(bunch => ({item, bunch})); | ||
.then((result) => (Object.assign({}, result, {item}))); | ||
} | ||
@@ -111,3 +124,2 @@ } | ||
p(bunchie).state.bunchedCount = 0; | ||
p(bunchie).state.timeoutCompleted = false; | ||
@@ -117,6 +129,6 @@ const bunch = p(bunchie).queue || p(bunchie).map || p(bunchie).set; | ||
p(bunchie).deferred.resolve(bunch); | ||
p(bunchie).deferred.resolve({bunch}); | ||
resetPromise(bunchie); | ||
p(bunchie).callbacks.forEach(cb => cb(bunch)); | ||
p(bunchie).callbacks.onFlush.forEach(cb => cb(bunch)); | ||
@@ -156,3 +168,4 @@ return bunch; | ||
p(bunchie).deferred = ({resolve, reject}); | ||
}); | ||
}) | ||
.then(({bunch}) => p(bunchie).bunchHandler ? p(bunchie).bunchHandler({bunch}) : {bunch}); | ||
} | ||
@@ -159,0 +172,0 @@ |
{ | ||
"name": "bunchie", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Bunch up calls by count or time before resolving. Useful for clustering together requests to a remote service, or anything where gating together would be necessary.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
6796
5
167
1
46