Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/framework/asset/asset-file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* Wraps a source of asset data.
*
* @ignore
*/
class AssetFile {
constructor(url = '', filename = '', hash = null, size = null, opt = null, contents = null) {
Expand Down
180 changes: 94 additions & 86 deletions src/framework/asset/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,101 @@ class Asset extends EventHandler {
*/
static EVENT_REMOVELOCALIZED = 'remove:localized';

/**
* @type {AssetFile | null}
* @private
*/
_file = null;

/**
* A string-assetId dictionary that maps locale to asset id.
*
* @type {object}
* @private
*/
_i18n = {};

/**
* Whether to preload the asset.
*
* @type {boolean}
* @private
*/
_preload = false;

/**
* This is where the loaded resource(s) are stored.
*
* @type {object[]}
* @private
*/
_resources = [];

/**
* The asset id.
*
* @type {number}
*/
id = assetIdCounter--;

/**
* True if the asset has finished attempting to load the resource. It is not guaranteed
* that the resources are available as there could have been a network error.
*
* @type {boolean}
*/
loaded = false;

/**
* True if the resource is currently being loaded.
*
* @type {boolean}
*/
loading = false;

/**
* Optional JSON data that contains the asset handler options.
*
* @type {object}
*/
options = {};

/**
* The asset registry that this Asset belongs to.
*
* @type {AssetRegistry|null}
*/
registry = null;

/**
* Asset tags. Enables finding of assets by tags using the {@link AssetRegistry#findByTag} method.
*
* @type {Tags}
*/
tags = new Tags(this);

/**
* The type of the asset.
*
* @type {"animation"|"audio"|"binary"|"container"|"cubemap"|"css"|"font"|"json"|"html"|"material"|"model"|"render"|"script"|"shader"|"sprite"|"template"|"text"|"texture"|"textureatlas"}
*/
type;

/**
* The URL object.
*
* @type {string | null}
* @ignore
*/
urlObject = null;

/**
* Create a new Asset record. Generally, Assets are created in the loading process and you
* won't need to create them by hand.
*
* @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", "bundle", "container",
* "cubemap", "css", "font", "json", "html", "material", "model", "script", "shader", "sprite",
* "template", text", "texture", "textureatlas"]
* @param {"animation"|"audio"|"binary"|"container"|"cubemap"|"css"|"font"|"json"|"html"|"material"|"model"|"render"|"script"|"shader"|"sprite"|"template"|"text"|"texture"|"textureatlas"} type - Type of asset.
* @param {object} [file] - Details about the file the asset is made from. At the least must
* contain the 'url' field. For assets that don't contain file data use null.
* @param {string} [file.url] - The URL of the resource file that contains the asset data.
Expand All @@ -177,95 +263,17 @@ class Asset extends EventHandler {
* url: "http://example.com/my/assets/here/texture.png"
* });
*/
constructor(name, type, file, data, options) {
constructor(name, type, file, data = {}, options = {}) {
super();

this._id = assetIdCounter--;
this._name = name || '';

/**
* The type of the asset. One of ["animation", "audio", "binary", "container", "cubemap",
* "css", "font", "json", "html", "material", "model", "render", "script", "shader", "sprite",
* "template", "text", "texture", "textureatlas"]
*
* @type {("animation"|"audio"|"binary"|"container"|"cubemap"|"css"|"font"|"json"|"html"|"material"|"model"|"render"|"script"|"shader"|"sprite"|"template"|"text"|"texture"|"textureatlas")}
*/
this.type = type;

/**
* Asset tags. Enables finding of assets by tags using the {@link AssetRegistry#findByTag} method.
*
* @type {Tags}
*/
this.tags = new Tags(this);

this._preload = false;

/**
* @type {AssetFile | null}
*/
this._file = null;
this._data = data || { };

/**
* Optional JSON data that contains the asset handler options.
*
* @type {object}
*/
this.options = options || { };

// 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 = {};

/**
* True if the asset has finished attempting to load the resource. It is not guaranteed
* that the resources are available as there could have been a network error.
*
* @type {boolean}
*/
this.loaded = false;

/**
* True if the resource is currently being loaded.
*
* @type {boolean}
*/
this.loading = false;

/**
* The asset registry that this Asset belongs to.
*
* @type {AssetRegistry|null}
*/
this.registry = null;
this._data = data;
this.options = options;

if (file) this.file = file;
}

/**
* Sets the asset id.
*
* @type {number}
*/
set id(value) {
this._id = value;
}

/**
* Gets the asset id.
*
* @type {number}
*/
get id() {
return this._id;
}

/**
* Sets the asset name.
*
Expand Down Expand Up @@ -374,7 +382,7 @@ class Asset extends EventHandler {
}

/**
* Sets the asset resource. For example, a {@link Texture} or a {@link Model}.
* Sets the asset resource. For example, a {@link StandardMaterial} or a {@link Texture}.
*
* @type {object}
*/
Expand Down Expand Up @@ -521,7 +529,7 @@ class Asset extends EventHandler {

/**
* Adds a replacement asset id for the specified locale. When the locale in
* {@link Application#i18n} changes then references to this asset will be replaced with the
* {@link AppBase#i18n} changes then references to this asset will be replaced with the
* specified asset id. (Currently only supported by the {@link ElementComponent}).
*
* @param {string} locale - The locale e.g. Ar-AR.
Expand Down