| 52 | * The controller's `value` is usable during the host's update cycle. |
| 53 | */ |
| 54 | export class PerformanceController<T = unknown> implements ReactiveController { |
| 55 | private _host: ReactiveControllerHost; |
| 56 | private _config: PerformanceObserverInit; |
| 57 | private _observer!: PerformanceObserver; |
| 58 | private _skipInitial = false; |
| 59 | /** |
| 60 | * Flag used to help manage calling the `callback` when observe is called |
| 61 | * in addition to when a mutation occurs. This is done to help setup initial |
| 62 | * state and is performed async by requesting a host update and calling |
| 63 | * `handleChanges` once by checking and then resetting this flag. |
| 64 | */ |
| 65 | private _unobservedUpdate = false; |
| 66 | /** |
| 67 | * The result of processing the observer's changes via the `callback` |
| 68 | * function. |
| 69 | */ |
| 70 | value?: T; |
| 71 | /** |
| 72 | * Function that returns a value processed from the observer's changes. |
| 73 | * The result is stored in the `value` property. |
| 74 | */ |
| 75 | callback?: PerformanceValueCallback<T>; |
| 76 | constructor( |
| 77 | host: ReactiveControllerHost, |
| 78 | {config, callback, skipInitial}: PerformanceControllerConfig<T> |
| 79 | ) { |
| 80 | this._host = host; |
| 81 | this._config = config; |
| 82 | this._skipInitial = skipInitial ?? this._skipInitial; |
| 83 | this.callback = callback; |
| 84 | if (isServer) { |
| 85 | return; |
| 86 | } |
| 87 | // Check browser support. |
| 88 | if (!window.PerformanceObserver) { |
| 89 | console.warn( |
| 90 | `PerformanceController error: browser does not support PerformanceObserver.` |
| 91 | ); |
| 92 | return; |
| 93 | } |
| 94 | this._observer = new PerformanceObserver( |
| 95 | (entryList: PerformanceObserverEntryList) => { |
| 96 | this.handleChanges(entryList.getEntries(), entryList); |
| 97 | this._host.requestUpdate(); |
| 98 | } |
| 99 | ); |
| 100 | host.addController(this); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Process the observer's changes with the controller's `callback` |
| 105 | * function to produce a result stored in the `value` property. |
| 106 | */ |
| 107 | protected handleChanges( |
| 108 | entries: PerformanceEntryList, |
| 109 | entryList?: PerformanceObserverEntryList |
| 110 | ) { |
| 111 | this.value = this.callback?.(entries, this._observer, entryList); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…