From efaf559ae846567e68e4d96446c8760c3ceed8fc Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Tue, 27 May 2014 15:45:43 +0400 Subject: [PATCH] [CB-6763] Fixes issue when multiple simultaneous requests are sent. --- wp8/template/cordovalib/XHRHelper.cs | 62 +++++++++++++++------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/wp8/template/cordovalib/XHRHelper.cs b/wp8/template/cordovalib/XHRHelper.cs index d1651c30..534d5503 100644 --- a/wp8/template/cordovalib/XHRHelper.cs +++ b/wp8/template/cordovalib/XHRHelper.cs @@ -18,9 +18,28 @@ public class XHRHelper : IBrowserDecorator public void InjectScript() { + string script = @"(function(win, doc) { + var __XHRShimAliases = {}; - string script = @"(function(win, doc) { + window.__onXHRLocalCallback = function (responseCode, responseText, reqId) { + if (__XHRShimAliases[reqId]){ + var alias = __XHRShimAliases[reqId]; + if (alias){ + delete __XHRShimAliases[reqId]; + if (responseCode == '200'){ + alias.onResult && alias.onResult(responseText); + Object.defineProperty(alias, 'responseXML', { + get: function () { + return new DOMParser().parseFromString(this.responseText, 'text/xml'); + } + }); + } else { + alias.onError && alias.onError(responseText); + } + } + } + }; var docDomain = null; try { @@ -221,32 +240,17 @@ public void InjectScript() resolvedUrl = basePath + resolvedUrl; // consider it relative } + // Generate unique request ID + var reqId = new Date().getTime().toString() + Math.random(); + var funk = function () { - window.__onXHRLocalCallback = function (responseCode, responseText) { - alias.status = responseCode; - if (responseCode == '200') { - alias.responseText = responseText; - Object.defineProperty(alias, 'responseXML', { - get: function () { - return new DOMParser().parseFromString(this.responseText, 'text/xml'); - } - }); - } - else { - alias.onerror && alias.onerror(responseCode); - } + __XHRShimAliases[reqId] = alias; - alias.changeReadyState(XHRShim.DONE); - } alias.changeReadyState(XHRShim.LOADING); - window.external.Notify('XHRLOCAL/' + resolvedUrl); - } - if (this.isAsync) { - setTimeout(funk, 0); - } - else { - funk(); - } + window.external.Notify('XHRLOCAL/' + reqId + '/' + resolvedUrl); + }; + + this.isAsync ? setTimeout(funk, 0) : funk(); } }, status: 404 @@ -262,7 +266,9 @@ public bool HandleCommand(string commandStr) { if (commandStr.IndexOf("XHRLOCAL") == 0) { - string url = commandStr.Replace("XHRLOCAL/", ""); + var reqStr = commandStr.Replace("XHRLOCAL/", "").Split(new char[] {'/'}, 2); + string reqId = reqStr[0]; + string url = reqStr[1]; Uri uri = new Uri(url, UriKind.RelativeOrAbsolute); @@ -273,7 +279,7 @@ public bool HandleCommand(string commandStr) using (TextReader reader = new StreamReader(isoFile.OpenFile(uri.AbsolutePath, FileMode.Open, FileAccess.Read))) { string text = reader.ReadToEnd(); - Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text }); + Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text, reqId }); return true; } } @@ -286,7 +292,7 @@ public bool HandleCommand(string commandStr) if (resource == null) { // 404 ? - Browser.InvokeScript("__onXHRLocalCallback", new string[] { "404" }); + Browser.InvokeScript("__onXHRLocalCallback", new string[] { "404", null, reqId }); return true; } else @@ -294,7 +300,7 @@ public bool HandleCommand(string commandStr) using (StreamReader streamReader = new StreamReader(resource.Stream)) { string text = streamReader.ReadToEnd(); - Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text }); + Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text, reqId }); return true; } }