Skip to content

Commit 7e41c05

Browse files
committed
refactor: wip
1 parent 8334310 commit 7e41c05

5 files changed

Lines changed: 384 additions & 290 deletions

File tree

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,6 @@ export class PerformanceObserverSink<T> implements Observer, Buffered {
216216
if (this.#observer) {
217217
return;
218218
}
219-
if (this.#sink.isClosed()) {
220-
throw new Error(
221-
`Sink ${this.#sink.constructor.name} must be opened before subscribing PerformanceObserver`,
222-
);
223-
}
224219

225220
this.#observer = new PerformanceObserver(list => {
226221
list.getEntries().forEach(entry => {
@@ -272,18 +267,14 @@ export class PerformanceObserverSink<T> implements Observer, Buffered {
272267
if (this.#queue.length === 0) {
273268
return;
274269
}
275-
if (this.#sink.isClosed()) {
276-
// clear queue and drop items when sink closes unexpectedly
277-
this.#queue.length = 0;
278-
return;
279-
}
280270

281271
try {
282272
this.#queue.forEach(item => {
283273
this.#sink.write(item);
284274
this.#written++;
285275
});
286276
} catch (error) {
277+
this.#dropped += this.#queue.length;
287278
throw new Error(
288279
'PerformanceObserverSink failed to write items to sink.',
289280
{ cause: error },

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

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,6 @@ describe('PerformanceObserverSink', () => {
429429
);
430430
});
431431

432-
it('throws error when subscribing with sink that is not open', () => {
433-
const closedSink = new MockSink();
434-
const observer = new PerformanceObserverSink({
435-
sink: closedSink,
436-
encodePerfEntry,
437-
});
438-
439-
expect(() => observer.subscribe()).toThrow(
440-
'Sink MockSink must be opened before subscribing PerformanceObserver',
441-
);
442-
});
443-
444432
it('getStats returns dropped and queued item information', () => {
445433
const observer = new PerformanceObserverSink({
446434
sink,
@@ -622,49 +610,4 @@ describe('PerformanceObserverSink', () => {
622610

623611
observer.unsubscribe();
624612
});
625-
626-
it('clears queue without writing when sink is closed during flush', () => {
627-
const observer = new PerformanceObserverSink({
628-
sink,
629-
encodePerfEntry,
630-
flushThreshold: 10, // High threshold to prevent automatic flushing
631-
});
632-
633-
observer.subscribe();
634-
635-
const mockObserver = MockPerformanceObserver.lastInstance();
636-
mockObserver?.emit([
637-
{
638-
name: 'test-entry-1',
639-
entryType: 'mark',
640-
startTime: 0,
641-
duration: 0,
642-
},
643-
{
644-
name: 'test-entry-2',
645-
entryType: 'mark',
646-
startTime: 0,
647-
duration: 0,
648-
},
649-
]);
650-
651-
// Verify entries are queued
652-
expect(observer.getStats().queued).toBe(2);
653-
expect(observer.getStats().written).toBe(0);
654-
655-
// Close the sink
656-
sink.close();
657-
658-
// Flush should clear queue without writing
659-
observer.flush();
660-
661-
// Verify queue is cleared but written count unchanged
662-
expect(observer.getStats().queued).toBe(0);
663-
expect(observer.getStats().written).toBe(0);
664-
665-
// Verify sink received no additional writes
666-
expect(sink.getWrittenItems()).toHaveLength(0);
667-
668-
observer.unsubscribe();
669-
});
670613
});

packages/utils/src/lib/profiler/profiler.int.test.ts

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -279,23 +279,6 @@ describe('Profiler Integration', () => {
279279
]),
280280
);
281281
});
282-
283-
it('should not create performance entries when disabled', async () => {
284-
profiler.setEnabled(false);
285-
286-
expect(profiler.measure('disabled-sync', () => 'sync')).toBe('sync');
287-
288-
const asyncResult = profiler.measureAsync(
289-
'disabled-async',
290-
async () => 'async',
291-
);
292-
await expect(asyncResult).resolves.toBe('async');
293-
294-
profiler.marker('disabled-marker');
295-
296-
expect(performance.getEntriesByType('mark')).toHaveLength(0);
297-
expect(performance.getEntriesByType('measure')).toHaveLength(0);
298-
});
299282
});
300283

