Skip to content

Commit 835aac4

Browse files
Set the image cache when img is loaded (#5778)
* Use THREE.Cache methods instead of accessing directly THREE.Cache.files in tests * Set the image cache when img is loaded
1 parent 4c9b4a4 commit 835aac4

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

src/core/a-assets.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ 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('image:' + imgEls[i].getAttribute('src'), imgEl);
4544
if (imgEl.complete) {
45+
THREE.Cache.add('image:' + imgEls[i].getAttribute('src'), imgEl);
4646
resolve();
4747
return;
4848
}
49-
imgEl.onload = resolve;
49+
imgEl.onload = function () {
50+
THREE.Cache.add('image:' + imgEls[i].getAttribute('src'), imgEl);
51+
resolve();
52+
};
5053
imgEl.onerror = reject;
5154
}));
5255
}

tests/components/material.test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,20 @@ suite('material', function () {
176176
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
179+
// Adding the asset will add image:${IMG_SRC} in THREE.Cache when the img
180+
// loading is complete with img.onload, without going through THREE.ImageLoader
181+
assert.notOk(!!THREE.Cache.get(`image:${IMG_SRC}`));
181182
el.addEventListener('materialtextureloaded', function () {
182183
assert.notOk(imageLoaderSpy.called);
183184
assert.notOk(textureLoaderSpy.called);
184-
assert.ok(`image:${IMG_SRC}` in THREE.Cache.files);
185-
THREE.Cache.clear();
186185
THREE.ImageLoader.prototype.load.restore();
187186
THREE.TextureLoader.prototype.load.restore();
188-
done();
187+
// load event is triggered after this materialtextureloaded callback
188+
img.addEventListener('load', function () {
189+
assert.equal(THREE.Cache.get(`image:${IMG_SRC}`), img);
190+
THREE.Cache.clear();
191+
done();
192+
}, {once: true});
189193
});
190194
el.setAttribute('material', 'src', '#foo');
191195
});
@@ -194,7 +198,7 @@ suite('material', function () {
194198
var imageLoaderSpy = this.sinon.spy(THREE.ImageLoader.prototype, 'load');
195199
el.addEventListener('materialtextureloaded', function () {
196200
assert.ok(imageLoaderSpy.called);
197-
assert.ok(`image:${IMG_SRC}` in THREE.Cache.files);
201+
assert.ok(!!THREE.Cache.get(`image:${IMG_SRC}`));
198202
THREE.ImageLoader.prototype.load.restore();
199203
done();
200204
});

tests/components/sound.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import THREE from 'lib/three.js';
55
suite('sound', function () {
66
setup(function (done) {
77
var el = this.el = entityFactory();
8-
THREE.Cache.files = {};
8+
THREE.Cache.clear();
99
setTimeout(() => {
1010
el.setAttribute('sound', {
1111
autoplay: true,

tests/core/a-assets.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ suite('a-assets', function () {
2020
done();
2121
});
2222
document.body.appendChild(scene);
23-
THREE.Cache.files = {};
23+
THREE.Cache.clear();
2424
});
2525

2626
test('loads even if one asset fails to load', function (done) {
@@ -81,7 +81,7 @@ suite('a-assets', function () {
8181
assetsEl.appendChild(img);
8282

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

0 commit comments

Comments
 (0)