Index: messageResponder.js |
=================================================================== |
--- a/messageResponder.js |
+++ b/messageResponder.js |
@@ -52,7 +52,9 @@ |
"downloadStatus", "homepage", "lastDownload", "title", "url"]); |
var convertFilter = convertObject.bind(null, ["text"]); |
- var changeListeners = null; |
+ var changeListeners = new global.ext.PageMap(); |
+ var filterListenerAdded = false; |
+ var listenedPreferences = []; |
var messageTypes = { |
"app": "app.listen", |
"filter": "filters.listen", |
@@ -60,13 +62,14 @@ |
"subscription": "subscriptions.listen" |
}; |
- function sendMessage(type, action, args, page) |
+ function sendMessage(type, action, args) |
{ |
- var pages = page ? [page] : changeListeners.keys(); |
+ var pages = changeListeners.keys(); |
for (var i = 0; i < pages.length; i++) |
{ |
var filters = changeListeners.get(pages[i]); |
- if (filters[type] && filters[type].indexOf(action) >= 0) |
+ var actions = filters[type]; |
+ if (actions && actions.indexOf(action) != -1) |
{ |
pages[i].sendMessage({ |
type: messageTypes[type], |
@@ -110,31 +113,26 @@ |
sendMessage(type, action, args); |
} |
- function onPrefChange(name) |
+ function getListenerFilters(page, addListener) |
{ |
- sendMessage("pref", name, [Prefs[name]]); |
+ if (addListener && !filterListenerAdded) |
+ { |
+ FilterNotifier.addListener(onFilterChange); |
+ filterListenerAdded = true; |
+ } |
+ |
+ var listenerFilters = changeListeners.get(page); |
+ if (!listenerFilters) |
+ { |
+ listenerFilters = Object.create(null); |
+ changeListeners.set(page, listenerFilters); |
+ } |
+ |
+ return listenerFilters; |
} |
global.ext.onMessage.addListener(function(message, sender, callback) |
{ |
- var listenerFilters = null; |
- if (/\.listen$/.test(message.type)) |
- { |
- if (!changeListeners) |
- { |
- changeListeners = new global.ext.PageMap(); |
- FilterNotifier.addListener(onFilterChange); |
- Prefs.onChanged.addListener(onPrefChange); |
- } |
- |
- listenerFilters = changeListeners.get(sender.page); |
- if (!listenerFilters) |
- { |
- listenerFilters = Object.create(null); |
- changeListeners.set(sender.page, listenerFilters); |
- } |
- } |
- |
switch (message.type) |
{ |
case "app.get": |
@@ -187,6 +185,7 @@ |
callback(null); |
break; |
case "app.listen": |
+ var listenerFilters = getListenerFilters(sender.page, false); |
if (message.filter) |
listenerFilters.app = message.filter; |
else |
@@ -287,6 +286,7 @@ |
} |
break; |
case "filters.listen": |
+ var listenerFilters = getListenerFilters(sender.page, true); |
if (message.filter) |
listenerFilters.filter = message.filter; |
else |
@@ -307,8 +307,22 @@ |
callback(Prefs[message.key]); |
break; |
case "prefs.listen": |
+ var listenerFilters = getListenerFilters(sender.page, false); |
if (message.filter) |
+ { |
+ message.filter.forEach(function(preference) |
+ { |
+ if (listenedPreferences.indexOf(preference) == -1) |
+ { |
+ listenedPreferences.push(preference); |
+ Prefs.on(preference, function() |
+ { |
+ sendMessage("pref", preference, [Prefs[preference]]); |
+ }); |
+ } |
+ }); |
listenerFilters.pref = message.filter; |
+ } |
else |
delete listenerFilters.pref; |
break; |
@@ -355,6 +369,7 @@ |
callback(subscriptions.map(convertSubscription)); |
break; |
case "subscriptions.listen": |
+ var listenerFilters = getListenerFilters(sender.page, true); |
if (message.filter) |
listenerFilters.subscription = message.filter; |
else |