Skip to content

Commit 29bbd2d

Browse files
committed
PR feedback.
1 parent 241598b commit 29bbd2d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/telemetry/browser-telemetry/__tests__/collectors/rrweb/SessionBuffer.test.ts renamed to packages/telemetry/browser-telemetry/__tests__/collectors/rrweb/RollingBuffer.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,27 @@ it('when the buffer is exceeded it will wrap around', () => {
2727

2828
expect(buffer.toArray()).toEqual(expectedItems);
2929
});
30+
31+
it('can reset the buffer', () => {
32+
const bufferSize = 5;
33+
const numberBuffers = 4;
34+
const buffer = new RollingBuffer(bufferSize, numberBuffers);
35+
const demoItems = Array.from(new Array(10), (_, i) => i);
36+
37+
demoItems.forEach(buffer.push.bind(buffer));
38+
buffer.reset();
39+
40+
expect(buffer.toArray()).toEqual([]);
41+
});
42+
43+
it('returns correct items when buffer is partially filled', () => {
44+
const bufferSize = 5;
45+
const numberBuffers = 4;
46+
const buffer = new RollingBuffer(bufferSize, numberBuffers);
47+
const itemsToAdd = 7; // Less than total capacity
48+
const demoItems = Array.from(new Array(itemsToAdd), (_, i) => i);
49+
50+
demoItems.forEach(buffer.push.bind(buffer));
51+
52+
expect(buffer.toArray()).toEqual(demoItems);
53+
});

packages/telemetry/browser-telemetry/src/collectors/rrweb/RollingBuffer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ export default class RollingBuffer {
5050
return acc;
5151
}, 0);
5252

53-
for (let index = this._headPointer; index < this._headPointer + size; index += 1) {
53+
// Loop through the buffers, apprending their contents to asArray, until we find an empty one.
54+
for (let index = this._headPointer; index < this._headPointer + this._buffers.length; index += 1) {
5455
const realIndex = index % this._buffers.length;
55-
asArray.push(...this._buffers[realIndex].content);
56+
const item = this._buffers[realIndex];
57+
58+
if(!item.isPopulated) {
59+
break;
60+
}
61+
asArray.push(...item.content);
5662
}
5763

5864
return asArray;

0 commit comments

Comments
 (0)