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

Unified Diff: new-options.js

Issue 29340967: Issue 3971 - Refactor away Object.observe (Closed)
Patch Set: Created April 29, 2016, 12:42 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: new-options.js
diff --git a/new-options.js b/new-options.js
index 9140a459ec877781f06af4caf30867bc5228c516..5dc0098e7a40d0c92ea07ae2e134fa3134553642 100644
--- a/new-options.js
+++ b/new-options.js
@@ -386,100 +386,62 @@
}
]);
- function observeSubscription(subscription)
+ function updateLanguageCollections(subscription)
{
- function onObjectChanged(change)
+ var recommendation = recommendationsMap[subscription.url];
+
+ if (recommendation && recommendation.type == "ads")
{
- for (var i = 0; i < change.length; i++)
+ if (subscription.disabled)
{
- if (change[i].name == "disabled")
- {
- var recommendation = recommendationsMap[subscription.url];
- if (recommendation && recommendation.type == "ads")
- {
- if (subscription.disabled == false)
- {
- collections.allLangs.removeItem(subscription);
- collections.langs.addItems(subscription);
- }
- else
- {
- collections.allLangs.addItems(subscription);
- collections.langs.removeItem(subscription);
- }
- }
- }
+ collections.allLangs.addItems(subscription);
+ collections.langs.removeItem(subscription);
+ }
+ else
+ {
+ collections.allLangs.removeItem(subscription);
+ collections.langs.addItems(subscription);
}
-
- for (var name in collections)
- collections[name].updateItem(subscription);
}
+ }
- if (!Object.observe)
+ function addSubscription(subscription)
+ {
+ var collection;
+ if (subscription.url in recommendationsMap)
{
- Object.keys(subscription).forEach(function(property)
- {
- var value = subscription[property];
- Object.defineProperty(subscription, property,
- {
- get: function()
- {
- return value;
- },
- set: function(newValue)
- {
- if (value != newValue)
- {
- value = newValue;
- onObjectChanged([{name: property}]);
- }
- }
- });
- });
+ var recommendation = recommendationsMap[subscription.url];
+ if (recommendation.type != "ads")
+ collection = collections.popular;
+ else if (subscription.disabled == false)
+ collection = collections.langs;
+ else
+ collection = collections.allLangs;
}
+ else if (subscription.url == acceptableAdsUrl)
+ collection = collections.acceptableAds;
else
- {
- Object.observe(subscription, onObjectChanged);
- }
+ collection = collections.custom;
+
+ collection.addItems(subscription);
+ subscriptionsMap[subscription.url] = subscription;
}
function updateSubscription(subscription)
{
- var subscriptionUrl = subscription.url;
- var knownSubscription = subscriptionsMap[subscriptionUrl];
- if (knownSubscription)
+ var knownSubscription = subscriptionsMap[subscription.url];
+ for (var property in subscription)
{
- for (var property in subscription)
- {
- if (property == "title" && subscriptionUrl in recommendationsMap)
- knownSubscription.originalTitle = subscription.title;
- else
- knownSubscription[property] = subscription[property];
- }
+ if (property == "title" && subscription.url in recommendationsMap)
+ knownSubscription.originalTitle = subscription.title;
+ else
+ knownSubscription[property] = subscription[property];
}
- else
- {
- observeSubscription(subscription);
- var collection;
- if (subscriptionUrl in recommendationsMap)
- {
- var recommendation = recommendationsMap[subscriptionUrl];
- if (recommendation.type != "ads")
- collection = collections.popular;
- else if (subscription.disabled == false)
- collection = collections.langs;
- else
- collection = collections.allLangs;
- }
- else if (subscriptionUrl == acceptableAdsUrl)
- collection = collections.acceptableAds;
- else
- collection = collections.custom;
+ for (var name in collections)
+ collections[name].updateItem(knownSubscription);
- collection.addItems(subscription);
- subscriptionsMap[subscriptionUrl] = subscription;
- }
+ return knownSubscription;
}
function updateFilter(filter)
@@ -532,7 +494,7 @@
}
recommendationsMap[subscription.url] = recommendation;
- updateSubscription(subscription);
+ addSubscription(subscription);
}
});
}
@@ -875,7 +837,7 @@
function(url)
{
acceptableAdsUrl = url;
- updateSubscription({
+ addSubscription({
url: acceptableAdsUrl,
disabled: true
});
@@ -965,44 +927,45 @@
{
switch (action)
{
- case "added":
- updateSubscription(subscription);
- updateShareLink();
-
- var knownSubscription = subscriptionsMap[subscription.url];
- if (knownSubscription)
- collections.filterLists.addItems(knownSubscription);
- else
- collections.filterLists.addItems(subscription);
- break;
case "disabled":
+ subscription = updateSubscription(subscription);
+ updateLanguageCollections(subscription);
+ break;
+ case "downloading":
+ case "downloadStatus":
+ case "homepage":
+ case "lastDownload":
+ case "title":
updateSubscription(subscription);
- updateShareLink();
+ break;
+ case "added":
+ if (subscription.url in subscriptionsMap)
+ subscription = updateSubscription(subscription);
+ else
+ addSubscription(subscription);
+
+ collections.filterLists.addItems(subscription);
+ updateLanguageCollections(subscription);
break;
case "removed":
var knownSubscription = subscriptionsMap[subscription.url];
- if (subscription.url == acceptableAdsUrl)
+
+ if (subscription.url == acceptableAdsUrl ||
+ subscription.url in recommendationsMap)
{
subscription.disabled = true;
- updateSubscription(subscription);
+ onSubscriptionMessage("disabled", subscription);
}
else
{
- if (subscription.url in recommendationsMap)
- knownSubscription.disabled = true;
- else
- {
- collections.custom.removeItem(knownSubscription);
- delete subscriptionsMap[subscription.url];
- }
+ collections.custom.removeItem(knownSubscription);
+ delete subscriptionsMap[subscription.url];
}
- updateShareLink();
collections.filterLists.removeItem(knownSubscription);
break;
- default:
- updateSubscription(subscription);
- break;
}
+
+ updateShareLink();
}
function hidePref(key, value)
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld