Socket
Socket
Sign inDemoInstall

datawriter

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.0.1

readme.md

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"
}
}

@@ -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);
}
})();
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc