{"version":3,"file":"BackgroundLoader.mjs","sources":["../src/BackgroundLoader.ts"],"sourcesContent":["import type { Loader } from './loader/Loader';\nimport type { ResolvedAsset } from './types';\n\n/**\n * Quietly Loads assets in the background.\n * @memberof PIXI\n */\nexport class BackgroundLoader\n{\n    /** Whether or not the loader should continue loading. */\n    private _isActive: boolean;\n\n    /** Assets to load. */\n    private readonly _assetList: ResolvedAsset[];\n\n    /** Whether or not the loader is loading. */\n    private _isLoading: boolean;\n\n    /** Number of assets to load at a time. */\n    private readonly _maxConcurrent: number;\n\n    /** Should the loader log to the console. */\n    public verbose: boolean;\n    private readonly _loader: Loader;\n\n    /**\n     * @param loader\n     * @param verbose - should the loader log to the console\n     */\n    constructor(loader: Loader, verbose = false)\n    {\n        this._loader = loader;\n        this._assetList = [];\n        this._isLoading = false;\n        this._maxConcurrent = 1;\n        this.verbose = verbose;\n    }\n\n    /**\n     * Adds an array of assets to load.\n     * @param assetUrls - assets to load\n     */\n    public add(assetUrls: ResolvedAsset[]): void\n    {\n        assetUrls.forEach((a) =>\n        {\n            this._assetList.push(a);\n        });\n\n        if (this.verbose)\n        {\n            // eslint-disable-next-line no-console\n            console.log('[BackgroundLoader] assets: ', this._assetList);\n        }\n\n        if (this._isActive && !this._isLoading)\n        {\n            this._next();\n        }\n    }\n\n    /**\n     * Loads the next set of assets. Will try to load as many assets as it can at the same time.\n     *\n     * The max assets it will try to load at one time will be 4.\n     */\n    private async _next(): Promise<void>\n    {\n        if (this._assetList.length && this._isActive)\n        {\n            this._isLoading = true;\n\n            const toLoad = [];\n\n            const toLoadAmount = Math.min(this._assetList.length, this._maxConcurrent);\n\n            for (let i = 0; i < toLoadAmount; i++)\n            {\n                toLoad.push(this._assetList.pop());\n            }\n\n            await this._loader.load(toLoad);\n\n            this._isLoading = false;\n\n            this._next();\n        }\n    }\n\n    /**\n     * Activate/Deactivate the loading. If set to true then it will immediately continue to load the next asset.\n     * @returns whether the class is active\n     */\n    get active(): boolean\n    {\n        return this._isActive;\n    }\n\n    set active(value: boolean)\n    {\n        if (this._isActive === value) return;\n\n        this._isActive = value;\n\n        if (value && !this._isLoading)\n        {\n            this._next();\n        }\n    }\n}\n"],"names":[],"mappings":"AAOO,MAAM,iBACb;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBI,YAAY,QAAgB,UAAU,IACtC;AACI,SAAK,UAAU,QACf,KAAK,aAAa,CAAC,GACnB,KAAK,aAAa,IAClB,KAAK,iBAAiB,GACtB,KAAK,UAAU;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,IAAI,WACX;AACc,cAAA,QAAQ,CAAC,MACnB;AACS,WAAA,WAAW,KAAK,CAAC;AAAA,IAAA,CACzB,GAEG,KAAK,WAGL,QAAQ,IAAI,+BAA+B,KAAK,UAAU,GAG1D,KAAK,aAAa,CAAC,KAAK,cAExB,KAAK;EAEb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAc,QACd;AACI,QAAI,KAAK,WAAW,UAAU,KAAK,WACnC;AACI,WAAK,aAAa;AAEZ,YAAA,SAAS,CAAA,GAET,eAAe,KAAK,IAAI,KAAK,WAAW,QAAQ,KAAK,cAAc;AAEhE,eAAA,IAAI,GAAG,IAAI,cAAc;AAE9B,eAAO,KAAK,KAAK,WAAW,IAAK,CAAA;AAG/B,YAAA,KAAK,QAAQ,KAAK,MAAM,GAE9B,KAAK,aAAa,IAElB,KAAK,MAAM;AAAA,IACf;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,SACJ;AACI,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,IAAI,OAAO,OACX;AACQ,SAAK,cAAc,UAEvB,KAAK,YAAY,OAEb,SAAS,CAAC,KAAK,cAEf,KAAK,MAAM;AAAA,EAEnB;AACJ;"}