Skip to content

Commit 6690c69

Browse files
committed
Merge branch 'hotfix-5.1.1'
2 parents 652f205 + b6544c8 commit 6690c69

File tree

5 files changed

+55
-26
lines changed

5 files changed

+55
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "xml3d.js",
33
"description": "XML3D implementation based on JS and WebGL",
44
"homepage": "http://ww.xml3d.org",
5-
"version": "5.1.0",
5+
"version": "5.1.1",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/xml3d/xml3d.js"

src/data/adapter/javascript/factory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ var JavaScriptFormatHandler = function () {
55
XML3D.createClass(JavaScriptFormatHandler, XML3D.resource.FormatHandler);
66

77
JavaScriptFormatHandler.prototype.isFormatSupported = function (response) {
8-
return response.headers.get("Content-Type") === "application/javascript";
8+
var contentType = response.headers.get("Content-Type");
9+
return !!contentType && contentType.match(/\/javascript/);
910
};
1011

1112

src/data/adapter/json/factory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ XML3D.createClass(XML3DJSONFormatHandler, XML3D.resource.FormatHandler);
1111

1212
XML3DJSONFormatHandler.prototype.isFormatSupported = function(response) {
1313
if (response.headers.has("Content-Type")) {
14-
return response.headers.get("Content-Type") === "application/json";
14+
var contentType = response.headers.get("Content-Type");
15+
return contentType.match(/\/json/);
1516
}
1617
if (response.url.match(/\.json/)) {
1718
return true;

src/resource/fetcher.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

22
require("whatwg-fetch");
33
var URI = require("../utils/uri.js").URI;
4-
var Options = require("../utils/options.js");
5-
6-
var MAX_CONCURRENT_REQUESTS = 100; // Maximum number of requests awaiting a response
74

85
var c_requestHooks = []; // Request hooks called for each outgoing request
96
var c_formatHandlers = []; // All registered FormatHandler plugins
@@ -22,21 +19,26 @@ Resource.fetch = function(uriString, opt) {
2219
opt = initOptions(opt);
2320
var uri = new URI(uriString);
2421

22+
for (var i=0; i < c_requestHooks.length; i++) {
23+
c_requestHooks[i](uri, opt);
24+
}
25+
if (opt.abort) {
26+
return Promise.reject(new RequestAbortedException(uri));
27+
}
28+
29+
return doFetch(uri, opt);
30+
};
31+
32+
function doFetch(uri, opt) {
2533
return new Promise(function(resolve, reject) {
26-
for (var i=0; i < c_requestHooks.length; i++) {
27-
c_requestHooks[i](uri, opt);
28-
}
29-
if (opt.abort) {
30-
throw new RequestAbortedException(uri);
31-
}
3234
scheduleRequest({
3335
opt : opt,
3436
uri : uri,
3537
resolve : resolve,
3638
reject : reject
3739
});
3840
});
39-
};
41+
}
4042

4143
var popRequestQueue = function() {
4244
var request = c_requestQueue.shift();
@@ -54,21 +56,38 @@ var popRequestQueue = function() {
5456
};
5557

5658
Resource.getDocument = function(urlString, opt) {
57-
return Resource.fetch(urlString, opt)
59+
opt = initOptions(opt);
60+
var uri = new URI(urlString);
61+
62+
for (var i=0; i < c_requestHooks.length; i++) {
63+
c_requestHooks[i](uri, opt);
64+
}
65+
if (opt.abort) {
66+
Promise.reject(new RequestAbortedException(uri));
67+
}
68+
69+
var cache;
70+
if (opt.allowCaching) {
71+
if (cache = c_cachedDocuments.get(urlString)) {
72+
// We can skip the fetch phase and piggy back on the result of the previous fetch, this avoids unnecessary Requests
73+
return cache.pending ? cache.pending : Promise.resolve(cache.document);
74+
}
75+
76+
// There is no pending or complete Request for this url so lets create a cache entry and start one
77+
cache = {fragments: []};
78+
c_cachedDocuments.set(urlString, cache); // Resource.parseResponse expects this entry to exist already
79+
}
80+
81+
var documentPromise = doFetch(urlString, opt)
5882
.then(function(response) {
5983
if (!response.ok) {
6084
throw new RequestFailedException(response);
6185
}
6286
response.originalURL = urlString;
63-
var cache;
64-
if (cache = c_cachedDocuments.get(urlString)) {
65-
return cache.pending ? cache.pending : cache.document;
66-
} else {
67-
cache = { fragments : [] };
68-
c_cachedDocuments.set(urlString, cache); // Resource.parseResponse expects this entry to exist already
69-
cache.pending = Resource.parseResponse(response);
70-
return cache.pending;
71-
}
87+
var cache = { fragments : [] };
88+
c_cachedDocuments.set(urlString, cache); // Resource.parseResponse expects this entry to exist already
89+
cache.pending = Resource.parseResponse(response);
90+
return cache.pending;
7291
}).then(function(doc) {
7392
doc._documentURL = urlString;
7493
var cache = c_cachedDocuments.get(urlString);
@@ -79,6 +98,12 @@ Resource.getDocument = function(urlString, opt) {
7998
c_cachedDocuments.has(urlString) && delete c_cachedDocuments.get(urlString).pending;
8099
throw exception;
81100
});
101+
102+
if (cache) {
103+
cache.pending = documentPromise;
104+
}
105+
106+
return documentPromise;
82107
};
83108

84109
Resource.parseResponse = function(response) {
@@ -144,7 +169,7 @@ var prioritySort = function(a, b) {
144169

145170
var tickWorkWindow = function() {
146171
// Both of these loops trigger asynchronous work through Promises so working through all queue items shouldn't block the thread for too long
147-
while (c_requestQueue.length > 0 && c_openRequests < MAX_CONCURRENT_REQUESTS) {
172+
while (c_requestQueue.length > 0) {
148173
popRequestQueue();
149174
}
150175

@@ -159,6 +184,7 @@ var initOptions = function(opt) {
159184
opt.headers = opt.headers || {};
160185
opt.priority = opt.priority || 0;
161186
opt.abort = opt.abort || false;
187+
opt.allowCaching = opt.allowCaching !== undefined ? opt.allowCaching : true;
162188
return opt;
163189
};
164190

src/resource/xml3dformathandler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ XML3D.createClass(XML3DFormatHandler, XML3D.resource.FormatHandler);
1616

1717
XML3DFormatHandler.prototype.isFormatSupported = function (response) {
1818
if (response.headers.has("Content-Type")) {
19-
return response.headers.get("Content-Type") === "application/xml";
19+
var contentType = response.headers.get("Content-Type");
20+
return contentType.match(/\/xml/);
2021
}
2122
if (response.url.match(/\.xml/)) {
2223
return true;
@@ -72,4 +73,4 @@ XML3D.xml3dFormatHandler = xml3dFormatHandler;
7273

7374
module.exports = {
7475
XML3DFormatHandler : XML3DFormatHandler
75-
};
76+
};

0 commit comments

Comments
 (0)