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

Unified Diff: lib/prefs.js

Issue 29396582: Issue 5039 - add support of nullable non-object values in settings
Patch Set: add proper support of nullable setting values Created March 28, 2017, 2:28 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 | « include/AdblockPlus/JsEngine.h ('k') | src/FilterEngine.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/prefs.js
diff --git a/lib/prefs.js b/lib/prefs.js
index 14785c4a7f616d224030a84914e2e98ace2efad9..97c7b9072a46f0bfe30fbc618b6ab4117a4f7b48 100644
--- a/lib/prefs.js
+++ b/lib/prefs.js
@@ -46,6 +46,13 @@ let defaults = {
disable_auto_updates: false,
first_run_subscription_auto_select: true,
notifications_ignoredcategories: [],
+ allowed_connection_type: null
Oleksandr 2017/03/28 14:45:01 I think it would be better to have an empty string
sergei 2017/03/28 14:46:35 That where we started from, an empty string is a m
+};
+
+// It is for non-objects, e.g. for string, number, etc.
+let nullableValues_ExpectedTypes = {
+ __proto__: null,
+ allowed_connection_type: "string"
};
let preconfigurable = ["suppress_first_run_page", "disable_auto_updates",
@@ -57,6 +64,14 @@ let listeners = [];
let isDirty = false;
let isSaving = false;
+function isValueTypeCorrect(key, value)
+{
+ let nullableValue_ExpectedType = nullableValues_ExpectedTypes[key];
+ return !nullableValue_ExpectedType ?
+ typeof value == typeof defaults[key] :
+ value === null || typeof value == nullableValue_ExpectedType;
+}
+
function defineProperty(key)
{
Object.defineProperty(Prefs, key,
@@ -64,7 +79,7 @@ function defineProperty(key)
get: () => values[key],
set: function(value)
{
- if (typeof value != typeof defaults[key])
+ if (!isValueTypeCorrect(key, value))
throw new Error("Attempt to change preference type");
if (value == defaults[key])
@@ -141,7 +156,13 @@ let Prefs = exports.Prefs = {
// Update the default prefs with what was preconfigured
for (let key in _preconfiguredPrefs)
if (preconfigurable.indexOf(key) != -1)
- defaults[key] = _preconfiguredPrefs[key];
+ {
+ let value = _preconfiguredPrefs[key];
+ if (!isValueTypeCorrect(key, value))
+ throw new Error("Unexpected value type in preconfigured preferences");
+
+ defaults[key] = value;
+ }
// Define defaults
for (let key in defaults)
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | src/FilterEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld