@nodeart/async-buffer
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -9,5 +9,6 @@ /** | ||
function AsyncBuffer(AsyncBufferLimit = 50, criticalLimit = 1000) { | ||
function AsyncBuffer(AsyncBufferLimit = 50, autoStart = true, criticalLimit = 1000) { | ||
this.limit = AsyncBufferLimit; | ||
this.criticalLimit = criticalLimit; | ||
this.autostart = autoStart; | ||
this.stack = []; | ||
@@ -36,3 +37,5 @@ this.results = []; | ||
if (this.stack.length >= this.limit) { | ||
setTimeout(() => this.drainBuffer(), 0); | ||
this.autostart ? | ||
setTimeout(() => this.drainBuffer(), 0) : | ||
this.emit('stack_filled'); | ||
} else if (this.stack.length >= this.criticalLimit) { | ||
@@ -53,3 +56,3 @@ setImmediate(() => this.drainBuffer()); | ||
let task = this.stack.pop(), | ||
res = this.results; | ||
res = this.results; | ||
if (typeof task === 'function') { | ||
@@ -56,0 +59,0 @@ task(this.callback.bind(this), res[res.length - 1]); |
{ | ||
"name": "@nodeart/async-buffer", | ||
"version": "1.0.4", | ||
"description": "AsyncBuffer is used for async tasks accumulation and calling them sequentially after buffer limit will be exceeded. AsyncBuffer starts its operation automatically just task limit is reached.", | ||
"version": "1.0.5", | ||
"description": "AsyncBuffer is used for async tasks accumulation and calling them sequentially after buffer limit will be exceeded. By default AsyncBuffer starts its operation automatically just after tasks limit is reached.", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
**AsyncBuffer** | ||
AsyncBuffer is used for async tasks accumulation and calling them sequentially after buffer limit will be exceeded. | ||
AsyncBuffer starts its operation automatically just task limit is reached. | ||
By default AsyncBuffer starts its operation automatically just after tasks limit is reached. | ||
@@ -31,2 +31,19 @@ For example one can use this package to work with database. | ||
You can switch off auto execution by setting second parameter in constructor to `false` and start task execution manually. | ||
```javascript | ||
let buffer = new AsyncBuffer(1, false); | ||
buffer.on('stack_filled', function () { | ||
console.log('Stack is filled'); | ||
buffer.drainBuffer(); | ||
}); | ||
buffer.push(function(cb) { | ||
setTimeout(function () { | ||
console.log(`I was called`); | ||
cb('result'); | ||
}, 1000) | ||
}); | ||
``` | ||
It is necessary to mention that tasks execution is started asynchronously using `setTimeout(fn, 0)` to be pushed in the end of queue. | ||
@@ -33,0 +50,0 @@ |
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
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
6118
65
93