You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
const Cycle = require("./cycle").Cycle;
|
|
|
|
const logger = require("./logger");
|
|
|
|
|
|
|
|
class CycleBuffer {
|
|
|
|
constructor(purgeTime, fixed=false) {
|
|
|
|
this.time = purgeTime;
|
|
|
|
this.buffer=[];
|
|
|
|
var th = this;
|
|
|
|
this.onclear = function(sp) {};
|
|
|
|
|
|
|
|
if(fixed) {
|
|
|
|
this.fixed=true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.inter = setInterval(function() {
|
|
|
|
th.purge();
|
|
|
|
}, this.time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
purge()
|
|
|
|
{
|
|
|
|
this.onclear(this);
|
|
|
|
this.buffer.length=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
add(cycle) {
|
|
|
|
this.buffer.push(cycle);
|
|
|
|
if(this.fixed && this.buffer.length>=this.time)
|
|
|
|
this.buffer.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
all() {
|
|
|
|
var ret=[];
|
|
|
|
for(var i=0;i<this.buffer.length;i++)
|
|
|
|
{
|
|
|
|
ret.push(this.buffer[i]);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
close() {
|
|
|
|
if(this.inter)
|
|
|
|
clearInterval(this.inter);
|
|
|
|
this.buffer.length=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CycleBuffer;
|