From 5631f3f1c9eebe7ca5b9c4025a3ded486821c322 Mon Sep 17 00:00:00 2001 From: Garrett Robinson Date: Wed, 23 Apr 2014 17:30:10 -0700 Subject: [PATCH] fix race condition in resetting data, fixes #506 --- data/aggregate.js | 9 +-------- data/ui.js | 7 ++++++- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/data/aggregate.js b/data/aggregate.js index d1ac910..ca5646e 100644 --- a/data/aggregate.js +++ b/data/aggregate.js @@ -30,7 +30,7 @@ aggregate.edgemap = {}; function resetData() { console.log('aggregate::resetData'); - aggregate.getBlockedDomains().filter(function (domain) { + aggregate.getBlockedDomains().forEach(function (domain) { console.log("deleting", domain); delete userSettings[domain]; }); @@ -48,13 +48,6 @@ aggregate.on('reset', resetData); aggregate.getBlockedDomains = function () { return Object.keys(userSettings).filter(function (domain) { - // ignore domains already known - var nodes = aggregate.nodes; - for (var i = nodes.length - 1; i >= 0; i--) { - if (nodes[i].name == domain) { - return false; - } - } return userSettings[domain] == 'block'; }); } diff --git a/data/ui.js b/data/ui.js index d7b7a66..ade3db5 100644 --- a/data/ui.js +++ b/data/ui.js @@ -156,7 +156,12 @@ document.querySelector('.reset-data').addEventListener('click', function () { allConnections = []; global.self.port.emit('reset'); aggregate.emit('reset'); - location.reload(); // reload page + // WARNING: this is a race condition. Since the event emitters are + // async, we were running into the situation where the page was reloaded + // before aggregate::resetData was finished, resulting in #506 + // + // TODO: using a short timeout for now, would be better to use a Promise + setTimeout(500, function () { location.reload(); /* reload page */ }); } }); });