datawriter
Advanced tools
Comparing version 1.0.0 to 1.0.1
21
index.js
class DataWriter { | ||
constructor(maxBufferSize, maxDelay, bulkWriteFunction, onError) { | ||
constructor(maxBufferSize, maxDelay, batchWriteFunction, onError) { | ||
this.maxBufferSize = maxBufferSize; | ||
this.maxDelay = maxDelay; | ||
this.bulkWriteFunction = bulkWriteFunction; | ||
this.batchWriteFunction = batchWriteFunction; | ||
this.buffer = []; | ||
@@ -19,11 +19,16 @@ this.timeout = null; | ||
checkWrite() { | ||
if (this.timeout) clearTimeout(this.timeout); | ||
if (!this.timeout) { | ||
this.timeout = setTimeout(this.batchWrite.bind(this), this.maxDelay); | ||
} | ||
if (this.buffer.length >= this.maxBufferSize) { | ||
this.bulkWrite(); | ||
} else { | ||
this.timeout = setTimeout(this.bulkWrite.bind(this), this.maxDelay); | ||
this.batchWrite(); | ||
} | ||
} | ||
async bulkWrite() { | ||
async batchWrite() { | ||
if (this.timeout) { | ||
clearTimeout(this.timeout); | ||
this.timeout = null; | ||
} | ||
if (this.buffer.length === 0) return; | ||
@@ -33,3 +38,3 @@ let arr = [...this.buffer]; | ||
try { | ||
await this.bulkWriteFunction(arr); | ||
await this.batchWriteFunction(arr); | ||
} catch (err) { | ||
@@ -36,0 +41,0 @@ let error = new Error(`DataWriter write error: ${err ? err.message : 'unknown error'}`); |
{ | ||
"name": "datawriter", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "cache and write", | ||
@@ -23,2 +23,2 @@ "main": "index.js", | ||
"homepage": "https://github.com/longbill/datawriter#readme" | ||
} | ||
} |
21
test.js
@@ -9,14 +9,19 @@ const DataWriter = require('./'); | ||
let writer = new DataWriter(100, 1000, async function(arr) { | ||
console.log('writing', arr.length, 'rows to database'); | ||
await wait(100); | ||
if (Math.random() < 0.5) throw new Error('write failed'); | ||
let writer = new DataWriter(3, 1000, async function(arr) { | ||
console.log('writing', arr, 'rows to database'); | ||
// await wait(100); | ||
if (Math.random() < 0.5) throw new Error('batch write failed'); | ||
console.log(`write success`); | ||
}, function(err) { | ||
console.log(err.message); | ||
console.log('onError', err); | ||
}); | ||
setInterval(() => { | ||
writer.write(Date.now()); | ||
}, 20); | ||
(async () => { | ||
for (let i = 0; i < 1; i++ ) { | ||
writer.write(i); | ||
await wait((i % 10) * 200); | ||
} | ||
})(); |
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
3609
4
62
1
62