Skip full period in rate

This commit is contained in:
Jerry Kurian 2019-08-16 10:06:23 -04:00
parent 50f6886b4b
commit 7394b6ef84
2 changed files with 5 additions and 5 deletions

View File

@ -9,13 +9,13 @@ import { TransformOptions } from "./baseDefinitions";
*/ */
export function rate( export function rate(
targetRate: number = 50, targetRate: number = 50,
period: number = 2, period: number = 1,
options: TransformOptions = { options: TransformOptions = {
readableObjectMode: true, readableObjectMode: true,
writableObjectMode: true, writableObjectMode: true,
}, },
): Transform { ): Transform {
const deltaMS = ((1 / targetRate) * 1000) / period; // Skip half a period const deltaMS = ((1 / targetRate) * 1000) / period; // Skip a full period
let total = 0; let total = 0;
const start = performance.now(); const start = performance.now();
return new Transform({ return new Transform({

View File

@ -19,7 +19,7 @@ test.cb("rate() sends data at desired rate", t => {
let k = 0; let k = 0;
sourceFast sourceFast
.pipe(rate(fastRate, 1)) .pipe(rate(fastRate))
.on("data", (element: string[]) => { .on("data", (element: string[]) => {
const currentRate = (i / (performance.now() - start)) * 1000; const currentRate = (i / (performance.now() - start)) * 1000;
expect(element).to.deep.equal(expectedElements[i]); expect(element).to.deep.equal(expectedElements[i]);
@ -30,7 +30,7 @@ test.cb("rate() sends data at desired rate", t => {
.on("error", t.end); .on("error", t.end);
sourceMed sourceMed
.pipe(rate(medRate, 1)) .pipe(rate(medRate))
.on("data", (element: string[]) => { .on("data", (element: string[]) => {
const currentRate = (j / (performance.now() - start)) * 1000; const currentRate = (j / (performance.now() - start)) * 1000;
expect(element).to.deep.equal(expectedElements[j]); expect(element).to.deep.equal(expectedElements[j]);
@ -41,7 +41,7 @@ test.cb("rate() sends data at desired rate", t => {
.on("error", t.end); .on("error", t.end);
sourceSlow sourceSlow
.pipe(rate(slowRate, 1)) .pipe(rate(slowRate))
.on("data", (element: string[]) => { .on("data", (element: string[]) => {
const currentRate = (k / (performance.now() - start)) * 1000; const currentRate = (k / (performance.now() - start)) * 1000;
expect(element).to.deep.equal(expectedElements[k]); expect(element).to.deep.equal(expectedElements[k]);