Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

timed-buffer

A buffer for NodeJS that keeps anything you push into it for a set amount of time.

#Note

Currently not published on npm, I'll publish it after some further testing.

#Usage

##Instantiation TimedBuffer takes 2 parameters, a time in miliseconds and an optional options object.

var TimedBuffer = require('./timed-buffer'),
	buffer      = new TimedBuffer(1000, {
		timePadding: 0, //NUMBER Defaults to 0
		defaultLength: 5, //NUMBER Defaults to 2
		sampleRateRetention: 25 //NUMBER Defaults to 50
	});

setInterval(function (){
	buffer.push({foo: 'bar'})
}, 100);

setInterval(function (){
	var maxLength       = buffer.length, //Computed max-length based on sample rate
		actualLength    = buffer.buffer.length; //Actual Buffer Length
	
	console.log(maxLength, actualLength) 
}, 300);

###Optional parameters

  • timePadding: Used to pad the timeMS can be used to ensure the retrieval of a full-buffer when using getByTime with a value equal to the timeMS.
  • defaultLength: Default value of the length property, used to ensure we don't slice an empty array.
  • sampleRateRetention: The length of the internal array of sample-rate (times between pushes) values.

##Methods

###push Accepts 1 parameter, namely the item that is to be pushed into the buffer.

var TimedBuffer = require('./timed-buffer'),
	buffer      = new TimedBuffer(1000);

buffer.push({foo:'bar'});

####note: Uses unshift internally.

This also triggers the sample-rate checks, a buffer that isn't pushed to for a long time won't empty out on it's own. Timed-buffer was developed with a constant stream of data in mind.

###getLast Returns the last item pushed into the buffer.

var TimedBuffer = require('./timed-buffer'),
	buffer      = new TimedBuffer(1000);

buffer.push({foo:'bar'});
buffer.getLast(); //{foo:'bar'}
buffer.push({wat:'wut'})
buffer.getLast(); //{wat:'wut'}

###getByTime Accepts 2 parameters:

  • timeMS required (amount of miliseconds to request from the buffer)
  • skipMS optional (amount of miliseconds to skip before requesting the timeMS block)
var TimedBuffer = require('./timed-buffer'),
	buffer      = new TimedBuffer(1000);

buffer.getByTime(500); //Get the last 500ms of data
buffer.getByTime(500, 300); //Get 500ms worth of data that was commited 300ms ago

##Events

###add Supplies the added item.

var TimedBuffer = require('./timed-buffer'),
	buffer      = new TimedBuffer(1000);
	
buffer.on('add', function (addedItem){
    console.log(addedItem);
})

###change Supplies the entire buffer when a change is made.

var TimedBuffer = require('./timed-buffer'),
	buffer      = new TimedBuffer(1000);
	
buffer.on('change', function (buffer){
    console.log(buffer);
})

####note The array is sorted with the newest values at the beginning and the oldest values at the end.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages