cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [01/11] git commit: update to fix concurrent api calls issue
Date Thu, 09 May 2013 06:14:52 GMT
Updated Branches:
  refs/heads/master aee42fae0 -> 9f0d74c12


update to fix concurrent api calls issue


Project: http://git-wip-us.apache.org/repos/asf/cordova-wp8/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-wp8/commit/9b532e67
Tree: http://git-wip-us.apache.org/repos/asf/cordova-wp8/tree/9b532e67
Diff: http://git-wip-us.apache.org/repos/asf/cordova-wp8/diff/9b532e67

Branch: refs/heads/master
Commit: 9b532e6757b750949a1c9de13b14af9b51415adf
Parents: aee42fa
Author: Jesse MacFadyen <purplecabbage@gmail.com>
Authored: Tue Apr 30 13:56:34 2013 -0700
Committer: Jesse MacFadyen <purplecabbage@gmail.com>
Committed: Tue Apr 30 13:56:34 2013 -0700

----------------------------------------------------------------------
 .../standalone/cordovalib/Commands/BaseCommand.cs  |   67 ++++++++++-----
 templates/standalone/cordovalib/ConfigHandler.cs   |   15 ++--
 .../standalone/cordovalib/CordovaCommandCall.cs    |   15 +++-
 templates/standalone/cordovalib/NativeExecution.cs |   28 ++++--
 templates/standalone/cordovalib/PluginResult.cs    |    2 +
 5 files changed, 88 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/9b532e67/templates/standalone/cordovalib/Commands/BaseCommand.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/Commands/BaseCommand.cs b/templates/standalone/cordovalib/Commands/BaseCommand.cs
index 43f5d90..c390047 100644
--- a/templates/standalone/cordovalib/Commands/BaseCommand.cs
+++ b/templates/standalone/cordovalib/Commands/BaseCommand.cs
@@ -16,6 +16,8 @@ using System;
 using System.Reflection;
 using Microsoft.Phone.Shell;
 using System.Diagnostics;
