From 820829581d2492b71fc1fcd0e2658f8380e27491 Mon Sep 17 00:00:00 2001 From: Cesar Gonzalez Date: Fri, 31 May 2024 16:31:56 -0500 Subject: [PATCH] [PM-8027] Updating how we guard against excessive getPageDetails calls --- .../collect-autofill-content.service.ts | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/apps/browser/src/autofill/services/collect-autofill-content.service.ts b/apps/browser/src/autofill/services/collect-autofill-content.service.ts index 5218961014..1417a318ac 100644 --- a/apps/browser/src/autofill/services/collect-autofill-content.service.ts +++ b/apps/browser/src/autofill/services/collect-autofill-content.service.ts @@ -44,7 +44,7 @@ class CollectAutofillContentService implements CollectAutofillContentServiceInte private elementInitializingIntersectionObserver: Set = new Set(); private mutationObserver: MutationObserver; private mutationsQueue: MutationRecord[][] = []; - private currentlyUpdatingPageDetails = false; + private updateAfterMutationIdleCallback: number; private readonly updateAfterMutationTimeout = 1000; private readonly formFieldQueryString; private readonly nonInputFormFieldTags = new Set(["textarea", "select"]); @@ -1223,19 +1223,13 @@ class CollectAutofillContentService implements CollectAutofillContentServiceInte * @private */ private updateAutofillElementsAfterMutation() { - if (this.currentlyUpdatingPageDetails) { - return; + if (this.updateAfterMutationIdleCallback) { + globalThis.cancelIdleCallback(this.updateAfterMutationIdleCallback); } - this.currentlyUpdatingPageDetails = true; - globalThis.requestIdleCallback( - async () => { - await this.getPageDetails(); - this.currentlyUpdatingPageDetails = false; - }, - { - timeout: this.updateAfterMutationTimeout, - }, + this.updateAfterMutationIdleCallback = globalThis.requestIdleCallback( + this.getPageDetails.bind(this), + { timeout: this.updateAfterMutationTimeout }, ); } @@ -1457,6 +1451,9 @@ class CollectAutofillContentService implements CollectAutofillContentServiceInte * timeouts and disconnects the mutation observer. */ destroy() { + if (this.updateAfterMutationIdleCallback) { + globalThis.cancelIdleCallback(this.updateAfterMutationIdleCallback); + } this.mutationObserver?.disconnect(); this.intersectionObserver?.disconnect(); }