You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2012/10/26 22:10:36 UTC

js commit: [android] Fix synch exec return values when made recursively.

Updated Branches:
  refs/heads/master 97a05cb0f -> d819e1e60
Updated Tags:  refs/tags/2.2.0rc2 97a05cb0f -> d819e1e60


[android] Fix synch exec return values when made recursively.

For an explanation of the case, refer to the added unit test.
Related to: https://issues.apache.org/jira/browse/CB-1745


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/d819e1e6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/d819e1e6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/d819e1e6

Branch: refs/heads/master
Commit: d819e1e60599c12ac831379b06bba9110e9f1790
Parents: 97a05cb
Author: Andrew Grieve <ag...@chromium.org>
Authored: Fri Oct 26 16:00:55 2012 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Fri Oct 26 16:00:55 2012 -0400

----------------------------------------------------------------------
 lib/android/exec.js       |   35 ++++++++++++++++++++++-------------
 test/android/test.exec.js |   16 ++++++++++++++++
 2 files changed, 38 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/d819e1e6/lib/android/exec.js
----------------------------------------------------------------------
diff --git a/lib/android/exec.js b/lib/android/exec.js
index c339e2c..490633f 100644
--- a/lib/android/exec.js
+++ b/lib/android/exec.js
@@ -105,8 +105,8 @@ function pollOnce() {
 
 function pollingTimerFunc() {
     if (pollEnabled) {
-      pollOnce();
-      setTimeout(pollingTimerFunc, 50);
+        pollOnce();
+        setTimeout(pollingTimerFunc, 50);
     }
 }
 
@@ -199,25 +199,34 @@ function processMessage(message) {
 
 // This is called from the NativeToJsMessageQueue.java.
 androidExec.processMessages = function(messages) {
-    messagesFromNative.push(messages);
-    // Check for the reentrant case, and enqueue the message if that's the case.
-    if (messagesFromNative.length > 1) {
-        return;
-    }
-    while (messagesFromNative.length) {
-        messages = messagesFromNative[0];
-        while (messages) {
+    if (messages) {
+        messagesFromNative.push(messages);
+        while (messagesFromNative.length) {
+            messages = messagesFromNative.shift();
+            // The Java side can send a * message to indicate that it
+            // still has messages waiting to be retrieved.
+            // TODO(agrieve): This is currently disabled on the Java side
+            // since it breaks returning the result in exec of synchronous
+            // plugins. Once we remove this ability, we can remove this comment.
             if (messages == '*') {
                 window.setTimeout(pollOnce, 0);
-                break;
+                continue;
             }
+
             var spaceIdx = messages.indexOf(' ');
             var msgLen = +messages.slice(0, spaceIdx);
             var message = messages.substr(spaceIdx + 1, msgLen);
             messages = messages.slice(spaceIdx + msgLen + 1);
-            processMessage(message);
+            // Put the remaining messages back into queue in case an exec()
+            // is made by the callback.
+            if (messages) {
+                messagesFromNative.unshift(messages);
+            }
+
+            if (message) {
+                processMessage(message);
+            }
         }
-        messagesFromNative.shift();
     }
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/d819e1e6/test/android/test.exec.js
----------------------------------------------------------------------
diff --git a/test/android/test.exec.js b/test/android/test.exec.js
index f4e760f..3363e45 100644
--- a/test/android/test.exec.js
+++ b/test/android/test.exec.js
@@ -74,6 +74,22 @@ describe('exec.processMessages', function () {
             var result = exec(null, null, 'Service', 'action', []);
             expect(result).toBe(true);
         });
+        it('should return payload value when plugin is synchronous even when called recursively', function() {
+            var winSpy = jasmine.createSpy('win');
+            nativeApi.exec.andCallFake(function(service, action, callbackId, argsJson) {
+                return createCallbackMessage(true, true, 1, callbackId, 't');
+            });
+
+            function firstWin(value) {
+                expect(value).toBe(true);
+                var result = exec(winSpy, null, 'Service', 'action', []);
+                expect(result).toBe(true);
+            }
+
+            var result2 = exec(firstWin, null, 'Service', 'action', []);
+            expect(winSpy).toHaveBeenCalledWith(true);
+            expect(result2).toBe(true);
+        });
     });
 
     describe('processMessages', function() {