flipper-plugin
Version:
Flipper Desktop plugin SDK and components
45 lines • 1.47 kB
JavaScript
"use strict";
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
Object.defineProperty(exports, "__esModule", { value: true });
class LowPassFilter {
constructor(smoothing = 0.9) {
this._nextReduce = (last, current) => {
return this.smoothing * current + (1 - this.smoothing) * last;
};
this.smoothing = smoothing;
this.buffer = [];
this.bufferMaxSize = 5;
}
hasFullBuffer() {
return this.buffer.length === this.bufferMaxSize;
}
push(value) {
let removed = 0;
if (this.hasFullBuffer()) {
const tmp = this.buffer.shift();
if (tmp === undefined)
throw new Error('Invariant violation: Buffer reported full but shift returned nothing.');
removed = tmp;
}
this.buffer.push(value);
return removed;
}
next(nextValue) {
// push new value to the end, and remove oldest one
const removed = this.push(nextValue);
// smooth value using all values from buffer
const result = this.buffer.reduce(this._nextReduce, removed);
// replace smoothed value
this.buffer[this.buffer.length - 1] = result;
return result;
}
}
exports.default = LowPassFilter;
//# sourceMappingURL=LowPassFilter.js.map