301284
describe('NodeJS Profiler Integration', () => {
@@ -343,8 +326,8 @@ describe('NodeJS Profiler Integration', () => {
343326
});
344327

345328
it('should disable profiling and close sink', () => {
346-
nodejsProfiler.setEnabled(false);
347-
expect(nodejsProfiler.isEnabled()).toBe(false);
329+
nodejsProfiler.stop();
330+
expect(nodejsProfiler.isRunning()).toBe(false);
348331
expect(mockSink.isClosed()).toBe(true);
349332
expect(mockSink.close).toHaveBeenCalledTimes(1);
350333

@@ -356,10 +339,10 @@ describe('NodeJS Profiler Integration', () => {
356339
});
357340

358341
it('should re-enable profiling correctly', () => {
359-
nodejsProfiler.setEnabled(false);
360-
nodejsProfiler.setEnabled(true);
342+
nodejsProfiler.stop();
343+
nodejsProfiler.start();
361344

362-
expect(nodejsProfiler.isEnabled()).toBe(true);
345+
expect(nodejsProfiler.isRunning()).toBe(true);
363346
expect(mockSink.isClosed()).toBe(false);
364347
expect(mockSink.open).toHaveBeenCalledTimes(2);
365348

@@ -396,14 +379,14 @@ describe('NodeJS Profiler Integration', () => {
396379
});
397380

398381
const bufferedStats = bufferedProfiler.getStats();
399-
expect(bufferedStats.enabled).toBe(true);
382+
expect(bufferedStats.state).toBe('running');
400383
expect(bufferedStats.walOpen).toBe(true);
401384
expect(bufferedStats.isSubscribed).toBe(true);
402385
expect(bufferedStats.queued).toBe(0);
403386
expect(bufferedStats.dropped).toBe(0);
404387
expect(bufferedStats.written).toBe(0);
405388

406-
bufferedProfiler.setEnabled(false);
389+
bufferedProfiler.stop();
407390
});
408391

409392
it('should return correct getStats with dropped and written counts', () => {
@@ -420,14 +403,14 @@ describe('NodeJS Profiler Integration', () => {
420403
expect(statsProfiler.measure('test-op', () => 'result')).toBe('result');
421404

422405
const stats = statsProfiler.getStats();
423-
expect(stats.enabled).toBe(true);
406+
expect(stats.state).toBe('running');
424407
expect(stats.walOpen).toBe(true);
425408
expect(stats.isSubscribed).toBe(true);
426409
expect(typeof stats.queued).toBe('number');
427410
expect(typeof stats.dropped).toBe('number');
428411
expect(typeof stats.written).toBe('number');
429412

430-
statsProfiler.setEnabled(false);
413+
statsProfiler.stop();
431414
});
432415

433416
it('should provide comprehensive queue statistics via getStats', () => {
@@ -443,7 +426,7 @@ describe('NodeJS Profiler Integration', () => {
443426

444427
// Initial stats should be zero
445428
const initialStats = profiler.getStats();
446-
expect(initialStats.enabled).toBe(true);
429+
expect(initialStats.state).toBe('running');
447430
expect(initialStats.walOpen).toBe(true);
448431
expect(initialStats.isSubscribed).toBe(true);
449432
expect(initialStats.queued).toBe(0);
@@ -467,10 +450,10 @@ describe('NodeJS Profiler Integration', () => {
467450
expect(statsAfterMeasurements.written).toBeGreaterThanOrEqual(0);
468451

469452
// Disable profiler to flush remaining items
470-
profiler.setEnabled(false);
453+
profiler.stop();
471454

472455
const finalStats = profiler.getStats();
473-
expect(finalStats.enabled).toBe(false); // Should be disabled
456+
expect(finalStats.state).toBe('idle'); // Should be idle
474457
expect(finalStats.walOpen).toBe(false); // WAL should be closed when disabled
475458
expect(finalStats.isSubscribed).toBe(false); // Should not be subscribed when disabled
476459
expect(finalStats.queued).toBe(0); // Should be cleared when disabled

0 commit comments

Comments
 (0)