Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundles Refactor #5675

Merged
merged 28 commits into from Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b7d15d3
wip
Maksims Sep 13, 2023
3751973
Bundles refactor
Maksims Sep 25, 2023
ddc24fe
Merge branch 'master' into bundles-refactor
Maksims Sep 25, 2023
33a340c
fix jsdoc
Maksims Sep 25, 2023
49fed73
Merge branch 'main' into bundles-refactor
Maksims Sep 26, 2023
953a5d0
@internal > @ignore
Maksims Sep 26, 2023
f0da59b
PR comments
Maksims Sep 26, 2023
ed664fa
engine-only example for bundle loading
Maksims Sep 28, 2023
51125b4
Merge branch 'main' into bundles-refactor
Maksims Sep 28, 2023
a656bb3
Merge branch 'main' into bundles-refactor
Maksims Oct 3, 2023
3777956
Merge branch 'main' into bundles-refactor
Maksims Oct 7, 2023
c60a3b8
Merge branch 'main' into bundles-refactor
Maksims Oct 13, 2023
6efe8ad
merge
Maksims Oct 24, 2023
3652a88
update bundle example to new examples format
Maksims Oct 24, 2023
67fb021
merge
Maksims Nov 10, 2023
4442d9c
Merge branch 'main' into bundles-refactor
Maksims Nov 14, 2023
1bed8e9
Merge branch 'main' into bundles-refactor
Maksims Nov 23, 2023
984a08a
Merge branch 'main' into bundles-refactor
Maksims Nov 28, 2023
cf9a172
Merge branch 'main' into bundles-refactor
Maksims Dec 12, 2023
a96d04f
edits based on PR review comments
Maksims Mar 4, 2024
ec11e79
merge
Maksims Mar 4, 2024
6a0108a
lint, fix
Maksims Mar 4, 2024
682e9b0
events docs
Maksims Mar 4, 2024
5b0b6ed
lazy initialize TextDecoder
Maksims Mar 4, 2024
2a24e92
example update
Maksims Mar 4, 2024
2927e30
linter
Maksims Mar 4, 2024
043847f
Merge branch 'main' into bundles-refactor
kpal81xd Mar 5, 2024
9a0f7ee
Merge branch 'main' into bundles-refactor
kpal81xd Mar 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/event-handle.js
Expand Up @@ -97,7 +97,7 @@ class EventHandle {
/**
* Mark if event has been removed.
* @type {boolean}
* @internal
* @ignore
*/
set removed(value) {
if (!value) return;
Expand Down
82 changes: 74 additions & 8 deletions src/framework/asset/asset-registry.js
Expand Up @@ -26,6 +26,14 @@ import { Asset } from './asset.js';
* @param {Asset} [asset] - The loaded asset if no errors were encountered.
*/

/**
* Callback used by {@link ResourceLoader#load} and called when an asset is choosing a bundle
* to load from. Return a single bundle to ensure asset is loaded from it.
*
* @callback BundlesFilterCallback
* @param {import('../bundle/bundle.js').Bundle[]} bundles - List of bundles which contain the asset.
*/

/**
* Container for all assets that are available to this application. Note that PlayCanvas scripts
* are provided with an AssetRegistry instance as `app.assets`.
Expand Down Expand Up @@ -71,6 +79,13 @@ class AssetRegistry extends EventHandler {
*/
prefix = null;

/**
* BundleRegistry
*
* @type {import('../bundle/bundle-registry.js').BundleRegistry|null}
*/
bundles = null;

/**
* Create an instance of an AssetRegistry.
*
Expand Down Expand Up @@ -345,6 +360,13 @@ class AssetRegistry extends EventHandler {
* out when it is loaded.
*
* @param {Asset} asset - The asset to load.
* @param {object} [options] - Options for asset loading.
* @param {boolean} [options.bundlesIgnore] - If set to true, then asset will not try to load
* from a bundle. Defaults to false.
* @param {BundlesFilterCallback} [options.bundlesFilter] - A callback that will be called
* when loading an asset that is contained in any of the bundles. It provides an array of
* bundles and will ensure asset is loaded from bundle returned from a callback. By default
* smallest filesize bundle is choosen.
* @example
* // load some assets
* const assetsToLoad = [
Expand All @@ -362,16 +384,24 @@ class AssetRegistry extends EventHandler {
* app.assets.load(assetToLoad);
* });
*/
load(asset) {
load(asset, options) {
kpal81xd marked this conversation as resolved.
Show resolved Hide resolved
// do nothing if asset is already loaded
// note: lots of code calls assets.load() assuming this check is present
// don't remove it without updating calls to assets.load() with checks for the asset.loaded state
if (asset.loading || asset.loaded) {
if ((asset.loading || asset.loaded) && !options?.force) {
Maksims marked this conversation as resolved.
Show resolved Hide resolved
Maksims marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const file = asset.file;

const _fireLoad = () => {
this.fire('load', asset);
this.fire('load:' + asset.id, asset);
if (file && file.url)
this.fire('load:url:' + file.url, asset);
asset.fire('load', asset);
};

// open has completed on the resource
const _opened = (resource) => {
if (resource instanceof Array) {
Expand All @@ -383,11 +413,28 @@ class AssetRegistry extends EventHandler {
// let handler patch the resource
this._loader.patch(asset, this);

this.fire('load', asset);
this.fire('load:' + asset.id, asset);
if (file && file.url)
this.fire('load:url:' + file.url, asset);
asset.fire('load', asset);
if (asset.type === 'bundle') {
const assetIds = asset.data.assets;
for (let i = 0; i < assetIds.length; i++) {
const assetInBundle = this._idToAsset.get(assetIds[i]);
if (assetInBundle && !assetInBundle.loaded) {
this.load(assetInBundle, { force: true });
}
}

if (asset.resource.loaded) {
_fireLoad();
} else {
this.fire('load:start', asset);
this.fire('load:start:' + asset.id, asset);
if (file && file.url)
this.fire('load:start:url:' + file.url, asset);
asset.fire('load:start', asset);
asset.resource.on('load', _fireLoad);
}
} else {
_fireLoad();
}
};

// load has completed on the resource
Expand Down Expand Up @@ -419,7 +466,26 @@ class AssetRegistry extends EventHandler {
this.fire('load:' + asset.id + ':start', asset);

asset.loading = true;
this._loader.load(asset.getFileUrl(), asset.type, _loaded, asset);

const fileUrl = asset.getFileUrl();

// mark bundle assets as loading
if (asset.type === 'bundle') {
const assetIds = asset.data.assets;
for (let i = 0; i < assetIds.length; i++) {
const assetInBundle = this._idToAsset.get(assetIds[i]);
if (!assetInBundle)
continue;

if (assetInBundle.loaded || assetInBundle.resource || assetInBundle.loading)
continue;

assetInBundle.loading = true;
}
}


this._loader.load(fileUrl, asset.type, _loaded, asset, options);
} else {
// asset has no file to load, open it directly
const resource = this._loader.open(asset.type, asset.data);
Expand Down
9 changes: 8 additions & 1 deletion src/framework/asset/asset.js
Expand Up @@ -53,7 +53,7 @@ class Asset extends EventHandler {
*
* @param {string} name - A non-unique but human-readable name which can be later used to
* retrieve the asset.
* @param {string} type - Type of asset. One of ["animation", "audio", "binary", "container",
* @param {string} type - Type of asset. One of ["animation", "audio", "binary", "bundle", "container",
* "cubemap", "css", "font", "json", "html", "material", "model", "script", "shader", "sprite",
* "template", text", "texture", "textureatlas"]
* @param {object} [file] - Details about the file the asset is made from. At the least must
Expand Down Expand Up @@ -117,6 +117,8 @@ class Asset extends EventHandler {
// This is where the loaded resource(s) will be
this._resources = [];

this.urlObject = null;

// a string-assetId dictionary that maps
// locale to asset id
this._i18n = {};
Expand Down Expand Up @@ -510,6 +512,11 @@ class Asset extends EventHandler {

const old = this._resources;

if (this.urlObject) {
URL.revokeObjectURL(this.urlObject);
this.urlObject = null;
}

// clear resources on the asset
this.resources = [];
this.loaded = false;
Expand Down