nitro-imager/src/app/avatar/AvatarAssetDownloadLibrary.ts
2021-09-09 03:14:44 -04:00

70 lines
1.8 KiB
TypeScript

import { IAssetManager } from '../../core';
export class AvatarAssetDownloadLibrary
{
private static NOT_LOADED: number = 0;
private static LOADING: number = 1;
private static LOADED: number = 2;
private _state: number;
private _libraryName: string;
private _revision: string;
private _downloadUrl: string;
private _assets: IAssetManager;
constructor(id: string, revision: string, assets: IAssetManager, assetUrl: string)
{
this._state = AvatarAssetDownloadLibrary.NOT_LOADED;
this._libraryName = id;
this._revision = revision;
this._downloadUrl = assetUrl;
this._assets = assets;
this._downloadUrl = this._downloadUrl.replace(/%libname%/gi, this._libraryName);
this._downloadUrl = this._downloadUrl.replace(/%revision%/gi, this._revision);
this.checkIfAssetLoaded();
}
private checkIfAssetLoaded(): boolean
{
if(this._state === AvatarAssetDownloadLibrary.LOADED) return true;
const asset = this._assets.getCollection(this._libraryName);
if(asset)
{
this._state = AvatarAssetDownloadLibrary.LOADED;
return true;
}
return false;
}
public async downloadAsset(): Promise<boolean>
{
if(!this._assets || (this._state === AvatarAssetDownloadLibrary.LOADING)) return false;
if(this.checkIfAssetLoaded()) return false;
this._state = AvatarAssetDownloadLibrary.LOADING;
if(!await this._assets.downloadAsset(this._downloadUrl)) return false;
this._state = AvatarAssetDownloadLibrary.LOADED;
return true;
}
public get libraryName(): string
{
return this._libraryName;
}
public get isLoaded(): boolean
{
return (this._state === AvatarAssetDownloadLibrary.LOADED);
}
}