Skip to content

Commit 3880800

Browse files
author
John Doe
committed
refactor: add performance observer
1 parent 16629a4 commit 3880800

File tree

2 files changed

+251
-335
lines changed

2 files changed

+251
-335
lines changed

packages/utils/src/lib/performance-observer.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ import type { Buffered, Encoder, Sink } from './sink-source.types.js';
88
export interface PerformanceObserverOptions<T> {
99
sink: Sink<T, unknown>;
1010
encode: (entry: PerformanceEntry) => T[];
11-
onEntry?: (entry: T) => void;
1211
captureBuffered?: boolean;
13-
flushEveryN?: number;
12+
flushThreshold?: number;
1413
}
1514

1615
export class PerformanceObserverHandle<T>
1716
implements Buffered, Encoder<PerformanceEntry, T[]>
1817
{
1918
#encode: (entry: PerformanceEntry) => T[];
2019
#captureBuffered: boolean;
21-
#flushEveryN: number;
20+
#observedEntryCount: number;
2221
#flushThreshold: number;
23-
#onEntry?: (entry: T) => void;
2422
#processedEntries = new Set<string>();
2523
#sink: Sink<T, unknown>;
2624
#observer: PerformanceObserver | undefined;
@@ -30,9 +28,8 @@ export class PerformanceObserverHandle<T>
3028
this.#encode = options.encode;
3129
this.#sink = options.sink;
3230
this.#captureBuffered = options.captureBuffered ?? false;
33-
this.#flushThreshold = options.flushEveryN ?? 20;
34-
this.#flushEveryN = 0;
35-
this.#onEntry = options.onEntry;
31+
this.#flushThreshold = options.flushThreshold ?? 20;
32+
this.#observedEntryCount = 0;
3633
}
3734

3835
encode(entry: PerformanceEntry): T[] {
@@ -42,10 +39,10 @@ export class PerformanceObserverHandle<T>
4239
connect(): void {
4340
if (this.#observer || this.#closed) return;
4441
this.#observer = new PerformanceObserver(() => {
45-
this.#flushEveryN++;
46-
if (this.#flushEveryN >= this.#flushThreshold) {
42+
this.#observedEntryCount++;
43+
if (this.#observedEntryCount >= this.#flushThreshold) {
4744
this.flush();
48-
this.#flushEveryN = 0;
45+
this.#observedEntryCount = 0;
4946
}
5047
});
5148

@@ -72,7 +69,6 @@ export class PerformanceObserverHandle<T>
7269
const encoded = this.encode(e);
7370
for (const item of encoded) {
7471
this.#sink.write(item);
75-
this.#onEntry?.(item);
7672
}
7773

7874
if (clear) {

0 commit comments

Comments
 (0)