Skip to content

Commit b8bf1de

Browse files
author
John Doe
committed
refactor: fix lint
1 parent 3880800 commit b8bf1de

File tree

3 files changed

+78
-67
lines changed

3 files changed

+78
-67
lines changed

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import {
55
} from 'node:perf_hooks';
66
import type { Buffered, Encoder, Sink } from './sink-source.types.js';
77

8-
export interface PerformanceObserverOptions<T> {
8+
export const DEFAULT_FLUSH_THRESHOLD = 20;
9+
10+
export type PerformanceObserverOptions<T> = {
911
sink: Sink<T, unknown>;
1012
encode: (entry: PerformanceEntry) => T[];
1113
captureBuffered?: boolean;
1214
flushThreshold?: number;
13-
}
15+
};
1416

1517
export class PerformanceObserverHandle<T>
1618
implements Buffered, Encoder<PerformanceEntry, T[]>
@@ -28,7 +30,7 @@ export class PerformanceObserverHandle<T>
2830
this.#encode = options.encode;
2931
this.#sink = options.sink;
3032
this.#captureBuffered = options.captureBuffered ?? false;
31-
this.#flushThreshold = options.flushThreshold ?? 20;
33+
this.#flushThreshold = options.flushThreshold ?? DEFAULT_FLUSH_THRESHOLD;
3234
this.#observedEntryCount = 0;
3335
}
3436

@@ -37,7 +39,9 @@ export class PerformanceObserverHandle<T>
3739
}
3840

3941
connect(): void {
40-
if (this.#observer || this.#closed) return;
42+
if (this.#observer || this.#closed) {
43+
return;
44+
}
4145
this.#observer = new PerformanceObserver(() => {
4246
this.#observedEntryCount++;
4347
if (this.#observedEntryCount >= this.#flushThreshold) {
@@ -53,42 +57,50 @@ export class PerformanceObserverHandle<T>
5357
}
5458

5559
flush(clear = false): void {
56-
if (this.#closed || !this.#sink) return;
60+
if (this.#closed || !this.#sink) {
61+
return;
62+
}
5763
const entries = [
5864
...performance.getEntriesByType('mark'),
5965
...performance.getEntriesByType('measure'),
6066
];
6167

6268
// Process all entries
63-
for (const e of entries) {
64-
if (e.entryType !== 'mark' && e.entryType !== 'measure') continue;
65-
66-
// Skip if already processed (unless clearing)
67-
if (!clear && this.#processedEntries.has(e.name)) continue;
69+
entries
70+
.filter(e => e.entryType === 'mark' || e.entryType === 'measure')
71+
.filter(e => clear || !this.#processedEntries.has(e.name))
72+
.forEach(e => {
73+
const encoded = this.encode(e);
74+
encoded.forEach(item => {
75+
this.#sink.write(item);
76+
});
6877

69-
const encoded = this.encode(e);
70-
for (const item of encoded) {
71-
this.#sink.write(item);
72-
}
73-
74-
if (clear) {
75-
this.#processedEntries.delete(e.name);
76-
if (e.entryType === 'mark') performance.clearMarks(e.name);
77-
if (e.entryType === 'measure') performance.clearMeasures(e.name);
78-
} else {
79-
this.#processedEntries.add(e.name);
80-
}
81-
}
78+
if (clear) {
79+
this.#processedEntries.delete(e.name);
80+
if (e.entryType === 'mark') {
81+
performance.clearMarks(e.name);
82+
}
83+
if (e.entryType === 'measure') {
84+
performance.clearMeasures(e.name);
85+
}
86+
} else {
87+
this.#processedEntries.add(e.name);
88+
}
89+
});
8290
}
8391

8492
disconnect(): void {
85-
if (!this.#observer) return;
93+
if (!this.#observer) {
94+
return;
95+
}
8696
this.#observer?.disconnect();
8797
this.#observer = undefined;
8898
}
8999

90100
close(): void {
91-
if (this.#closed) return;
101+
if (this.#closed) {
102+
return;
103+
}
92104
this.flush();
93105
this.#closed = true;
94106
this.disconnect();

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ vi.mock('node:perf_hooks', () => ({
2626

2727
class MockSink<T> implements Sink<T, unknown> {
2828
open(): void {
29-
throw new Error('Method not implemented.');
29+
throw new Error(`Method not implemented in ${this.constructor.name}.`);
3030
}
3131

3232
close(): void {
33-
throw new Error('Method not implemented.');
33+
throw new Error(`Method not implemented in ${this.constructor.name}.`);
3434
}
3535

36-
encode(input: T): unknown {
37-
throw new Error('Method not implemented.');
36+
encode(_input: T): unknown {
37+
throw new Error(`Method not implemented in ${this.constructor.name}.`);
3838
}
3939

4040
written: T[] = [];
@@ -59,8 +59,7 @@ const mockMeasureEntry = {
5959
} as PerformanceEntry;
6060

6161
describe('PerformanceObserverHandle', () => {
62-
let getEntriesByTypeSpy = vi.spyOn(performance, 'getEntriesByType');
63-
let observedTrigger: (() => void) | undefined;
62+
const getEntriesByTypeSpy = vi.spyOn(performance, 'getEntriesByType');
6463
let mockObserverInstance: {
6564
observe: MockedFunction<any>;
6665
disconnect: MockedFunction<any>;
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
export interface Encoder<I, O> {
2-
encode(input: I): O;
3-
}
4-
5-
export interface Decoder<O, I> {
6-
decode(output: O): I;
7-
}
8-
9-
export interface Sink<I, O> extends Encoder<I, O> {
10-
open(): void;
11-
write(input: I): void;
12-
close(): void;
13-
}
14-
15-
export interface Buffered {
16-
flush(): void;
17-
}
18-
export interface BufferedSink<I, O> extends Sink<I, O>, Buffered {}
19-
20-
export interface Source<I, O = any> {
21-
read?(): O;
22-
decode?(input: I): O;
23-
}
24-
25-
export interface Recoverable {
26-
recover(): RecoverResult;
27-
repack(): void;
28-
finalize(): void;
29-
}
30-
31-
export interface RecoverResult<T = any> {
1+
export type Encoder<I, O> = {
2+
encode: (input: I) => O;
3+
};
4+
5+
export type Decoder<O, I> = {
6+
decode: (output: O) => I;
7+
};
8+
9+
export type Sink<I, O> = {
10+
open: () => void;
11+
write: (input: I) => void;
12+
close: () => void;
13+
} & Encoder<I, O>;
14+
15+
export type Buffered = {
16+
flush: () => void;
17+
};
18+
export type BufferedSink<I, O> = {} & Sink<I, O> & Buffered;
19+
20+
export type Source<I, O = unknown> = {
21+
read?: () => O;
22+
decode?: (input: I) => O;
23+
};
24+
25+
export type Recoverable = {
26+
recover: () => RecoverResult;
27+
repack: () => void;
28+
finalize: () => void;
29+
};
30+
31+
export type RecoverResult<T = unknown> = {
3232
records: T[];
33-
errors: Array<{ lineNo: number; line: string; error: Error }>;
33+
errors: { lineNo: number; line: string; error: Error }[];
3434
partialTail: string | null;
35-
}
35+
};
3636

37-
export interface RecoverOptions<T = any> {
37+
export type RecoverOptions = {
3838
keepInvalid?: boolean;
39-
}
39+
};
4040

41-
export interface Output<I, O> extends BufferedSink<I, O> {}
41+
export type Output<I, O> = {} & BufferedSink<I, O>;

0 commit comments

Comments
 (0)