Skip to content

Commit 1247369

Browse files
committed
PR feedback.
1 parent 241598b commit 1247369

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
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: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,20 @@ export default class RollingBuffer {
4343

4444
toArray(): any[] {
4545
const asArray: any[] = [];
46-
const size = this._buffers.reduce((acc: number, item: EventBuffer) => {
47-
if (item.isPopulated()) {
48-
return acc + 1;
49-
}
50-
return acc;
51-
}, 0);
5246

53-
for (let index = this._headPointer; index < this._headPointer + size; index += 1) {
47+
// Loop through the buffers, apprending their contents to asArray, until we find an empty one.
48+
for (
49+
let index = this._headPointer;
50+
index < this._headPointer + this._buffers.length;
51+
index += 1
52+
) {
5453
const realIndex = index % this._buffers.length;
55-
asArray.push(...this._buffers[realIndex].content);
54+
const item = this._buffers[realIndex];
55+
56+
if (!item.isPopulated) {
57+
break;
58+
}
59+
asArray.push(...item.content);
5660
}
5761

5862
return asArray;

0 commit comments

Comments
 (0)