Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions src/framework/app-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
RESOLUTION_AUTO, RESOLUTION_FIXED
} from './constants.js';
import { Asset } from './asset/asset.js';
import { AssetListLoader } from './asset/asset-list-loader.js';
import { AssetRegistry } from './asset/asset-registry.js';
import { BundleRegistry } from './bundle/bundle-registry.js';
import { ComponentSystemRegistry } from './components/registry.js';
Expand Down Expand Up @@ -707,9 +708,7 @@
this.fire('preload:start');

// get list of assets to preload
const assets = this.assets.list({
preload: true
});
const assets = this.assets.filter(asset => asset.preload === true && asset.loaded === false)

Check failure on line 711 in src/framework/app-base.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
Comment thread
mvaligursky marked this conversation as resolved.
Outdated

if (assets.length === 0) {
this.fire('preload:end');
Expand All @@ -719,25 +718,18 @@

let loadedCount = 0;

const onAssetLoadOrError = () => {
const onAssetLoad = () => {
loadedCount++;
this.fire('preload:progress', loadedCount / assets.length);

if (loadedCount === assets.length) {
this.fire('preload:end');
callback();
}
};

// for each asset
assets.forEach((asset) => {
if (!asset.loaded) {
asset.once('load', onAssetLoadOrError);
asset.once('error', onAssetLoadOrError);
this.assets.load(asset);
} else {
onAssetLoadOrError();
}
const assetListLoader = new AssetListLoader(assets, this.assets);

assetListLoader.on('progress', onAssetLoad);
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AssetListLoader only fires 'progress' events on successful asset loads, not on errors. This changes the behavior from the previous implementation where preload:progress was fired for both successful loads and errors. This means if some assets fail to load, the progress will not reach 1.0 (100%). Consider also handling the 'error' event from AssetListLoader, or document this behavior change.

Suggested change
assetListLoader.on('progress', onAssetLoad);
assetListLoader.on('progress', onAssetLoad);
assetListLoader.on('error', onAssetLoad);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There exists no error event for AssetListLoader. We would need to make adjustments to the AssetListLoader class to make this work.


assetListLoader.load(() => {
this.fire('preload:end');
callback();
});
}

Expand Down
41 changes: 41 additions & 0 deletions test/framework/application.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';

import { Asset } from '../../src/framework/asset/asset.js';
import { AssetRegistry } from '../../src/framework/asset/asset-registry.js';

Check failure on line 4 in test/framework/application.test.mjs

View workflow job for this annotation

GitHub Actions / Lint

`../../src/framework/asset/asset-registry.js` import should occur before import of `../../src/framework/asset/asset.js`
import { ComponentSystemRegistry } from '../../src/framework/components/registry.js';
import { FILLMODE_KEEP_ASPECT, RESOLUTION_FIXED } from '../../src/framework/constants.js';
import { Entity } from '../../src/framework/entity.js';
Expand All @@ -19,6 +20,7 @@
describe('Application', function () {

let app;
const assetPath = 'http://localhost:3000/test/assets/';

beforeEach(function () {
jsdomSetup();
Expand Down Expand Up @@ -89,4 +91,43 @@

});

describe('#preload', function () {

it('should preload assets with preload set to true', function (done) {
const assets = [
new Asset('model', 'container', { url: `${assetPath}test.glb` }),
new Asset('styling', 'css', { url: `${assetPath}test.css` })
];
assets.forEach((asset) => {
asset.preload = true;
app.assets.add(asset);
});

app.preload(function () {
assets.forEach((asset) => {
expect(asset.loaded).to.be.true;
});
done();
});
});

it('should not preload assets with preload set to false', function (done) {
const assets = [
new Asset('model', 'container', { url: `${assetPath}test.glb` }),
new Asset('styling', 'css', { url: `${assetPath}test.css` })
];
assets.forEach((asset) => {
app.assets.add(asset);
});

app.preload(function () {
assets.forEach((asset) => {
expect(asset.loaded).to.be.false;
});
done();
});
});

});

});
Loading