Skip to content

Commit

Permalink
Added possibility to handle progress during load (#143)
Browse files Browse the repository at this point in the history
* Added possibility to handle progress during load #142

* Fixes according pull-request comments
  • Loading branch information
CrazyFlasher authored Oct 2, 2023
1 parent 8311ddb commit c586edf
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/animate/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Texture } from '@pixi/core';
import { Spritesheet } from '@pixi/spritesheet';

type Complete = (instance: MovieClip | null) => void;
type Progress = (value: number) => void;
export interface LoadOptions
{
/**
Expand All @@ -18,6 +19,10 @@ export interface LoadOptions
* Callback for load completion.
*/
complete?: Complete;
/**
* Callback for load progress.
*/
progress?: Progress;
/**
* Base root directory
*/
Expand Down Expand Up @@ -94,6 +99,8 @@ export function load(scene: AnimateAsset, options: LoadOptions): void;
export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOptions): void
{
const complete: Complete = typeof optionsOrComplete === 'function' ? optionsOrComplete : optionsOrComplete?.complete;
const progress: Progress | undefined = typeof optionsOrComplete === 'function' ? undefined : optionsOrComplete?.progress;

let basePath = '';
let parent: Container = null;
let metadata: any;
Expand Down Expand Up @@ -143,6 +150,9 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt

if (assets && Object.keys(assets).length)
{
let totalAssets = 0;
let loadedAssets = 0;

const promises: Promise<any>[] = [];
// assetBaseDir can accept either with trailing slash or not

Expand All @@ -152,6 +162,8 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt
}
for (const id in assets)
{
if (progress) totalAssets++;

let data = null;

if (metadata)
Expand All @@ -169,9 +181,16 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt
}
promises.push(Assets.load({ alias: [id], src: basePath + assets[id], data }).then((loadedAsset) =>
{
if (progress)
{
loadedAssets++;

progress(loadedAssets / totalAssets);
}

if (!loadedAsset)
{
return; // not sure if this can evet happen
return; // not sure if this can ever happen
}
if (loadedAsset instanceof Spritesheet)
{
Expand Down

0 comments on commit c586edf

Please sign in to comment.