From 3109d19e3c52f0eb81a8baa2563ae4324ba720de Mon Sep 17 00:00:00 2001 From: gorhill Date: Sat, 20 May 2017 15:32:03 -0400 Subject: [PATCH] code review: avoid compiling twice when list is fetched from remote location the 1st time --- src/js/storage.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/js/storage.js b/src/js/storage.js index e9260129f..4a43eb0a9 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -658,21 +658,34 @@ µBlock.getCompiledFilterList = function(assetKey, callback) { var µb = this, - compiledPath = 'compiled/' + assetKey; + compiledPath = 'compiled/' + assetKey, + rawContent; + + var onCompiledListLoaded2 = function(details) { + if ( details.content === '' ) { + details.content = µb.compileFilters(rawContent); + µb.assets.put(compiledPath, details.content); + } + rawContent = undefined; + details.assetKey = assetKey; + callback(details); + }; var onRawListLoaded = function(details) { - details.assetKey = assetKey; if ( details.content === '' ) { + details.assetKey = assetKey; callback(details); return; } µb.extractFilterListMetadata(assetKey, details.content); - details.content = µb.compileFilters(details.content); - µb.assets.put(compiledPath, details.content); - callback(details); + // Fectching the raw content may cause the compiled content to be + // generated somewhere else in uBO, hence we try one last time to + // fetch the compiled content in case it has become available. + rawContent = details.content; + µb.assets.get(compiledPath, onCompiledListLoaded2); }; - var onCompiledListLoaded = function(details) { + var onCompiledListLoaded1 = function(details) { if ( details.content === '' ) { µb.assets.get(assetKey, onRawListLoaded); return; @@ -681,7 +694,7 @@ callback(details); }; - this.assets.get(compiledPath, onCompiledListLoaded); + this.assets.get(compiledPath, onCompiledListLoaded1); }; /******************************************************************************/