+using System.Collections;
+using System.Collections.Generic;
 
 namespace WPCordovaClassLib.Cordova.Commands
 {
@@ -30,17 +32,37 @@ namespace WPCordovaClassLib.Cordova.Commands
 
         public event EventHandler<ScriptCallback> OnCustomScript;
 
+        public string CurrentCommandCallbackId { get; set; }
+
         public BaseCommand()
         {
+            ResultHandlers = new Dictionary<string, EventHandler<PluginResult>>();
             PhoneApplicationService service = PhoneApplicationService.Current;
             service.Activated += this.OnResume;
             service.Deactivated += this.OnPause;
         }
 
+        protected Dictionary<string, EventHandler<PluginResult>> ResultHandlers;
+        public void AddResultHandler(string callbackId, EventHandler<PluginResult>
handler)
+        {
+            ResultHandlers.Add(callbackId, handler);
+        }
+        public bool RemoveResultHandler(string callbackId)
+        {
+            return ResultHandlers.Remove(callbackId);
+        }
+
         /*
          *  InvokeMethodNamed will call the named method of a BaseCommand subclass if it
exists and pass the variable arguments list along.
          **/
 
+        public object InvokeMethodNamed(string callbackId, string methodName, params object[]
args)
+        {
+            Debug.WriteLine(string.Format("InvokeMethodNamed:{0} callbackId:{1}",methodName,callbackId));
+            this.CurrentCommandCallbackId = callbackId;
+            return InvokeMethodNamed(methodName, args);
+        }
+
         public object InvokeMethodNamed(string methodName, params object[] args)
         {
             MethodInfo mInfo = this.GetType().GetMethod(methodName);
@@ -58,7 +80,6 @@ namespace WPCordovaClassLib.Cordova.Commands
                 PropertyInfo pInfo = this.GetType().GetProperty(methodName);
                 if (pInfo != null)
                 {
-
                     object res = pInfo.GetValue(this, null);
 
                     DispatchCommandResult(new PluginResult(PluginResult.Status.OK, res));
@@ -89,51 +110,55 @@ namespace WPCordovaClassLib.Cordova.Commands
             this.DispatchCommandResult(new PluginResult(PluginResult.Status.NO_RESULT));
         }
 
-        public void DispatchCommandResult(PluginResult result)
+        public void DispatchCommandResult(PluginResult result,string callbackId="")
         {
-            if (this.OnCommandResult != null)
+            if (!string.IsNullOrEmpty(callbackId)) 
+            {
+                result.CallbackId = callbackId;
+            }
+            else
             {
-                this.OnCommandResult(this, result);
+                result.CallbackId = this.CurrentCommandCallbackId;
+            }
 
-                if (!result.KeepCallback)
-                {
-                    this.Dispose();
-                }
+            if (ResultHandlers.ContainsKey(result.CallbackId))
+            {
+                ResultHandlers[result.CallbackId](this, result);
+            }
+            else if (this.OnCommandResult != null)
+            {
+                OnCommandResult(this, result);
+            }
 
+            if (!result.KeepCallback)
+            {
+                this.Dispose();
             }
+
         }
 
 
         /// <summary>
         /// Occurs when the application is being deactivated.
         /// </summary>        
-        public virtual void OnReset()
-        {
-        }
+        public virtual void OnReset() {}
 
         /// <summary>
         /// Occurs when the application is being loaded, and the config.xml has an autoload
entry
         /// </summary>    
-        public virtual void OnInit()
-        {
-
-        }
+        public virtual void OnInit() {}
 
 
         /// <summary>
         /// Occurs when the application is being deactivated.
         /// </summary>        
-        public virtual void OnPause(object sender, DeactivatedEventArgs e)
-        {
-        }
+        public virtual void OnPause(object sender, DeactivatedEventArgs e) {}
 
         /// <summary>
         /// Occurs when the application is being made active after previously being put
         /// into a dormant state or tombstoned.
         /// </summary>        
-        public virtual void OnResume(object sender, Microsoft.Phone.Shell.ActivatedEventArgs
e)
-        {
-        }
+        public virtual void OnResume(object sender, Microsoft.Phone.Shell.ActivatedEventArgs
e) {}
 
         public void Dispose()
         {

http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/9b532e67/templates/standalone/cordovalib/ConfigHandler.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/ConfigHandler.cs b/templates/standalone/cordovalib/ConfigHandler.cs
index 0684f6a..06806d3 100644
--- a/templates/standalone/cordovalib/ConfigHandler.cs
+++ b/templates/standalone/cordovalib/ConfigHandler.cs
@@ -44,7 +44,7 @@ namespace WPCordovaClassLib.CordovaLib
             return Preferences[key];
         }
 
-        protected static string[] AllowedSchemes = {"http","https","ftp","ftps"};
+        protected static string[] AllowedSchemes = { "http", "https", "ftp", "ftps" };
         protected bool SchemeIsAllowed(string scheme)
         {
             return AllowedSchemes.Contains(scheme);
@@ -110,14 +110,14 @@ namespace WPCordovaClassLib.CordovaLib
         {
             // Debug.WriteLine("Testing URLIsAllowed : " + url);
             // easy case first
-            if (this.AllowAllDomains )
+            if (this.AllowAllDomains)
             {
                 return true;
             }
             else
             {
                 // start simple
-                Uri uri = new Uri(url,UriKind.RelativeOrAbsolute);
+                Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
                 if (uri.IsAbsoluteUri)
                 {
                     if (this.SchemeIsAllowed(uri.Scheme))
@@ -134,7 +134,7 @@ namespace WPCordovaClassLib.CordovaLib
                             {
                                 // make sure it is at the start, and not part of the query
string
                                 // special case :: http://some.other.domain/page.html?x=1&g=http://build.apache.org/
-                                if ( Regex.IsMatch(uri.Scheme + "://" +  uri.Host + "/",
pattern) ||
+                                if (Regex.IsMatch(uri.Scheme + "://" + uri.Host + "/", pattern)
||
                                      (!Regex.IsMatch(uri.PathAndQuery, pattern)))
                                 {
                                     return true;
@@ -156,13 +156,14 @@ namespace WPCordovaClassLib.CordovaLib
             return AllowAllPlugins || AllowedPlugins.Keys.Contains(key);
         }
 
-        public string[] AutoloadPlugins {
+        public string[] AutoloadPlugins
+        {
             get
             {
                 var res = from results in AllowedPlugins.TakeWhile(p => p.Value.isAutoLoad)
-                          select results.Value.Name ;
+                          select results.Value.Name;
 
-                foreach(var s in res)
+                foreach (var s in res)
                 {
                     Debug.WriteLine(s);
                 }

http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/9b532e67/templates/standalone/cordovalib/CordovaCommandCall.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/CordovaCommandCall.cs b/templates/standalone/cordovalib/CordovaCommandCall.cs
index 084fd2c..a73ac68 100644
--- a/templates/standalone/cordovalib/CordovaCommandCall.cs
+++ b/templates/standalone/cordovalib/CordovaCommandCall.cs
@@ -56,12 +56,23 @@ namespace WPCordovaClassLib.Cordova
             }
 
             CordovaCommandCall commandCallParameters = new CordovaCommandCall();
-
             commandCallParameters.Service = split[0];
             commandCallParameters.Action = split[1];
             commandCallParameters.CallbackId = split[2];
-            commandCallParameters.Args = split.Length <= 3 ? String.Empty : String.Join("/",
split.Skip(3));
 
+
+            try
+            {
+                string arg = split.Length <= 3 ? String.Empty : String.Join("/", split.Skip(3));
+                //string[] _args = JSON.JsonHelper.Deserialize<string[]>(arg);
+                System.Collections.Generic.List<string> args = JSON.JsonHelper.Deserialize<System.Collections.Generic.List<string>>(arg);
+                args.Add(commandCallParameters.CallbackId);
+                commandCallParameters.Args = JSON.JsonHelper.Serialize(args.ToArray());
+            }
+            catch (Exception)
+            {
+                return null; 
+            }
             // sanity check for illegal names
             // was failing with ::
             // CordovaCommandResult :: 1, Device1, {"status":1,"message":"{\"name\":\"XD.....

http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/9b532e67/templates/standalone/cordovalib/NativeExecution.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/NativeExecution.cs b/templates/standalone/cordovalib/NativeExecution.cs
index 8d56225..af6b207 100644
--- a/templates/standalone/cordovalib/NativeExecution.cs
+++ b/templates/standalone/cordovalib/NativeExecution.cs
@@ -95,36 +95,40 @@ namespace WPCordovaClassLib.Cordova
 
                 EventHandler<PluginResult> OnCommandResultHandler = delegate(object
o, PluginResult res)
                 {
-                    this.OnCommandResult(commandCallParams.CallbackId, res);
+                    if (res.CallbackId == null || res.CallbackId == commandCallParams.CallbackId)
+                    {
+                        this.OnCommandResult(commandCallParams.CallbackId, res);
+                        if (!res.KeepCallback)
+                        {
+                            bc.RemoveResultHandler(commandCallParams.CallbackId);
+                        }
+                    }
                 };
 
-                bc.OnCommandResult += OnCommandResultHandler;
+                //bc.OnCommandResult += OnCommandResultHandler;
+                bc.AddResultHandler(commandCallParams.CallbackId, OnCommandResultHandler);
 
                 EventHandler<ScriptCallback> OnCustomScriptHandler = delegate(object
o, ScriptCallback script)
                 {
                     this.InvokeScriptCallback(script);
                 };
 
-
                 bc.OnCustomScript += OnCustomScriptHandler;
 
                 ThreadStart methodInvokation = () =>
                 {
-
                     try
                     {
-                        bc.InvokeMethodNamed(commandCallParams.Action, commandCallParams.Args);
+                        bc.InvokeMethodNamed(commandCallParams.CallbackId,commandCallParams.Action,
commandCallParams.Args);
                     }
                     catch (Exception ex)
                     {
                         Debug.WriteLine("ERROR: Exception in ProcessCommand :: " + ex.Message);
-                        bc.OnCommandResult -= OnCommandResultHandler;
+                        bc.RemoveResultHandler(commandCallParams.CallbackId);
                         bc.OnCustomScript -= OnCustomScriptHandler;
 
                         Debug.WriteLine("ERROR: failed to InvokeMethodNamed :: " + commandCallParams.Action
+ " on Object :: " + commandCallParams.Service);
-
                         this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.INVALID_ACTION));
-
                         return;
                     }
                 };
@@ -140,7 +144,7 @@ namespace WPCordovaClassLib.Cordova
                     new Thread(methodInvokation).Start();
                 }
 
-                    
+
             }
             catch (Exception ex)
             {
@@ -174,6 +178,12 @@ namespace WPCordovaClassLib.Cordova
                 return;
             }
 
+            if (!String.IsNullOrEmpty(result.CallbackId) && callbackId != result.CallbackId)
+            {
+                Debug.WriteLine("Multiple Overlapping Results :: " + result.CallbackId +
" :: " + callbackId);
+                return;
+            }
+
             #endregion
 
             string jsonResult = result.ToJSONString();

http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/9b532e67/templates/standalone/cordovalib/PluginResult.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/PluginResult.cs b/templates/standalone/cordovalib/PluginResult.cs
index e6d0c56..00017d2 100644
--- a/templates/standalone/cordovalib/PluginResult.cs
+++ b/templates/standalone/cordovalib/PluginResult.cs
@@ -69,6 +69,7 @@ namespace WPCordovaClassLib.Cordova
         public Status Result { get; private set; }
         public string Message { get; set; }
         public bool KeepCallback { get; set; }
+        public string CallbackId { get; set; }
 
         /// <summary>
         /// Whether command succeded or not
@@ -113,6 +114,7 @@ namespace WPCordovaClassLib.Cordova
 
         }
 
+        [Obsolete]
         public string ToCallbackString(string callbackId, string successCallback, string
errorCallback)
         {
             if (this.IsSuccess)


Mime
View raw message