Repository: playcanvas/engine (js)

Issue:
If all assets are already loaded and AssetListLoader is used to load the assets, the done callback is not called
Project: https://playcanvas.com/project/966580/overview/asset-list-all-loader-bug
Expectation: 'Done' to be printed in console

Workaround, use the ready function to set a callback.

Looks like in the `load` function, we just need to call `_loadingComplete` where we fire the `load` event here: 

https://github.com/playcanvas/engine/blob/main/src/asset/asset-list-loader.js#L130

Pull request:
AssetListLoader fix
The AssetListLoader should call loadingComplete in the load function, even if all assets have been previously loaded. The loadingComplete function will internally fire the load event as necessary. This mirrors the functionality of the ready function. I've also added a test for this case.

Fixes #4512

I confirm I have read the [contributing guidelines](https://github.com/playcanvas/engine/blob/master/.github/CONTRIBUTING.md) and signed the [Contributor License Agreement](https://docs.google.com/a/playcanvas.com/forms/d/1Ih69zQfJG-QDLIEpHr6CsaAs6fPORNOVnMv5nuo0cjk/viewform).


Patch:
diff --git a/src/asset/asset-list-loader.js b/src/asset/asset-list-loader.js
index 2b734e8d6..18d7b31e4 100644
--- a/src/asset/asset-list-loader.js
+++ b/src/asset/asset-list-loader.js
@@ -127,7 +127,7 @@ class AssetListLoader extends EventHandler {
             }
         });
         if (!loadingAssets && this._waitingAssets.size === 0) {
-            this.fire("load", Array.from(this._assets));
+            this._loadingComplete();
         }
     }
 


Test patch:
diff --git a/test/asset/asset-list-loader.test.mjs b/test/asset/asset-list-loader.test.mjs
index 9da9a04d6..0a0e0bae7 100644
--- a/test/asset/asset-list-loader.test.mjs
+++ b/test/asset/asset-list-loader.test.mjs
@@ -113,7 +113,7 @@ describe('AssetListLoader', function () {
 
     describe('#load', function () {
 
-        it('can succeed if an asset is already loaded', function (done) {
+        it('can call the ready callback if an asset is already loaded', function (done) {
             const asset = new Asset('model', 'container', { url: `${assetPath}test.glb` });
             const assetListLoader = new AssetListLoader([asset], app.assets);
             asset.on('load', (asset) => {
@@ -130,6 +130,19 @@ describe('AssetListLoader', function () {
             app.assets.load(asset);
         });
 
+        it('can call the load callback if an asset is already loaded', function (done) {
+            const asset = new Asset('model', 'container', { url: `${assetPath}test.glb` });
+            const assetListLoader = new AssetListLoader([asset], app.assets);
+            asset.on('load', (asset) => {
+                expect(asset.loaded).to.equal(true);
+                assetListLoader.load(() => {
+                    done();
+                });
+            });
+            app.assets.add(asset);
+            app.assets.load(asset);
+        });
+
         it('can succeed if one asset is already loaded and another is not', function (done) {
             const assets = [
                 new Asset('model', 'container', { url: `${assetPath}test.glb` }),