The iterator over the circular buffer.
The user’s function, fn, will be called with arguments fn(listIndex, itemIndex)
where listIndex is the saved record index and itemIndex is the actual item index.
this.forEach = function forEach(fn) {
if (itemIndex === -1) {
return;
}
if (itemIndex < maxListSize) {
for (let i = 0; i <= itemIndex; i += 1) {
fn(i, i);
}
return;
}
for (let i = itemIndex - maxListSize + 1; i <= itemIndex; i += 1) {
const listIndex = (i + maxListSize) % maxListSize;
fn(listIndex, i);
}
};
};