Skip to content

Commit aa4db04

Browse files
committed
Set the image cache when img is loaded
1 parent 1a7e100 commit aa4db04

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
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: 9 additions & 5 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
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.equal(THREE.Cache.get(`image:${IMG_SRC}`), img);
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
});

0 commit comments

Comments
 (0)