Skip to content

Commit 3c53044

Browse files
Fix handling image/file cache for three r178 and above (#5776)
* Fix handling image/file cache now that the ImageLoader/FileLoader are using a prefix since three r178 (fix #5771) * Don't set cache for video, it's not used
1 parent b0148d9 commit 3c53044

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

src/core/a-assets.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AAssets extends ANode {
4141
loaded.push(new Promise(function (resolve, reject) {
4242
// Set in cache because we won't be needing to call three.js loader if we have.
4343
// a loaded media element.
44-
THREE.Cache.add(imgEls[i].getAttribute('src'), imgEl);
44+
THREE.Cache.add('image:' + imgEls[i].getAttribute('src'), imgEl);
4545
if (imgEl.complete) {
4646
resolve();
4747
return;
@@ -166,13 +166,6 @@ function mediaElementLoaded (el) {
166166

167167
// Compare seconds buffered to media duration.
168168
if (secondsBuffered >= el.duration) {
169-
// Set in cache because we won't be needing to call three.js loader if we have.
170-
// a loaded media element.
171-
// Store video elements only. three.js loader is used for audio elements.
172-
// See assetParse too.
173-
if (el.tagName === 'VIDEO') {
174-
THREE.Cache.add(el.getAttribute('src'), el);
175-
}
176169
resolve();
177170
}
178171
}

tests/components/material.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,16 @@ suite('material', function () {
173173
var textureLoaderSpy = this.sinon.spy(THREE.TextureLoader.prototype, 'load');
174174
img.setAttribute('src', IMG_SRC);
175175
img.setAttribute('id', 'foo');
176-
THREE.Cache.files[IMG_SRC] = img;
176+
THREE.Cache.clear();
177177
assetsEl.appendChild(img);
178178
el.sceneEl.appendChild(assetsEl);
179+
// Adding the asset will add image:${IMG_SRC} in THREE.Cache.files
180+
// without going through THREE.ImageLoader
179181
el.addEventListener('materialtextureloaded', function () {
180182
assert.notOk(imageLoaderSpy.called);
181183
assert.notOk(textureLoaderSpy.called);
182-
delete THREE.Cache.files[IMG_SRC];
184+
assert.ok(`image:${IMG_SRC}` in THREE.Cache.files);
185+
THREE.Cache.clear();
183186
THREE.ImageLoader.prototype.load.restore();
184187
THREE.TextureLoader.prototype.load.restore();
185188
done();
@@ -191,10 +194,11 @@ suite('material', function () {
191194
var imageLoaderSpy = this.sinon.spy(THREE.ImageLoader.prototype, 'load');
192195
el.addEventListener('materialtextureloaded', function () {
193196
assert.ok(imageLoaderSpy.called);
194-
assert.ok(IMG_SRC in THREE.Cache.files);
197+
assert.ok(`image:${IMG_SRC}` in THREE.Cache.files);
195198
THREE.ImageLoader.prototype.load.restore();
196199
done();
197200
});
201+
// The image is loaded via the material system using THREE.ImageLoader
198202
el.setAttribute('material', 'src', IMG_SRC);
199203
});
200204

tests/core/a-assets.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ suite('a-assets', function () {
8181
assetsEl.appendChild(img);
8282

8383
img.addEventListener('load', function () {
84-
assert.equal(THREE.Cache.files[IMG_SRC], img);
84+
assert.equal(THREE.Cache.files[`image:${IMG_SRC}`], img);
8585
done();
8686
});
8787

@@ -248,7 +248,7 @@ suite('a-asset-item', function () {
248248
});
249249

250250
test('emits progress event', function (done) {
251-
THREE.Cache.remove(XHR_SRC);
251+
THREE.Cache.remove(`file:${XHR_SRC}`);
252252
var assetItem = document.createElement('a-asset-item');
253253
assetItem.setAttribute('src', XHR_SRC);
254254
assetItem.addEventListener('progress', function (evt) {
@@ -284,7 +284,7 @@ suite('a-asset-item', function () {
284284
assetItem2.setAttribute('src', XHR_SRC);
285285

286286
// Remove cache data to not load from it.
287-
THREE.Cache.remove(XHR_SRC);
287+
THREE.Cache.remove(`file:${XHR_SRC}`);
288288

289289
assetItem1.addEventListener('error', function (evt) {
290290
assert.ok(evt.detail.xhr !== undefined);
@@ -306,7 +306,7 @@ suite('a-asset-item', function () {
306306
test('loads as text without responseType attribute', function (done) {
307307
var assetItem = document.createElement('a-asset-item');
308308
// Remove cache data to not load from it.
309-
THREE.Cache.remove(XHR_SRC);
309+
THREE.Cache.remove(`file:${XHR_SRC}`);
310310
assetItem.setAttribute('src', XHR_SRC);
311311
assetItem.addEventListener('loaded', function (evt) {
312312
assert.ok(assetItem.data !== null);
@@ -319,7 +319,7 @@ suite('a-asset-item', function () {
319319

320320
test('loads as arraybuffer', function (done) {
321321
var assetItem = document.createElement('a-asset-item');
322-
THREE.Cache.remove(XHR_SRC);
322+
THREE.Cache.remove(`file:${XHR_SRC}`);
323323
assetItem.setAttribute('src', XHR_SRC);
324324
assetItem.setAttribute('response-type', 'arraybuffer');
325325
assetItem.addEventListener('loaded', function (evt) {
@@ -344,7 +344,7 @@ suite('a-asset-item', function () {
344344
});
345345

346346
test('reloads as text', function (done) {
347-
THREE.Cache.remove(XHR_SRC);
347+
THREE.Cache.remove(`file:${XHR_SRC}`);
348348
var assetItem = document.createElement('a-asset-item');
349349
assetItem.setAttribute('src', XHR_SRC);
350350
assetItem.addEventListener('loaded', function (evt) {

0 commit comments

Comments
 (0)