Skip to content

Commit aba697f

Browse files
committed
1 parent e768b78 commit aba697f

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

src/js/hosts-files.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ vAPI.messaging.addListener(function onMessage(msg) {
4848
case 'loadHostsFilesCompleted':
4949
renderHostsFiles();
5050
break;
51+
case 'loadRecipeFilesCompleted':
52+
renderHostsFiles();
53+
break;
5154
default:
5255
break;
5356
}
@@ -70,9 +73,7 @@ var renderHostsFiles = function(soft) {
7073
// Assemble a pretty list name if possible
7174
var listNameFromListKey = function(collection, listKey) {
7275
let list = collection.get(listKey);
73-
let listTitle = list ? list.title : '';
74-
if ( listTitle === '' ) { return listKey; }
75-
return listTitle;
76+
return list && list.title || listKey;
7677
};
7778

7879
var liFromListEntry = function(collection, listKey, li) {
@@ -132,13 +133,22 @@ var renderHostsFiles = function(soft) {
132133
return li;
133134
};
134135
var onRenderAssetFiles = function(collection, listSelector) {
136+
// Incremental rendering: this will allow us to easily discard unused
137+
// DOM list entries.
138+
uDom(listSelector + ' .listEntry:not(.notAnAsset)').addClass('discard');
139+
135140
var assetKeys = Array.from(collection.keys());
136141

137142
// Sort works this way:
138143
// - Send /^https?:/ items at the end (custom hosts file URL)
139144
assetKeys.sort(function(a, b) {
140-
var ta = collection.get(a).title || a,
141-
tb = collection.get(b).title || b;
145+
let ea = collection.get(a),
146+
eb = collection.get(b);
147+
if ( ea.submitter !== eb.submitter ) {
148+
return ea.submitter !== 'user' ? -1 : 1;
149+
}
150+
let ta = ea.title || a,
151+
tb = eb.title || b;
142152
if ( reExternalHostFile.test(ta) === reExternalHostFile.test(tb) ) {
143153
return ta.localeCompare(tb);
144154
}
@@ -178,10 +188,6 @@ var renderHostsFiles = function(soft) {
178188
listDetails.contributor === true
179189
);
180190

181-
// Incremental rendering: this will allow us to easily discard unused
182-
// DOM list entries.
183-
uDom('#hosts .listEntry:not(.notAnAsset)').addClass('discard');
184-
185191
onRenderAssetFiles(details.hosts, '#hosts');
186192
onRenderAssetFiles(details.recipes, '#recipes');
187193

src/js/storage.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -273,33 +273,63 @@
273273
}
274274

275275
let µm = this,
276-
countdownCount = µm.userSettings.selectedRecipeFiles.length;
276+
countdownCount = 0;
277277

278278
if ( reset ) {
279279
µm.recipeManager.reset();
280280
}
281281

282-
var onLoaded = function(details) {
282+
let recipeMetadata;
283+
284+
let onDone = function() {
285+
vAPI.messaging.broadcast({ what: 'loadRecipeFilesCompleted' });
286+
µm.getBytesInUse();
287+
callback();
288+
};
289+
290+
let onLoaded = function(details) {
283291
if ( details.content ) {
292+
let entry = recipeMetadata.get(details.assetKey);
293+
if ( entry.submitter === 'user' ) {
294+
let match = /^! +Title: *(.+)$/im.exec(
295+
details.content.slice(2048)
296+
);
297+
if ( match !== null && match[1] !== entry.title ) {
298+
µm.assets.registerAssetSource(
299+
details.assetKey,
300+
{ title: match[1] }
301+
);
302+
}
303+
}
284304
µm.recipeManager.fromString(details.content);
285305
}
286306
countdownCount -= 1;
287307
if ( countdownCount === 0 ) {
288-
callback();
308+
onDone();
289309
}
290310
};
291311

292-
for ( let assetKey of µm.userSettings.selectedRecipeFiles ) {
293-
this.assets.get(assetKey, onLoaded);
294-
}
295-
296-
let userRecipes = µm.userSettings.userRecipes;
297-
if ( userRecipes.enabled ) {
298-
µm.recipeManager.fromString(
299-
'! uMatrix: Ruleset recipes 1.0\n' + userRecipes.content
300-
);
301-
}
312+
let onMetadataReady = function(metadata) {
313+
recipeMetadata = metadata;
314+
for ( let entry of metadata ) {
315+
let assetKey = entry[0];
316+
let recipeFile = entry[1];
317+
if ( recipeFile.selected !== true ) { continue; }
318+
µm.assets.get(assetKey, onLoaded);
319+
countdownCount += 1;
320+
}
321+
let userRecipes = µm.userSettings.userRecipes;
322+
if ( userRecipes.enabled ) {
323+
µm.recipeManager.fromString(
324+
'! uMatrix: Ruleset recipes 1.0\n' + userRecipes.content
325+
);
326+
}
327+
if ( countdownCount === 0 ) {
328+
onDone();
329+
}
330+
};
302331

332+
this.getAvailableRecipeFiles(onMetadataReady);
303333
};
304334

305335
/******************************************************************************/
@@ -406,8 +436,7 @@
406436
type: 'recipes',
407437
contentURL: assetKey,
408438
external: true,
409-
submitter: 'user',
410-
title: assetKey
439+
submitter: 'user'
411440
};
412441
this.assets.registerAssetSource(assetKey, entry);
413442
availableRecipeFiles.set(assetKey, entry);
@@ -717,9 +746,6 @@
717746
'externalRecipeFiles',
718747
'userRecipes'
719748
);
720-
if ( recipesChanged ) {
721-
µm.recipeManager.reset();
722-
}
723749
if ( typeof callback === 'function' ) {
724750
callback({
725751
hostsChanged: hostsChanged,

0 commit comments

Comments
 (0)