UNPKG

oxygen-core

Version:

Oxygen game engine (Xenon Core for browsers)

643 lines (581 loc) 33.9 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../../"> <title data-ice="title">src/systems/AssetSystem/index.js | oxygen-core</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> <meta name="description" content="Oxygen game engine (Xenon Core for browsers)"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="oxygen-core"><meta property="twitter:description" content="Oxygen game engine (Xenon Core for browsers)"></head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> <a style="position:relative; top:3px;" href="https://github.com/PsichiX/Oxygen"><img width="20px" src="./image/github.png"></a></header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/index.js~EventsController.html">EventsController</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-lazyInitialization">lazyInitialization</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#asset-loaders">asset-loaders</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/AssemblyAsset.js~AssemblyAsset.html">AssemblyAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/AtlasAsset.js~AtlasAsset.html">AtlasAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/BinaryAsset.js~BinaryAsset.html">BinaryAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/FontAsset.js~FontAsset.html">FontAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/ImageAsset.js~ImageAsset.html">ImageAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/JSONAsset.js~JSONAsset.html">JSONAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/MusicAsset.js~MusicAsset.html">MusicAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/PackAsset.js~PackAsset.html">PackAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/ParticleSystemAsset.js~ParticleSystemAsset.html">ParticleSystemAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/PostprocessRackEffectAsset.js~PostprocessRackEffectAsset.html">PostprocessRackEffectAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SVGAsset.js~SVGAsset.html">SVGAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SceneAsset.js~SceneAsset.html">SceneAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SetAsset.js~SetAsset.html">SetAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/ShaderAsset.js~ShaderAsset.html">ShaderAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SkeletonAsset.js~SkeletonAsset.html">SkeletonAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SoundAsset.js~SoundAsset.html">SoundAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/TextAsset.js~TextAsset.html">TextAsset</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#components">components</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/AtlasSprite.js~AtlasSprite.html">AtlasSprite</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Camera.js~Camera.html">Camera</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Camera.js~PostprocessPass.html">PostprocessPass</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Camera2D.js~Camera2D.html">Camera2D</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/CameraDirector2D.js~CameraDirector2D.html">CameraDirector2D</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/CircleShape.js~CircleShape.html">CircleShape</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Container.js~Container.html">Container</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/DeferredRenderer.js~DeferredPipeline.html">DeferredPipeline</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/DeferredRenderer.js~DeferredRenderer.html">DeferredRenderer</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/GestureListener.js~GestureListener.html">GestureListener</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/InputHandler.js~InputHandler.html">InputHandler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/MultipassRenderer.js~MultipassPipeline.html">MultipassPipeline</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/MultipassRenderer.js~MultipassRenderer.html">MultipassRenderer</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Particles.js~Particles.html">Particles</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PhysicsBody.js~PhysicsBody.html">PhysicsBody</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PhysicsWorld.js~PhysicsWorld.html">PhysicsWorld</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PolygonShape.js~PolygonShape.html">PolygonShape</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PostprocessRack.js~PostprocessRack.html">PostprocessRack</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PostprocessRack.js~PostprocessRackPass.html">PostprocessRackPass</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PostprocessRack.js~PostprocessRackRawEffectPass.html">PostprocessRackRawEffectPass</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PrefabInstance.js~PrefabInstance.html">PrefabInstance</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/RectangleRenderer.js~RectangleRenderer.html">RectangleRenderer</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/RectangleShape.js~RectangleShape.html">RectangleShape</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Script.js~Script.html">Script</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Shape.js~Shape.html">Shape</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Skeleton.js~Skeleton.html">Skeleton</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/SortedActions.js~SortedActions.html">SortedActions</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Sprite.js~Sprite.html">Sprite</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/TextRenderer.js~TextRenderer.html">TextRenderer</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/UiLayout.js~UiLayout.html">UiLayout</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/UiSprite.js~UiSprite.html">UiSprite</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/VerticesRenderer.js~VerticesRenderer.html">VerticesRenderer</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#systems">systems</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/AssemblySystem.js~AssemblySystem.html">AssemblySystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/AudioSystem.js~AudioSystem.html">AudioSystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/InputSystem.js~InputSystem.html">InputSystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~Command.html">Command</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~Pipeline.html">Pipeline</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~RenderFullscreenCommand.html">RenderFullscreenCommand</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~RenderSystem.html">RenderSystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~RenderTargetWrapper.html">RenderTargetWrapper</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/StorageSystem.js~StorageSystem.html">StorageSystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/System.js~System.html">System</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#systems-assetsystem">systems/AssetSystem</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/AssetSystem/Asset.js~Asset.html">Asset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/AssetSystem/index.js~AssetSystem.html">AssetSystem</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#systems-entitysystem">systems/EntitySystem</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/EntitySystem/Component.js~Component.html">Component</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/EntitySystem/Entity.js~Entity.html">Entity</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/EntitySystem/index.js~EntitySystem.html">EntitySystem</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#utils">utils</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/Events.js~Events.html">Events</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-angleDifference">angleDifference</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-bezierCubic">bezierCubic</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-convertGlobalPointToLocalPoint">convertGlobalPointToLocalPoint</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-convertLocalPointToGlobalPoint">convertLocalPointToGlobalPoint</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-findMapKeyOfValue">findMapKeyOfValue</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getMipmapScale">getMipmapScale</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getPOT">getPOT</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isGlobalPointInGlobalBoundingBox">isGlobalPointInGlobalBoundingBox</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isLocalPointInLocalBoundingBox">isLocalPointInLocalBoundingBox</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isPOT">isPOT</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-propsEnumStringify">propsEnumStringify</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-stringToRGBA">stringToRGBA</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-waitForSeconds">waitForSeconds</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Box2D">Box2D</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">src/systems/AssetSystem/index.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import System from &apos;../System&apos;; import Asset from &apos;./Asset&apos;; import Events from &apos;../../utils/Events&apos;; import parser from &apos;../../utils/jsonParser&apos;; export { Asset }; const _pathRegex = /(\w+)(\:\/\/)(.*)/; /** * Assets database and loader. * * @example * const system = new AssetSystem(&apos;assets/&apos;, { cache: &apos;no-store&apos; }, AssetSystem.fetchArrayView); */ export default class AssetSystem extends System { /** * Default browser fetch mechanism. * * @param {*} args - Fetch engine parameters. * * @return {Promise} Promise that fetches file. */ static fetch(...args) { return fetch(...args); } /** * Custom fetch mechanism that loads data from array view. * * @param {ArrayBufferView} view - Data array buffer view. * @param {string} path - Asset path. * @param {*} options - Fetch engine options. * @param {Function} fallbackEngine - Fallback fetch engine. * * @return {Promise} Promise that fetches file from array view. */ static fetchArrayView(view, path, options = {}, fallbackEngine = null) { if (!!view &amp;&amp; !ArrayBuffer.isView(view)) { throw new Error(&apos;`view` is not type of ArrayView!&apos;); } if (typeof path !== &apos;string&apos;) { throw new Error(&apos;`path` is not type of String!&apos;); } if (!!fallbackEngine &amp;&amp; !(fallbackEngine instanceof Function)) { throw new Error(&apos;`fallbackEngine` is not type of Function!&apos;); } try { if (!view) { if (!!fallbackEngine) { return fallbackEngine(path, options); } else { return Promise.resolve(new Response(new Blob(), { status: 404 })); } } else { return Promise.resolve( new Response(new Blob([ view ]), { status: 200 }) ); } } catch(error) { return Promise.reject(error); } } /** * Web fetch mechanism generator that loads data from specified address. * * @param {string} address - Assets hosting address. * @param {Function} fallbackEngine - Fallback fetch engine. * * @return {Function} Function that fetches file from web. */ static makeFetchEngineWeb(address, fallbackEngine = AssetSystem.fetch) { if (typeof address !== &apos;string&apos;) { throw new Error(&apos;`address` is not type of String!&apos;); } return (path, options) =&gt; AssetSystem.fetch( `${address}/${path}`, options, fallbackEngine ); } /** @type {string} */ get pathPrefix() { return this._pathPrefix; } /** @type {*} */ get fetchOptions() { return this._fetchOptions; } /** @type {Function} */ get fetchEngine() { return this._fetchEngine; } /** @type {Function} */ set fetchEngine(value) { if (!(value instanceof Function)) { throw new Error(&apos;`value` is not type of Function!&apos;); } this._fetchEngine = value; } /** @type {Events} */ get events() { return this._events; } /** * Constructor. * * @param {string|null} pathPrefix - Path prefix used for every requested asset or null no path prefix. * @param {*|null} fetchOptions - Custom fetch options or null if default will be used. * @param {Function|null} fetchEngine - Custom fetch engine or null if default will be used. */ constructor(pathPrefix, fetchOptions, fetchEngine) { super(); if (!!pathPrefix &amp;&amp; typeof pathPrefix !== &apos;string&apos;) { throw new Error(&apos;`pathPrefix` is not type of String!&apos;); } this._pathPrefix = pathPrefix || &apos;&apos;; this._fetchOptions = fetchOptions || {}; this._fetchEngine = fetchEngine || AssetSystem.fetch; this._assets = new Map(); this._loaders = new Map(); this._events = new Events(); this._loaded = 0; this._toLoad = 0; } /** * Destructor (dispose internal resources and clear assets database). * * @example * system.dispose(); * system = null; */ dispose() { const { _assets, _loaders, _events } = this; for (const asset of _assets.values()) { asset.dispose(); } _assets.clear(); _loaders.clear(); _events.dispose(); } /** * Register assets loader under given protocol. * * @param {string} protocol - Assets loader protocol name. * @param {Function} assetConstructor - Asset factory. * * @example * system.registerProtocol(&apos;json&apos;, JSONAsset.factory); */ registerProtocol(protocol, assetConstructor) { if (typeof protocol !== &apos;string&apos;) { throw new Error(&apos;`protocol` is not type of String!&apos;); } if (!(assetConstructor instanceof Function)) { throw new Error(&apos;`assetConstructor` is not type of Function!&apos;); } const { _loaders } = this; if (_loaders.has(protocol)) { throw new Error(`There is already registered protocol: ${protocol}`); } _loaders.set(protocol, assetConstructor); } /** * Unregister given assets loader protocol. * * @param {string} protocol - Assets loader protocol name. * * @example * system.unregisterProtocol(&apos;json&apos;); */ unregisterProtocol(protocol) { if (typeof protocol !== &apos;string&apos;) { throw new Error(&apos;`protocol` is not type of String!&apos;); } if (!this._loaders.delete(protocol)) { throw new Error(`There is no registered protocol: ${protocol}`); } } /** * Get asset by it&apos;s full path. * * @param {string} path - Asset path (with protocol). * * @return {Asset|null} Asset instance if found or null otherwise. * * @example * const config = system.get(&apos;json://config.json&apos;); */ get(path) { if (typeof path !== &apos;string&apos;) { throw new Error(&apos;`path` is not type of String!&apos;); } return this._assets.get(path) || null; } /** * Load asset from given path. * * @param {string} path - Asset path (with protocol). * * @return {Promise} Promise of fetch engine loader. * * @example * system.load(&apos;json://config.json&apos;).then(asset =&gt; console.log(asset.data)); */ load(path) { this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); ++this._toLoad; return this._load(path) .then(asset =&gt; { ++this._loaded; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); --this._loaded; --this._toLoad; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); return asset; }) .catch(error =&gt; { ++this._loaded; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); --this._loaded; --this._toLoad; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); throw error; }); } /** * Load list of assets in sequence (one by one). * * @param {Array.&lt;string&gt;} paths - Array of assets paths. * * @return {Promise} Promise of fetch engine loader. * * @example * const list = [ &apos;json://config.json&apos;, &apos;text://hello.txt&apos; ]; * system.loadSequence(list).then(assets =&gt; console.log(assets.map(a =&gt; a.data))); */ async loadSequence(paths) { if (!(paths instanceof Array)) { throw new Error(&apos;`paths` is not type of Array!&apos;); } const result = []; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); this._toLoad += paths.length; for (let i = 0, c = paths.length; i &lt; c; ++i) { result.push( await this._load(paths[i]) .then(asset =&gt; { ++this._loaded; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); return asset; }) .catch(error =&gt; { ++this._loaded; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); throw error; }) ); } this._loaded -= paths.length; this._toLoad -= paths.length; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); return result; } /** * Load list of assets possibly all at the same time (asynchronously). * * @param {Array.&lt;string&gt;} paths - Array of assets paths. * * @return {Promise} Promise of fetch engine loader. * * @example * const list = [ &apos;json://config.json&apos;, &apos;text://hello.txt&apos; ]; * system.loadAll(list).then(assets =&gt; console.log(assets.map(a =&gt; a.data))); */ async loadAll(paths) { if (!(paths instanceof Array)) { throw new Error(&apos;`paths` is not type of Array!&apos;); } this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); this._toLoad += paths.length; return Promise.all(paths.map( path =&gt; this._load(path) .then(asset =&gt; { ++this._loaded; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); return asset; }) .catch(error =&gt; { ++this._loaded; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); throw error; }) )) .then(assets =&gt; { this._loaded -= paths.length; this._toLoad -= paths.length; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); return assets; }) .catch(error =&gt; { this._loaded -= paths.length; this._toLoad -= paths.length; this._events.trigger(&apos;progress&apos;, this._loaded, this._toLoad); throw error; }); } /** * Load asset from given path with specified fetch engine. * * @param {string} path - Asset path (with protocol). * @param {Function} fetchEngine - Fetch engine used to fetch asset. * * @return {Promise} Promise of fetch engine loader. * * @example * system.loadWithFetchEngine(&apos;json://config.json&apos;, AssetSystem.fetch).then(asset =&gt; console.log(asset.data)); */ async loadWithFetchEngine(path, fetchEngine) { const fe = this.fetchEngine; this.fetchEngine = fetchEngine; const result = await this.load(path); this.fetchEngine = fe; return result; } /** * Load list of assets in sequence (one by one) with specified fetch engine. * * @param {Array.&lt;string&gt;} paths - Array of assets paths. * @param {Function} fetchEngine - Fetch engine used to fetch assets. * * @return {Promise} Promise of fetch engine loader. * * @example * const list = [ &apos;json://config.json&apos;, &apos;text://hello.txt&apos; ]; * system.loadSequenceWithFetchEngine(list, AssetSystem.fetch).then(assets =&gt; console.log(assets.map(a =&gt; a.data))); */ async loadSequenceWithFetchEngine(paths, fetchEngine) { const fe = this.fetchEngine; this.fetchEngine = fetchEngine; const result = await this.loadSequence(paths); this.fetchEngine = fe; return result; } /** * Load list of assets possibly all at the same time (asynchronously) with specified fetch engine. * * @param {Array.&lt;string&gt;} paths - Array of assets paths. * @param {Function} fetchEngine - Fetch engine used to fetch assets. * * @return {Promise} Promise of fetch engine loader. * * @example * const list = [ &apos;json://config.json&apos;, &apos;text://hello.txt&apos; ]; * system.loadAllWithFetchEngine(list, AssetSystem.fetch).then(assets =&gt; console.log(assets.map(a =&gt; a.data))); */ async loadAllWithFetchEngine(paths, fetchEngine) { const fe = this.fetchEngine; this.fetchEngine = fetchEngine; const result = await this.loadAll(paths); this.fetchEngine = fe; return result; } /** * Unload asset by path and remove from database. * * @param {string} path - Asset path (with protocol). * * @example * system.unload(&apos;json://config.json&apos;); */ unload(path) { const { _assets } = this; const asset = _assets.get(path); if (!asset) { throw new Error(`Trying to unload non-existing asset: ${path}`); } this._events.trigger(&apos;unload&apos;, asset); asset.dispose(); _assets.delete(path); } /** * Unload all assets from paths list. * * @param {Array.&lt;string&gt;} paths - Array of assets paths. */ unloadAll(paths) { if (!(paths instanceof Array)) { throw new Error(&apos;`paths` is not type of Array!&apos;); } for (const path of paths) { this.unload(path); } } /** * @override */ onUnregister() { dispose(); } _load(path) { if (typeof path !== &apos;string&apos;) { throw new Error(&apos;`path` is not type of String!&apos;); } const found = path.lastIndexOf(&apos;|&apos;); if (found &lt; 0) { return this._loadPart(path); } else { const prefix = path.substr(0, found).trim(); const container = this._assets.get(prefix); if (!container) { throw new Error(`There is no loaded subassets container: ${prefix}`); } const oldEngine = this.fetchEngine; const postfix = path.substr(found + 1).trim(); this.fetchEngine = container.makeFetchEngine(oldEngine); return this._loadPart(postfix, path).finally(() =&gt; { this.fetchEngine = oldEngine; }); } } _loadPart(path, key = null) { if (typeof path !== &apos;string&apos;) { throw new Error(&apos;`path` is not type of String!&apos;); } let options = null; const found = path.indexOf(&apos;?&apos;); if (found &gt;= 0) { options = parser.parse(path.substr(found + 1)); path = path.substr(0, found); } if (!key) { key = path; } const result = _pathRegex.exec(path); if (!result) { throw new Error(`\`path\` does not conform asset path name rules: ${path}`); } const [ , protocol,, filename ] = result; const loader = this._loaders.get(protocol); if (!loader) { throw new Error(`There is no registered protocol: ${protocol}`); } const { _assets } = this; if (_assets.has(path)) { return Promise.resolve(_assets.get(path)); } const asset = loader(this, protocol, filename, options); if (!(asset instanceof Asset)) { throw new Error( `Cannot create asset for file: ${filename} of protocol: ${protocol}` ); } return asset.load().then(data =&gt; { this._assets.set(key, asset); this._events.trigger(&apos;load&apos;, asset); return data; }); } } </code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>