Commit 1c2d4fcd authored by jlklein's avatar jlklein Committed by Commit bot

Switch prefs.js to Object.observe now that observe-js is no longer a dep.

BUG=

Review URL: https://codereview.chromium.org/1147933002

Cr-Commit-Position: refs/heads/master@{#330797}
parent 00c47bbf
...@@ -68,8 +68,8 @@ ...@@ -68,8 +68,8 @@
/** /**
* Updates the settings model with the given prefs. * Updates the settings model with the given prefs.
* @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs
* @param {boolean} shouldObserve Whether to add an ObjectObserver for each * @param {boolean} shouldObserve Whether each of the prefs should be
* of the prefs. * observed.
* @private * @private
*/ */
updatePrefs_: function(prefs, shouldObserve) { updatePrefs_: function(prefs, shouldObserve) {
...@@ -90,47 +90,39 @@ ...@@ -90,47 +90,39 @@
} }
// NOTE: Do this copy rather than just a re-assignment, so that the // NOTE: Do this copy rather than just a re-assignment, so that the
// ObjectObserver fires. // observer fires.
for (let objKey in prefObj) { for (let objKey in prefObj) {
let path = 'prefStore.' + prefObj.key + '.' + objKey; let path = 'prefStore.' + prefObj.key + '.' + objKey;
this.setPathValue(path, prefObj[objKey]); this.setPathValue(path, prefObj[objKey]);
} }
if (shouldObserve) { if (shouldObserve) {
let keyObserver = new ObjectObserver(root); Object.observe(root, this.propertyChangeCallback_, ['update']);
keyObserver.open(
this.propertyChangeCallback_.bind(this, prefObj.key));
} }
}, this); }, this);
}, },
/** /**
* Called when a property of a pref changes. * Called when a property of a pref changes.
* @param {string} propertyPath The path before the property names. * @param {!Array<!Object>} changes An array of objects describing changes.
* @param {!Array<string>} added An array of keys which were added. * @see http://www.html5rocks.com/en/tutorials/es7/observe/
* @param {!Array<string>} removed An array of keys which were removed.
* @param {!Array<string>} changed An array of keys of properties whose
* values changed.
* @param {function(string) : *} getOldValueFn A function which takes a
* property name and returns the old value for that property.
* @private * @private
*/ */
propertyChangeCallback_: function( propertyChangeCallback_: function(changes) {
propertyPath, added, removed, changed, getOldValueFn) { changes.forEach(function(change) {
for (let property in changed) {
// UI should only be able to change the value of a setting for now, not // UI should only be able to change the value of a setting for now, not
// disabled, etc. // disabled, etc.
assert(property == 'value'); assert(change.name == 'value');
let newValue = changed[property]; let newValue = change.object[change.name];
assert(newValue !== undefined); assert(newValue !== undefined);
chrome.settingsPrivate.setPref( chrome.settingsPrivate.setPref(
propertyPath, change.object['key'],
newValue, newValue,
/* pageId */ '', /* pageId */ '',
/* callback */ function() {}); /* callback */ function() {});
} });
}, },
}); });
})(); })();
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment