Skip to content

Commit

Permalink
Merge branch 'fix-xhr-race-condition' into 3.1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 28, 2013
2 parents 32b1369 + 76c958a commit fcbf4f2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 39 deletions.
34 changes: 17 additions & 17 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1628,8 +1628,16 @@ d3 = function() {
function d3_identity(d) {
return d;
}
d3.xhr = function(url, mimeType, callback) {
var xhr = {}, dispatch = d3.dispatch("progress", "load", "error"), headers = {}, response = d3_identity, request = new (d3_window.XDomainRequest && /^(http(s)?:)?\/\//.test(url) ? XDomainRequest : XMLHttpRequest)();
d3.xhr = d3_xhrType(d3_identity);
function d3_xhrType(response) {
return function(url, mimeType, callback) {
if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType,
mimeType = null;
return d3_xhr(url, mimeType, response, callback);
};
}
function d3_xhr(url, mimeType, response, callback) {
var xhr = {}, dispatch = d3.dispatch("progress", "load", "error"), headers = {}, request = new (d3_window.XDomainRequest && /^(http(s)?:)?\/\//.test(url) ? XDomainRequest : XMLHttpRequest)();
"onload" in request ? request.onload = request.onerror = respond : request.onreadystatechange = function() {
request.readyState > 3 && respond();
};
Expand Down Expand Up @@ -1693,10 +1701,8 @@ d3 = function() {
return xhr;
};
d3.rebind(xhr, dispatch, "on");
if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType,
mimeType = null;
return callback == null ? xhr : xhr.get(d3_xhr_fixCallback(callback));
};
}
function d3_xhr_fixCallback(callback) {
return callback.length === 1 ? function(error, request) {
callback(error == null ? request : null);
Expand Down Expand Up @@ -8555,31 +8561,25 @@ d3 = function() {
d3.time.scale.utc = function() {
return d3_time_scale(d3.scale.linear(), d3_time_scaleUTCMethods, d3_time_scaleUTCFormat);
};
d3.text = function() {
return d3.xhr.apply(d3, arguments).response(d3_text);
};
function d3_text(request) {
d3.text = d3_xhrType(function(request) {
return request.responseText;
}
});
d3.json = function(url, callback) {
return d3.xhr(url, "application/json", callback).response(d3_json);
return d3_xhr(url, "application/json", d3_json, callback);
};
function d3_json(request) {
return JSON.parse(request.responseText);
}
d3.html = function(url, callback) {
return d3.xhr(url, "text/html", callback).response(d3_html);
return d3_xhr(url, "text/html", d3_html, callback);
};
function d3_html(request) {
var range = d3_document.createRange();
range.selectNode(d3_document.body);
return range.createContextualFragment(request.responseText);
}
d3.xml = function() {
return d3.xhr.apply(d3, arguments).response(d3_xml);
};
function d3_xml(request) {
d3.xml = d3_xhrType(function(request) {
return request.responseXML;
}
});
return d3;
}();
10 changes: 5 additions & 5 deletions d3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/xhr/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "../core/document";
import "xhr";

d3.html = function(url, callback) {
return d3.xhr(url, "text/html", callback).response(d3_html);
return d3_xhr(url, "text/html", d3_html, callback);
};

function d3_html(request) {
Expand Down
2 changes: 1 addition & 1 deletion src/xhr/json.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "xhr";

d3.json = function(url, callback) {
return d3.xhr(url, "application/json", callback).response(d3_json);
return d3_xhr(url, "application/json", d3_json, callback);
};

function d3_json(request) {
Expand Down
8 changes: 2 additions & 6 deletions src/xhr/text.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import "xhr";

d3.text = function() {
return d3.xhr.apply(d3, arguments).response(d3_text);
};

function d3_text(request) {
d3.text = d3_xhrType(function(request) {
return request.responseText;
}
});
13 changes: 10 additions & 3 deletions src/xhr/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ import "../core/identity";
import "../core/rebind";
import "../event/dispatch";

d3.xhr = function(url, mimeType, callback) {
d3.xhr = d3_xhrType(d3_identity);

function d3_xhrType(response) {
return function(url, mimeType, callback) {
if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType, mimeType = null;
return d3_xhr(url, mimeType, response, callback);
};
}

function d3_xhr(url, mimeType, response, callback) {
var xhr = {},
dispatch = d3.dispatch("progress", "load", "error"),
headers = {},
response = d3_identity,
request = new (d3_window.XDomainRequest && /^(http(s)?:)?\/\//.test(url) ? XDomainRequest : XMLHttpRequest);

"onload" in request
Expand Down Expand Up @@ -85,7 +93,6 @@ d3.xhr = function(url, mimeType, callback) {

d3.rebind(xhr, dispatch, "on");

if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType, mimeType = null;
return callback == null ? xhr : xhr.get(d3_xhr_fixCallback(callback));
};

Expand Down
8 changes: 2 additions & 6 deletions src/xhr/xml.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import "xhr";

d3.xml = function() {
return d3.xhr.apply(d3, arguments).response(d3_xml);
};

function d3_xml(request) {
d3.xml = d3_xhrType(function(request) {
return request.responseXML;
}
});

0 comments on commit fcbf4f2

Please sign in to comment.