Repository: cordova-wp8 Updated Branches: refs/heads/master 4d65cc817 -> 6ded15611
[CB-6763] Fixes issue when multiple simultaneous requests are sent. Project: http://git-wip-us.apache.org/repos/asf/cordova-wp8/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-wp8/commit/efaf559a Tree: http://git-wip-us.apache.org/repos/asf/cordova-wp8/tree/efaf559a Diff: http://git-wip-us.apache.org/repos/asf/cordova-wp8/diff/efaf559a Branch: refs/heads/master Commit: efaf559ae846567e68e4d96446c8760c3ceed8fc Parents: 64fe2f6 Author: Vladimir Kotikov <[email protected]> Authored: Tue May 27 15:45:43 2014 +0400 Committer: Vladimir Kotikov <[email protected]> Committed: Wed Jun 4 13:22:22 2014 +0400 ---------------------------------------------------------------------- wp8/template/cordovalib/XHRHelper.cs | 62 +++++++++++++++++-------------- 1 file changed, 34 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/efaf559a/wp8/template/cordovalib/XHRHelper.cs ---------------------------------------------------------------------- diff --git a/wp8/template/cordovalib/XHRHelper.cs b/wp8/template/cordovalib/XHRHelper.cs index d1651c3..534d550 100644 --- a/wp8/template/cordovalib/XHRHelper.cs +++ b/wp8/template/cordovalib/XHRHelper.cs @@ -18,9 +18,28 @@ namespace WPCordovaClassLib.CordovaLib 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 @@ namespace WPCordovaClassLib.CordovaLib 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 @@ namespace WPCordovaClassLib.CordovaLib { 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 @@ namespace WPCordovaClassLib.CordovaLib 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 @@ namespace WPCordovaClassLib.CordovaLib 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 @@ namespace WPCordovaClassLib.CordovaLib 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; } }
