Skip to content

Commit 8d07b9c

Browse files
ngokevindmarcos
authored andcommitted
make xhrloader public from <a-assets> (fixes #2022) (#2023)
* make xhrloader public from <a-assets> (fixes #2022) * rename to fileloader
1 parent d6e695a commit 8d07b9c

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

‎docs/core/asset-management-system.md‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ Given that CORS headers *are* set, `<a-assets>` will automatically set
8181

8282
## Preloading Audio and Video
8383

84-
Audio and video assets will only block the scene if `autoplay` is set or if `preload="auto"`:
84+
Audio and video assets will only block the scene if `autoplay` is set or if
85+
`preload="auto"`:
8586

8687
```html
8788
<a-scene>
@@ -185,3 +186,17 @@ Thus, since we block entity initialization on assets, by the time entities
185186
load, all assets will have been already fetched. As long as `<a-asset-item>`s
186187
are defined, and the entity is fetching files using some form
187188
`THREE.XHRLoader`, then caching will automatically work.
189+
190+
## Accessing the `XHRLoader` and Cache
191+
192+
To access the three.js `XHRLoader` if we want to listen more closely:
193+
194+
```js
195+
console.log(document.querySelector('a-assets').fileLoader);
196+
```
197+
198+
To access the cache that stores XHR responses:
199+
200+
```js
201+
console.log(THREE.Cache);
202+
```

‎src/core/a-assets.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var debug = require('../utils/debug');
44
var registerElement = require('./a-register-element').registerElement;
55
var THREE = require('../lib/three');
66

7-
var xhrLoader = new THREE.XHRLoader();
7+
var fileLoader = new THREE.XHRLoader();
88
var warn = debug('core:a-assets:warn');
99

1010
/**
@@ -15,6 +15,7 @@ module.exports = registerElement('a-assets', {
1515
createdCallback: {
1616
value: function () {
1717
this.isAssets = true;
18+
this.fileLoader = fileLoader;
1819
}
1920
},
2021

@@ -87,7 +88,7 @@ registerElement('a-asset-item', {
8788
value: function () {
8889
var self = this;
8990
var src = this.getAttribute('src');
90-
xhrLoader.load(src, function (textResponse) {
91+
fileLoader.load(src, function (textResponse) {
9192
THREE.Cache.files[src] = textResponse;
9293
self.data = textResponse;
9394
// Workaround for a Chrome bug.

‎tests/core/a-assets.test.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* global assert, setup, suite, test */
2+
var THREE = require('lib/three');
23

34
suite('a-assets', function () {
45
// Empty src will not trigger load events in Chrome. Use data URI where a load event is needed.
@@ -18,6 +19,19 @@ suite('a-assets', function () {
1819
document.body.appendChild(scene);
1920
});
2021

22+
test('throws error if not in a-scene', function () {
23+
var div = document.createElement('div');
24+
var assets = document.createElement('a-assets');
25+
div.appendChild(assets);
26+
assert.throws(function () {
27+
assets.attachedCallback();
28+
}, Error);
29+
});
30+
31+
test('has fileLoader', function () {
32+
assert.ok(this.el.fileLoader.constructor, THREE.XHRLoader);
33+
});
34+
2135
test('waits for images to load', function (done) {
2236
var el = this.el;
2337
var scene = this.scene;

0 commit comments

Comments
 (0)