diff --git a/platform/chromium/vapi-common.js b/platform/chromium/vapi-common.js index 805467f01..efeb4c82f 100644 --- a/platform/chromium/vapi-common.js +++ b/platform/chromium/vapi-common.js @@ -230,10 +230,22 @@ vAPI.closePopup = function() { vAPI.localStorage = { start: function() { + if ( this.cache instanceof Promise ) { return this.cache; } if ( this.cache instanceof Object ) { return Promise.resolve(); } - if ( this.promise !== undefined ) { return this.promise; } - this.promise = new Promise(resolve => { + const onChanged = (changes, area) => { + if ( + area !== 'local' || + changes instanceof Object === false || + changes.localStorage instanceof Object === false + ) { + return; + } + const newValue = changes.localStorage.newValue; + this.cache = newValue instanceof Object ? newValue : {}; + }; + this.cache = new Promise(resolve => { browser.storage.local.get('localStorage', bin => { + this.cache = undefined; if ( bin instanceof Object === false || bin.localStorage instanceof Object === false @@ -255,22 +267,15 @@ vAPI.localStorage = { if ( this.cache instanceof Object === false ) { this.cache = {}; } - this.promise = undefined; - browser.storage.onChanged.addListener((changes, area) => { - if ( - area !== 'local' || - changes instanceof Object === false || - changes.localStorage instanceof Object === false - ) { - return; - } - const newValue = changes.localStorage.newValue; - this.cache = newValue instanceof Object ? newValue : {}; - }); resolve(); + browser.storage.onChanged.addListener(onChanged); + self.addEventListener('beforeunload', ( ) => { + this.cache = undefined; + browser.storage.onChanged.removeListener(onChanged); + }); }); }); - return this.promise; + return this.cache; }, clear: function() { this.cache = {}; @@ -295,11 +300,11 @@ vAPI.localStorage = { }, setItem: function(key, value = undefined) { return this.start().then(( ) => { + if ( value === this.cache[key] ) { return; } this.cache[key] = value; return browser.storage.local.set({ localStorage: this.cache }); }); }, - promise: undefined, cache: undefined, }; diff --git a/src/js/dashboard.js b/src/js/dashboard.js index 2323f8c05..20a4f618a 100644 --- a/src/js/dashboard.js +++ b/src/js/dashboard.js @@ -25,7 +25,8 @@ /******************************************************************************/ -(( ) => { +{ +// >>>>> start of local scope /******************************************************************************/ @@ -82,13 +83,7 @@ const discardUnsavedData = function(synchronous = false) { }); }; -const loadDashboardPanel = function(pane = '') { - if ( pane === '' ) { - vAPI.localStorage.getItemAsync('dashboardLastVisitedPane').then(value => { - loadDashboardPanel(value !== null ? value : 'settings.html'); - }); - return; - } +const loadDashboardPanel = function(pane, first) { const tabButton = uDom(`[href="#${pane}"]`); if ( !tabButton || tabButton.hasClass('selected') ) { return; } const loadPane = ( ) => { @@ -98,6 +93,9 @@ const loadDashboardPanel = function(pane = '') { uDom.nodeFromId('iframe').setAttribute('src', pane); vAPI.localStorage.setItem('dashboardLastVisitedPane', pane); }; + if ( first ) { + return loadPane(); + } const r = discardUnsavedData(); if ( r === false ) { return; } if ( r === true ) { @@ -122,18 +120,22 @@ vAPI.messaging.send('dashboard', { }); resizeFrame(); -loadDashboardPanel(); -window.addEventListener('resize', resizeFrame); -uDom('.tabButton').on('click', onTabClickHandler); +vAPI.localStorage.getItemAsync('dashboardLastVisitedPane').then(value => { + loadDashboardPanel(value !== null ? value : 'settings.html', true); -// https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event -window.addEventListener('beforeunload', ( ) => { - if ( discardUnsavedData(true) ) { return; } - event.preventDefault(); - event.returnValue = ''; + window.addEventListener('resize', resizeFrame); + uDom('.tabButton').on('click', onTabClickHandler); + + // https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event + window.addEventListener('beforeunload', ( ) => { + if ( discardUnsavedData(true) ) { return; } + event.preventDefault(); + event.returnValue = ''; + }); }); /******************************************************************************/ -})(); +// <<<<< end of local scope +}