Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: chrome/browser/resources/chromeos/login/screen_context.js

Issue 328763003: Added usefull utility methods to Screen and ScreenContext. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @fileoverview Implementation of ScreenContext class: key-value storage for 6 * @fileoverview Implementation of ScreenContext class: key-value storage for
7 * values that are shared between C++ and JS sides. 7 * values that are shared between C++ and JS sides.
8 */ 8 */
9 cr.define('login', function() { 9 cr.define('login', function() {
10 'use strict';
11
10 function require(condition, message) { 12 function require(condition, message) {
11 if (!condition) { 13 if (!condition) {
12 throw Error(message); 14 throw Error(message);
13 } 15 }
14 } 16 }
15 17
16 function checkKeyIsValid(key) { 18 function checkKeyIsValid(key) {
17 var keyType = typeof key; 19 var keyType = typeof key;
18 require(keyType === 'string', 'Invalid type of key: "' + keyType + '".'); 20 require(keyType === 'string', 'Invalid type of key: "' + keyType + '".');
19 } 21 }
20 22
21 function checkValueIsValid(value) { 23 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.
22 var valueType = typeof value; 24 var valueType = typeof value;
23 require(['string', 'boolean', 'number'].indexOf(valueType) != -1, 25 require(['string', 'boolean', 'number'].indexOf(valueType) != -1,
24 'Invalid type of value: "' + valueType + '".'); 26 'Invalid type of value ' + t + ': "' + valueType + '".');
25 } 27 }
26 28
27 function ScreenContext() { 29 function ScreenContext(screen) {
30 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.
28 this.storage_ = {}; 31 this.storage_ = {};
29 this.changes_ = {}; 32 this.changes_ = {};
33 this.observers_ = {};
30 } 34 }
31 35
32 ScreenContext.prototype = { 36 ScreenContext.prototype = {
33 /** 37 /**
34 * Returns stored value for |key| or |defaultValue| if key not found in 38 * Returns stored value for |key| or |defaultValue| if key not found in
35 * storage. Throws Error if key not found and |defaultValue| omitted. 39 * storage. Throws Error if key not found and |defaultValue| omitted.
36 */ 40 */
37 get: function(key, defaultValue) { 41 get: function(key, defaultValue) {
38 checkKeyIsValid(key); 42 checkKeyIsValid(key);
39 if (this.hasKey(key)) { 43 if (this.hasKey(key)) {
40 return this.storage_[key]; 44 return this.storage_[key];
41 } else if (typeof defaultValue !== 'undefined') { 45 } else if (typeof defaultValue !== 'undefined') {
42 return defaultValue; 46 return defaultValue;
43 } else { 47 } else {
44 throw Error('Key "' + key + '" not found.'); 48 throw Error('Key "' + key + '" not found.');
45 } 49 }
46 }, 50 },
47 51
48 /** 52 /**
49 * Sets |value| for |key|. Returns true if call changes state of context, 53 * Sets |value| for |key|. Returns true if call changes state of context,
50 * false otherwise. 54 * false otherwise.
51 */ 55 */
52 set: function(key, value) { 56 set: function(key, value) {
53 checkKeyIsValid(key); 57 checkKeyIsValid(key);
54 checkValueIsValid(value); 58 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.
55 if (this.hasKey(key) && this.storage_[key] === value) 59 if (this.hasKey(key) && this.storage_[key] === value)
56 return false; 60 return false;
57 this.changes_[key] = value; 61 this.changes_[key] = value;
58 this.storage_[key] = value; 62 this.storage_[key] = value;
59 return true; 63 return true;
60 }, 64 },
61 65
62 hasKey: function(key) { 66 hasKey: function(key) {
63 checkKeyIsValid(key); 67 checkKeyIsValid(key);
64 return this.storage_.hasOwnProperty(key); 68 return this.storage_.hasOwnProperty(key);
65 }, 69 },
66 70
67 hasChanges: function() { 71 hasChanges: function() {
68 return Object.keys(this.changes_).length > 0; 72 return Object.keys(this.changes_).length > 0;
69 }, 73 },
70 74
71 /** 75 /**
72 * Applies |changes| to context. Returns Array of changed keys' names. 76 * Applies |changes| to context. Returns Array of changed keys' names.
73 */ 77 */
74 applyChanges: function(changes) { 78 applyChanges: function(changes) {
75 require(!this.hasChanges(), 'Context has changes.'); 79 require(!this.hasChanges(), 'Context has changes.');
76 Object.keys(changes).forEach(function(key) { 80 var oldValues = {};
81 for (var key in changes) {
77 checkKeyIsValid(key); 82 checkKeyIsValid(key);
78 checkValueIsValid(changes[key]); 83 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.
84 oldValues[key] = this.storage_[key];
79 this.storage_[key] = changes[key]; 85 this.storage_[key] = changes[key];
80 }, this); 86 }
87 var observers = this.cloneObservers_();
88 for (var key in changes) {
89 if (observers.hasOwnProperty(key)) {
90 var keyObservers = observers[key];
91 for (var observerIndex in keyObservers)
92 keyObservers[observerIndex](changes[key], oldValues[key], key);
93 }
94 }
81 return Object.keys(changes); 95 return Object.keys(changes);
82 }, 96 },
83 97
84 /** 98 /**
85 * Returns changes made on context since previous call. 99 * Returns changes made on context since previous call.
86 */ 100 */
87 getChangesAndReset: function() { 101 getChangesAndReset: function() {
88 var result = this.changes_; 102 var result = this.changes_;
89 this.changes_ = {}; 103 this.changes_ = {};
90 return result; 104 return result;
91 } 105 },
106
107 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.
108 if (!this.observers_.hasOwnProperty(key))
109 this.observers_[key] = [];
110 if (this.observers_[key].indexOf(observer) !== -1) {
111 console.warn('Observer already registered.');
112 return;
113 }
114 this.observers_[key].push(observer);
115 },
116
117 unobserve: function(observer) {
118 for (var key in this.observers_) {
119 var observerIndex = this.observers_[key].indexOf(observer);
120 if (observerIndex != -1)
121 this.observers_[key].splice(observerIndex, 1);
122 }
123 },
124
125 cloneObservers_: function() {
126 var result = {};
127 for (var key in this.observers_)
128 result[key] = this.observers_[key].slice();
129 return result;
130 },
131
ygorshenin1 2014/06/11 10:44:00 nit: drop empty line and trailing comma.
dzhioev (left Google) 2014/06/18 14:43:39 Done.
92 }; 132 };
93 133
94 return { 134 return {
95 ScreenContext: ScreenContext 135 ScreenContext: ScreenContext
96 }; 136 };
97 }); 137 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698