Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: messageResponder.js

Issue 29338734: Issue 3839 - Adapt for Prefs event changes (Closed)
Patch Set: app.listen doesn't need a FilterNotifier listener Created March 21, 2016, 5:18 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « background.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld