Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-timed-cargo

Package Overview
Dependencies
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-timed-cargo

An lib to periodically flush a collection with a limited size based on async.cargo

  • 0.0.3
  • latest
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

Async Timed Cargo

Build Status

NPM

async.cargo based task execution.

Differences from async.cargo

Async.cargo:

  • Payload number is used as a limit to the message, but most times the callback is called with a fewer number of items
  • It tries to process tasks as soon as possible when they are pushed, which is bad if used to control batch messages processing, as it will result in more calls with fewer items

async-timed-cargo

  • Adds a timer concept to the task execution
  • Payload tries to be respected, if number of messages received is less than payload it does not do anything until timer is triggered or more messages are received, whatever comes first
  • It is better for message batch process as it waits until the queue is full

Install

add async-timed-cargo to you package.json

Example


var asyncTimedCargo = require('async-timed-cargo');

var cargo = asyncTimedCargo(function(tasks, callback) {

  // the tasks array will be [1, 2, 3]
  // this will be called after 1000ms, as number of items (3) is smaller than payload (10)
  callback('err', 'arg');

}, 10, 1000);

asyncTimedCargo.push(1);
asyncTimedCargo.push(2);
asyncTimedCargo.push(3, function(err, arg) {
  // err will be 'err'
  // arg will be 'arg'
});


Also, check the example app for a working example on how to use the middleware

Usage

async-timed-cargo(worker, [[payload], timeout])

Arguments

  • worker(tasks, callback) - An asynchronous function for processing an array of queued tasks, which must call its callback(err, arg) argument when finished, with an optional err and arg argument.
  • payload - An optional integer for determining how many tasks should be processed per round; if omitted, the default is unlimited and only timer will be considered
  • timeout - An optional integer for determining the interval (in ms) to be used to flush data each round; if omitted, the default is 1000ms

Cargo objects

The cargo object returned by this function has the following properties and methods:

  • length() - A function returning the number of items waiting to be processed.
  • push(task, [callback]) - Adds task to the queue. The callback is called once the worker has finished processing the task. Instead of a single task, an array of tasks can be submitted. The respective callback is used for every task in the list.

Test

$ npm install
$ npm test

Keywords

FAQs

Package last updated on 27 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc