open-nomad/ui/app/utils/classes/rolling-array.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

// An array with a max length.
//
// When max length is surpassed, items are removed from
// the front of the array.
// Native array methods
let { push, splice } = Array.prototype;
// Ember array prototype extension
let { insertAt } = Array.prototype;
// Using Classes to extend Array is unsupported in Babel so this less
// ideal approach is taken: https://babeljs.io/docs/en/caveats#classes
export default function RollingArray(maxLength, ...items) {
const array = new Array(...items);
array.maxLength = maxLength;
// Bring the length back down to maxLength by removing from the front
2021-12-28 14:45:20 +00:00
array._limit = function () {
const surplus = this.length - this.maxLength;
if (surplus > 0) {
this.splice(0, surplus);
}
};
2021-12-28 14:45:20 +00:00
array.push = function (...items) {
push.apply(this, items);
this._limit();
return this.length;
};
2021-12-28 14:45:20 +00:00
array.splice = function (...args) {
const returnValue = splice.apply(this, args);
this._limit();
return returnValue;
};
// All mutable array methods build on top of insertAt
2021-12-28 14:45:20 +00:00
array.insertAt = function (...args) {
const returnValue = insertAt.apply(this, args);
this._limit();
this.arrayContentDidChange();
return returnValue;
};
2021-12-28 14:45:20 +00:00
array.unshift = function () {
throw new Error('Cannot unshift onto a RollingArray');
};
return array;
}