patrolling

1.1.0 • Public • Published

patrolling

NPM Package Dependency status

NPM

Patrol buffers and flush, periodically or when the buffer is full.

Installation

npm install patrolling

Usage

import Patrolling from 'patrolling';
 
/**
 * @class Patrolling
 *
 * @constructor
 * @param {number} capacity The capacity of the buffer, i.e., maximum number of elements to hold.
 * @param {number} timeout Timeout in milliseconds to flush the buffer.
 * @param {function} flush How to flush the buffer.
 * @param {function} push How to push an element into the buffer.
 */
var patrolling = new Patrolling( capacity, timeout, flush, push );

Example

const buffer = [];
 
const capacity = 10;
const timeout = 100;
const flush = function() { buffer = []; console.log( 'flushed' ); }
const push = function( elem ) { buffer.push( elem ); }
 
const cache = new Patrolling( capacity, timeout, flush, push );
 
// At the tenth iteration the buffer is flushed because it hits the capacity.
// The buffer ends up with five elements left when the loop ends.
for ( var i = 0; i < 15; ++) {
  cache.push( i );
}
 
// If execution so far lasts for at least 100 milliseconds, the remaining five
// elements are flushed.
 
// You can flush it yourself anytime as well.
cache.flush();

License

MIT. See LICENSE.md for details.

Package Sidebar

Install

npm i patrolling

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

8.16 kB

Total Files

7

Last publish

Collaborators

  • szchenghuang