From dc291337a82392bb86b7cf45a3c581546ca9b439 Mon Sep 17 00:00:00 2001 From: Sean Lee Date: Tue, 29 Aug 2017 14:09:18 +0800 Subject: [PATCH] Bug 1394704 - Early return ManageRecords.loadRecords during the loading process. r=lchang,scottwu MozReview-Commit-ID: 8yR6kDg67PL --HG-- extra : rebase_source : b08d90136ab02e3ef38da97c53d20d3a5623562f --- .../formautofill/content/manageDialog.js | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/browser/extensions/formautofill/content/manageDialog.js b/browser/extensions/formautofill/content/manageDialog.js index eb48f26923c1..de22129421b5 100644 --- a/browser/extensions/formautofill/content/manageDialog.js +++ b/browser/extensions/formautofill/content/manageDialog.js @@ -29,6 +29,8 @@ class ManageRecords { this._subStorageName = subStorageName; this._elements = elements; this._records = []; + this._newRequest = false; + this._isLoadingRecords = false; this.prefWin = window.opener; this.localizeDocument(); window.addEventListener("DOMContentLoaded", this, {once: true}); @@ -70,17 +72,40 @@ class ManageRecords { } /** - * Load records and render them. + * Load records and render them. This function is a wrapper for _loadRecords + * to ensure any reentrant will be handled well. */ async loadRecords() { + // This function can be early returned when there is any reentrant happends. + // "_newRequest" needs to be set to ensure all changes will be applied. + if (this._isLoadingRecords) { + this._newRequest = true; + return; + } + this._isLoadingRecords = true; + + await this._loadRecords(); + + // _loadRecords should be invoked again if there is any multiple entrant + // during running _loadRecords(). This step ensures that the latest request + // still is applied. + while (this._newRequest) { + this._newRequest = false; + await this._loadRecords(); + } + this._isLoadingRecords = false; + + // For testing only: Notify when records are loaded + this._elements.records.dispatchEvent(new CustomEvent("RecordsLoaded")); + } + + async _loadRecords() { let storage = await this.getStorage(); let records = storage.getAll(); // Sort by last modified time starting with most recent records.sort((a, b) => b.timeLastModified - a.timeLastModified); await this.renderRecordElements(records); this.updateButtonsStates(this._selectedOptions.length); - // For testing only: Notify when records are loaded - this._elements.records.dispatchEvent(new CustomEvent("RecordsLoaded")); } /**