Skip to content

Commit

Permalink
fix(engines): use options observer instead of internal listener (#2107)
Browse files Browse the repository at this point in the history
* fix(engines): use options observer instead of internal listener

* Update rules only when is change
  • Loading branch information
smalluban authored Dec 11, 2024
1 parent bbe8d4d commit ba96ec5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
26 changes: 19 additions & 7 deletions src/background/exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import { parseFilter } from '@ghostery/adblocker';

import * as OptionsObserver from '/utils/options-observer.js';
import * as trackerdb from '/utils/trackerdb.js';
import * as engines from '/utils/engines.js';

import {
createDocumentConverter,
Expand Down Expand Up @@ -137,15 +137,27 @@ async function updateFilters() {
.filter(({ id }) => id >= 2000000)
.map(({ id }) => id);

await chrome.declarativeNetRequest.updateDynamicRules({
addRules,
removeRuleIds,
});
if (addRules.length || removeRuleIds.length) {
await chrome.declarativeNetRequest.updateDynamicRules({
addRules,
removeRuleIds,
});

console.info('[exceptions] DNR rules for filters updated successfully');
console.info('[exceptions] Updated DNR rules');
}
}

if (__PLATFORM__ === 'chromium' || __PLATFORM__ === 'safari') {
// Update exceptions filters every time TrackerDB updates
engines.addChangeListener(engines.TRACKERDB_ENGINE, updateFilters);
// It happens when all engines are updated
OptionsObserver.addListener(
'filtersUpdatedAt',
async function updateExceptions(value, lastValue) {
// Only update exceptions filters if the value has changed and is set to timestamp.
// It will happen only after successful update of the engines.
if (lastValue !== undefined && value !== 0) {
await updateFilters();
}
},
);
}
31 changes: 0 additions & 31 deletions src/utils/engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,30 +204,6 @@ function check(response) {
return response;
}

const updateListeners = new Map();
export function addChangeListener(name, fn) {
if (!updateListeners.has(name)) {
updateListeners.set(name, new Set());
}

updateListeners.get(name).add(fn);
}

function notifyListeners(name) {
const fns = updateListeners.get(name);

fns?.forEach((fn) => {
try {
fn();
} catch (e) {
console.error(
`[engines] Error while calling update listener for "${name}"`,
e,
);
}
});
}

const CDN_HOSTNAME = stagingMode
? 'staging-cdn.ghostery.com'
: 'cdn.ghostery.com';
Expand Down Expand Up @@ -411,9 +387,6 @@ export async function update(name) {
if (updated) {
console.info(`[engines] Engine "${name}" updated`);

// Notify listeners
notifyListeners(name);

// Save the new engine to storage
saveToStorage(name);

Expand Down Expand Up @@ -450,8 +423,6 @@ export function create(name, options = null) {
console.error(`[engines] Failed to save engine "${name}" to storage`);
});

notifyListeners(name);

return engine;
}

Expand All @@ -470,8 +441,6 @@ export function replace(name, engineOrEngines) {
console.error(`[engines] Failed to save engine "${name}" to storage`);
});

notifyListeners(name);

return engine;
}

Expand Down

0 comments on commit ba96ec5

Please sign in to comment.