Skip to content

Commit

Permalink
Sweep for menu commands referencing stale windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
arantius committed Jul 7, 2015
1 parent 8360d3b commit 4966b74
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions content/menucommander.js
Expand Up @@ -16,6 +16,29 @@ GM_MenuCommander.initialize = function() {
GM_MenuCommander.clearMenuCommands);
messageManager.addMessageListener('greasemonkey:toggle-menu-commands',
GM_MenuCommander.toggleMenuCommands);

setInterval(GM_MenuCommander.sweep, 66000);
};

GM_MenuCommander.sweep = function() {
for (var windowId in GM_MenuCommander.menuCommands) {
var commands = GM_MenuCommander.menuCommands[windowId];

// Rather than mutate the (commands) array in the loop, which is failure
// prone, just track whether it is "empty". All commands should go
// stale together, or not. If empty, we can safely mutate the object
// (menuCommands) to clean it out, even inside its loop.
var empty = true;
for (var i in commands) {
var b = commands[i].browser.get();
if (b) empty = false;
}

if (empty) {
GM_MenuCommander.menuCommands[windowId] = null;
delete GM_MenuCommander.menuCommands[windowId];
}
}
};

GM_MenuCommander.menuCommandRegistered = function(aMessage) {
Expand Down

0 comments on commit 4966b74

Please sign in to comment.