You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by jo...@apache.org on 2009/08/18 02:59:28 UTC
svn commit: r805239 - in /incubator/shindig/trunk/features/src:
main/javascript/features/core/prefs.js
main/javascript/features/setprefs/setprefs.js
test/javascript/features/core/prefstest.js
test/javascript/features/setprefs/setprefstest.js
Author: johnh
Date: Tue Aug 18 00:59:28 2009
New Revision: 805239
URL: http://svn.apache.org/viewvc?rev=805239&view=rev
Log:
Only call gadgets.rpc method to set prefs when the prefs have actually changed. Thanks to Shuo-Peng Liao for the patch! This resolves issue SHINDIG-1154.
Modified:
incubator/shindig/trunk/features/src/main/javascript/features/core/prefs.js
incubator/shindig/trunk/features/src/main/javascript/features/setprefs/setprefs.js
incubator/shindig/trunk/features/src/test/javascript/features/core/prefstest.js
incubator/shindig/trunk/features/src/test/javascript/features/setprefs/setprefstest.js
Modified: incubator/shindig/trunk/features/src/main/javascript/features/core/prefs.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/core/prefs.js?rev=805239&r1=805238&r2=805239&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/core/prefs.js (original)
+++ incubator/shindig/trunk/features/src/main/javascript/features/core/prefs.js Tue Aug 18 00:59:28 2009
@@ -115,17 +115,27 @@
/**
* Sets internal values
+ * @return {Boolean} True if the prefs is modified.
*/
gadgets.Prefs.setInternal_ = function(key, value) {
+ var wasModified = false;
if (typeof key === "string") {
+ if (!prefs.hasOwnProperty(key) || prefs[key] !== value) {
+ wasModified = true;
+ }
prefs[key] = value;
} else {
for (var k in key) {
if (key.hasOwnProperty(k)) {
- prefs[k] = key[k];
+ var v = key[k];
+ if (!prefs.hasOwnProperty(k) || prefs[k] !== v) {
+ wasModified = true;
+ }
+ prefs[k] = v;
}
}
}
+ return wasModified;
};
/**
Modified: incubator/shindig/trunk/features/src/main/javascript/features/setprefs/setprefs.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/setprefs/setprefs.js?rev=805239&r1=805238&r2=805239&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/setprefs/setprefs.js (original)
+++ incubator/shindig/trunk/features/src/main/javascript/features/setprefs/setprefs.js Tue Aug 18 00:59:28 2009
@@ -30,6 +30,7 @@
* @private This feature is documented in prefs.js
*/
gadgets.Prefs.prototype.set = function(key, value) {
+ var needUpdate = false;
if (arguments.length > 2) {
// For backwards compatibility. This can take the form:
// prefs.set(key0, value0, key1, value1, key2, value2);
@@ -39,9 +40,12 @@
for (var i = 0, j = arguments.length; i < j; i += 2) {
obj[arguments[i]] = arguments[i + 1];
}
- gadgets.Prefs.setInternal_(obj);
+ needUpdate = gadgets.Prefs.setInternal_(obj);
} else {
- gadgets.Prefs.setInternal_(key, value);
+ needUpdate = gadgets.Prefs.setInternal_(key, value);
+ }
+ if (!needUpdate) {
+ return;
}
var args = [
Modified: incubator/shindig/trunk/features/src/test/javascript/features/core/prefstest.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/test/javascript/features/core/prefstest.js?rev=805239&r1=805238&r2=805239&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/test/javascript/features/core/prefstest.js (original)
+++ incubator/shindig/trunk/features/src/test/javascript/features/core/prefstest.js Tue Aug 18 00:59:28 2009
@@ -40,6 +40,22 @@
this.params = undefined;
};
+PrefsTest.prototype.testSetInternal_ = function() {
+ var pref = new gadgets.Prefs();
+
+ // First time set the prefs, expect returning true.
+ this.assertTrue(gadgets.Prefs.setInternal_(this.params, 100));
+ this.assertEquals(true, pref.getBool('boolString'));
+
+ // Set the same prefs, expect returning false.
+ this.assertFalse(gadgets.Prefs.setInternal_(this.params, 100));
+ this.assertEquals(true, pref.getBool('boolString'));
+
+ // Modify 'boolString' value, expect returning true.
+ this.assertTrue(gadgets.Prefs.setInternal_('boolString', false));
+ this.assertEquals(false, pref.getBool('boolString'));
+};
+
PrefsTest.prototype.testGetInt = function() {
var expectedResults = {myCounter: 100, myString: 15, myUndefined: 0,
myObject: 0, myFloat: 3};
Modified: incubator/shindig/trunk/features/src/test/javascript/features/setprefs/setprefstest.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/test/javascript/features/setprefs/setprefstest.js?rev=805239&r1=805238&r2=805239&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/test/javascript/features/setprefs/setprefstest.js (original)
+++ incubator/shindig/trunk/features/src/test/javascript/features/setprefs/setprefstest.js Tue Aug 18 00:59:28 2009
@@ -35,7 +35,7 @@
gadgets.rpc = {};
gadgets.rpc.call = function() {
that.rpcArguments = Array.prototype.slice.call(arguments);
- }
+ };
};
SetPrefsTest.prototype.tearDown = function() {
@@ -46,10 +46,22 @@
SetPrefsTest.prototype.testSet = function() {
var pref = new gadgets.Prefs();
gadgets.Prefs.setInternal_('key', 100);
+
+ // Clear the RPC call history before pref.set() is called.
+ this.resetRpc();
+
+ // The same group of values, should not invoke a RPC call.
+ pref.set('key', 100);
+ this.assertNoRpcCalled();
+
+ // Value altered, should invoke a RPC call.
pref.set('key', 200);
this.assertEquals(200, pref.getInt('key'));
-
this.assertRpcCalled(null, 'set_pref', null, 0, 'key', 200);
+
+ // Set the same value again, should not invoke a RPC call.
+ pref.set('key', 200);
+ this.assertNoRpcCalled();
};
SetPrefsTest.prototype.testSetArray = function() {
@@ -85,6 +97,10 @@
this.assertRpcCalled(null, 'set_pref', null, 0, 'array', '1|2');
};
+/**
+ * Asserts gadgets.rpc.call() is called with the expected arguments given.
+ * Note that it resets this.rpcArguments for next RPC call assertion.
+ */
SetPrefsTest.prototype.assertRpcCalled = function() {
this.assertNotUndefined("RPC was not called.", this.rpcArguments);
this.assertEquals("RPC argument list not valid length.",
@@ -93,5 +109,19 @@
for (var i = 0; i < arguments.length; i++) {
this.assertEquals(arguments[i], this.rpcArguments[i]);
}
+ this.resetRpc();
};
+/**
+ * Resets this.rpcArguments.
+ */
+SetPrefsTest.prototype.resetRpc = function() {
+ this.rpcArguments = undefined;
+};
+
+/**
+ * Asserts that no gadgets.rpc.call() is called.
+ */
+SetPrefsTest.prototype.assertNoRpcCalled = function() {
+ this.assertUndefined("RPC was called.", this.rpcArguments);
+};