Skip to content

Commit

Permalink
fix: optimize JS native call
Browse files Browse the repository at this point in the history
  • Loading branch information
robik committed Sep 19, 2024
1 parent 3ec90f8 commit cad9c9c
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class PerformanceObserver {
#nativeObserverHandle: mixed | void;
#callback: PerformanceObserverCallback;
#type: 'single' | 'multiple' | void;
#calledAtLeastOnce = false;

constructor(callback: PerformanceObserverCallback) {
this.#callback = callback;
Expand Down Expand Up @@ -153,7 +154,7 @@ export class PerformanceObserver {

// The same observer may receive multiple calls to "observe", so we need
// to check what is new on this call vs. previous ones.
NativePerformanceObserver.observe(this.#observerHandle, {
NativePerformanceObserver.observe(this.#nativeObserverHandle, {
entryTypes,
durationThreshold:
options.type === 'event' ? options.durationThreshold : undefined,
Expand All @@ -174,9 +175,14 @@ export class PerformanceObserver {
}

#createNativeObserver() {
this.#calledAtLeastOnce = false;
return NativePerformanceObserver.createObserver(() => {
const entryList = new PerformanceObserverEntryList(NativePerformanceObserver.takeRecords(this.#observerHandle));
const droppedEntriesCount = NativePerformanceObserver.getDroppedEntriesCount(this.#nativeObserverHandle);
const entryList = new PerformanceObserverEntryList(NativePerformanceObserver.takeRecords(this.#nativeObserverHandle));
let droppedEntriesCount = 0;
if (!this.#calledAtLeastOnce) {
droppedEntriesCount = NativePerformanceObserver.getDroppedEntriesCount(this.#nativeObserverHandle);
this.#calledAtLeastOnce = true;
}
this.#callback(entryList, this, { droppedEntriesCount });
});
}
Expand Down

0 comments on commit cad9c9c

Please sign in to comment.