@@ -5,12 +5,14 @@ import {
55} from 'node:perf_hooks' ;
66import 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
1517export 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 ( ) ;
0 commit comments