Return-Path: X-Original-To: apmail-cordova-issues-archive@minotaur.apache.org Delivered-To: apmail-cordova-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 366341024C for ; Fri, 20 Sep 2013 10:46:59 +0000 (UTC) Received: (qmail 65889 invoked by uid 500); 20 Sep 2013 10:46:57 -0000 Delivered-To: apmail-cordova-issues-archive@cordova.apache.org Received: (qmail 65826 invoked by uid 500); 20 Sep 2013 10:46:55 -0000 Mailing-List: contact issues-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list issues@cordova.apache.org Received: (qmail 65795 invoked by uid 99); 20 Sep 2013 10:46:52 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 20 Sep 2013 10:46:52 +0000 Date: Fri, 20 Sep 2013 10:46:51 +0000 (UTC) From: "Jonathan Naguin (JIRA)" To: issues@cordova.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (CB-4873) XHRHelper is failing with simultaneous asynchronous requests MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/CB-4873?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13772930#comment-13772930 ] Jonathan Naguin commented on CB-4873: ------------------------------------- I tried to make fail the test, but without success... So I checked again my code and I think I found the "problem": I have this {{Main}} file as: {code} require( [ 'modules/login/LoginView', 'modules/home/HomeView' ], function (LoginView, HomeView) { ... } {code} While {{LoginView}} is as (notice the dependency with {{HomeView}}): {code} define( [ 'modules/home/HomeView' ], function (HomeView) { ... } {code} This works fine in every browser/platforms, but causes the problem with WP8 (I don't know why). But if I changed to: {code} require([ 'require' ], function (require) { require(['modules/home/HomeView'], function (HomeView) { //do something with HomeView }); require(['modules/login/LoginView'], function (LoginView) { //do something with LoginView }); } {code} The problem *disapears*. My guess is that is something related with Require JS but I am not sure. > XHRHelper is failing with simultaneous asynchronous requests > ------------------------------------------------------------ > > Key: CB-4873 > URL: https://issues.apache.org/jira/browse/CB-4873 > Project: Apache Cordova > Issue Type: Bug > Components: WP7, WP8 > Affects Versions: 3.0.0 > Environment: Any > Reporter: Jonathan Naguin > Assignee: Jesse MacFadyen > Priority: Critical > Labels: WP8, ajax, asynchronous, multiple, xhrhelper > > XHRHelper is failing in processing mutiple simultaneous asynchronous AJAX requests. I am using the latest code from https://github.com/apache/cordova-wp8/blob/master/wp8/template/cordovalib/XHRHelper.cs > The problem is related with {{_onXHRLocalCallback}} which is save into the {{window}} object as a unique function. When, for example, two Ajax requests are evaluated at same time, the last {{funk}} function overrides the first {{_onXHRLocalCallback}} without receive the data from the C# code to that particular request. > To demostrate this I put {{console.log("XHR: " + resolvedUrl);}} inside {{__onXHRLocalCallback}} and {{System.Diagnostics.Debug.WriteLine("HandleCommand: " + url);}} in {{HandleCommand}} method (my code uses *Require JS* to load this resources). The output is this: > {code} > HandleCommand: x-wmapp0:www/src/modules/home/HomeView.html > HandleCommand: x-wmapp0:www/src/modules/orders/OrdersView.html > XHR: x-wmapp0:www/src/modules/orders/OrdersView.html > XHR: x-wmapp0:www/src/modules/orders/OrdersView.html > XHR: HandleCommand: x-wmapp0:www/src/modules/order/OrderDetailView.html > XHR: x-wmapp0:www/src/modules/order/OrderDetailView.html > {code} > As you can see, one request is missing: "HomeView.html". > h6. NOTES > - If I set {{false}} the {{this.isAsync}} variable it works (this way it is executed without using setTimeout). > - If I put a console.log before launch {{funk}} it works. > - It works on the simulator, but it fails on a real device. > h6. Possible solution > In conclusion, I assumed that it's a timing problem. To resolve it I decided to save a onXHRLocalCallback function per each request: > {code} > var funk = function () { > if (! window.__onXHRLocalCallback){ > window.__onXHRLocalCallback = {}; //Object to store the functions > } > > window.__onXHRLocalCallback[resolvedUrl] = function (responseCode, responseText) { > alias.status = responseCode; > if (responseCode == '200') { > alias.responseText = responseText; > } > else { > alias.onerror && alias.onerror(responseCode); > } > alias.changeReadyState(XHRShim.DONE); > delete window.__onXHRLocalCallback[resolvedUrl]; //Delete the function > } > alias.changeReadyState(XHRShim.LOADING); > window.external.Notify('XHRLOCAL/' + resolvedUrl); > } > {code} > So I had to change in {{HandleCommand}} method the way of invoking this callback. I decided to create a helper function to be called in each case: > {code} > /// > /// Invoke a XHR callback > /// > /// The URL of the request > /// The response code > /// The response text > private void InvokeCallback(string url, int code, string text) > { > string args = string.Format("('{0}', {1}, {2});", code, WPCordovaClassLib.Cordova.JSON.JsonHelper.Serialize(text), WPCordovaClassLib.Cordova.JSON.JsonHelper.Serialize(url)); > string callback = @"(function(code, text, url){ > try { > window.__onXHRLocalCallback[ url ].call(null, code, text); > } > catch(e) { > console.log('Error calling method from XHRHelper :: ' + e); > } > })" + args; > Browser.InvokeScript("eval", new string[] { callback }); > } > {code} > To be called as {{InvokeCallback(url, 200, text);}} or {{InvokeCallback(url, 404, null);}} > Thanks. -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators For more information on JIRA, see: http://www.atlassian.com/software/jira