- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
import type { LoadAsset } from './loader';
import type { Loader } from './loader/Loader';
/**
* Quietly Loads assets in the background.
* @memberof PIXI
*/
export class BackgroundLoader
{
/** Whether or not the loader should continue loading. */
private _isActive: boolean;
/** Assets to load. */
private readonly _assetList: LoadAsset[];
/** Whether or not the loader is loading. */
private _isLoading: boolean;
/** Number of assets to load at a time. */
private readonly _maxConcurrent: number;
/** Should the loader log to the console. */
public verbose: boolean;
private readonly _loader: Loader;
/**
* @param loader
* @param verbose - should the loader log to the console
*/
constructor(loader: Loader, verbose = false)
{
this._loader = loader;
this._assetList = [];
this._isLoading = false;
this._maxConcurrent = 1;
this.verbose = verbose;
}
/**
* Adds an array of assets to load.
* @param assetUrls - assets to load
*/
public add(assetUrls: LoadAsset[]): void
{
assetUrls.forEach((a) =>
{
this._assetList.push(a);
});
// eslint-disable-next-line no-console
if (this.verbose)console.log('[BackgroundLoader] assets: ', this._assetList);
if (this._isActive && !this._isLoading)
{
this._next();
}
}
/**
* Loads the next set of assets. Will try to load as many assets as it can at the same time.
*
* The max assets it will try to load at one time will be 4.
*/
private async _next(): Promise<void>
{
if (this._assetList.length && this._isActive)
{
this._isLoading = true;
const toLoad = [];
const toLoadAmount = Math.min(this._assetList.length, this._maxConcurrent);
for (let i = 0; i < toLoadAmount; i++)
{
toLoad.push(this._assetList.pop());
}
await this._loader.load(toLoad);
this._isLoading = false;
this._next();
}
}
/**
* @returns whether the class is active
*/
get active(): boolean
{
return this._isActive;
}
/** Activate/Deactivate the loading. If set to true then it will immediately continue to load the next asset. */
set active(value: boolean)
{
if (this._isActive === value) return;
this._isActive = value;
if (value && !this._isLoading)
{
this._next();
}
}
}