Index: chrome/browser/resources/chromeos/login/screen_context.js |
diff --git a/chrome/browser/resources/chromeos/login/screen_context.js b/chrome/browser/resources/chromeos/login/screen_context.js |
index 6a014a4828365e61576db7ad3f07c5982af7ed6c..f7460f4de08746237eed8dd0da57f62c0e622283 100644 |
--- a/chrome/browser/resources/chromeos/login/screen_context.js |
+++ b/chrome/browser/resources/chromeos/login/screen_context.js |
@@ -7,6 +7,8 @@ |
* values that are shared between C++ and JS sides. |
*/ |
cr.define('login', function() { |
+ 'use strict'; |
+ |
function require(condition, message) { |
if (!condition) { |
throw Error(message); |
@@ -18,15 +20,17 @@ cr.define('login', function() { |
require(keyType === 'string', 'Invalid type of key: "' + keyType + '".'); |
} |
- function checkValueIsValid(value) { |
+ function checkValueIsValid(value, t) { |
ygorshenin1
2014/06/11 10:44:00
nit: from the function definition and usage it's n
Denis Kuznetsov (DE-MUC)
2014/06/11 14:29:17
Also, guide recommends to avoid abbreviations for
dzhioev (left Google)
2014/06/18 14:43:39
It is a garbage that I forgot to remove.
|
var valueType = typeof value; |
require(['string', 'boolean', 'number'].indexOf(valueType) != -1, |
- 'Invalid type of value: "' + valueType + '".'); |
+ 'Invalid type of value ' + t + ': "' + valueType + '".'); |
} |
- function ScreenContext() { |
+ function ScreenContext(screen) { |
+ this.screen_ = screen; |
ygorshenin1
2014/06/11 10:44:00
Cound you please point where |screen_| is used?
dzhioev (left Google)
2014/06/18 14:43:39
It is not used anymore. I forgot to remove it.
|
this.storage_ = {}; |
this.changes_ = {}; |
+ this.observers_ = {}; |
} |
ScreenContext.prototype = { |
@@ -51,7 +55,7 @@ cr.define('login', function() { |
*/ |
set: function(key, value) { |
checkKeyIsValid(key); |
- checkValueIsValid(value); |
+ checkValueIsValid(value, 2); |
Denis Kuznetsov (DE-MUC)
2014/06/11 14:29:17
It is not obvious what "2" means.
Could you use so
dzhioev (left Google)
2014/06/18 14:43:39
Removed.
|
if (this.hasKey(key) && this.storage_[key] === value) |
return false; |
this.changes_[key] = value; |
@@ -73,11 +77,21 @@ cr.define('login', function() { |
*/ |
applyChanges: function(changes) { |
require(!this.hasChanges(), 'Context has changes.'); |
- Object.keys(changes).forEach(function(key) { |
+ var oldValues = {}; |
+ for (var key in changes) { |
checkKeyIsValid(key); |
- checkValueIsValid(changes[key]); |
+ checkValueIsValid(changes[key], 1); |
Denis Kuznetsov (DE-MUC)
2014/06/11 14:29:17
What is the "1"?
dzhioev (left Google)
2014/06/18 14:43:39
Removed.
|
+ oldValues[key] = this.storage_[key]; |
this.storage_[key] = changes[key]; |
- }, this); |
+ } |
+ var observers = this.cloneObservers_(); |
+ for (var key in changes) { |
+ if (observers.hasOwnProperty(key)) { |
+ var keyObservers = observers[key]; |
+ for (var observerIndex in keyObservers) |
+ keyObservers[observerIndex](changes[key], oldValues[key], key); |
+ } |
+ } |
return Object.keys(changes); |
}, |
@@ -88,7 +102,33 @@ cr.define('login', function() { |
var result = this.changes_; |
this.changes_ = {}; |
return result; |
- } |
+ }, |
+ |
+ observe: function(key, observer) { |
Denis Kuznetsov (DE-MUC)
2014/06/11 14:29:17
subscribe/unsubscribe?
"Observe" is the local acti
dzhioev (left Google)
2014/06/18 14:43:39
I used same wording as new JS API use ( http://www
Denis Kuznetsov (DE-MUC)
2014/06/19 19:16:36
Then it's even more relevant : we need to avoid na
dzhioev (left Google)
2014/06/20 07:24:16
OK, renamed to familiar (add/remove)Observer.
|
+ if (!this.observers_.hasOwnProperty(key)) |
+ this.observers_[key] = []; |
+ if (this.observers_[key].indexOf(observer) !== -1) { |
+ console.warn('Observer already registered.'); |
+ return; |
+ } |
+ this.observers_[key].push(observer); |
+ }, |
+ |
+ unobserve: function(observer) { |
+ for (var key in this.observers_) { |
+ var observerIndex = this.observers_[key].indexOf(observer); |
+ if (observerIndex != -1) |
+ this.observers_[key].splice(observerIndex, 1); |
+ } |
+ }, |
+ |
+ cloneObservers_: function() { |
+ var result = {}; |
+ for (var key in this.observers_) |
+ result[key] = this.observers_[key].slice(); |
+ return result; |
+ }, |
+ |
ygorshenin1
2014/06/11 10:44:00
nit: drop empty line and trailing comma.
dzhioev (left Google)
2014/06/18 14:43:39
Done.
|
}; |
return { |