Skip to content

Commit

Permalink
[loaders] Improved typing & only load again when not allready loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
naranjamecanica committed Jul 30, 2020
1 parent 9ac154a commit f7f1933
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
13 changes: 9 additions & 4 deletions src/loaders/font-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@ export class FontLoader implements AssetLoaderInterface {
}

public prepareForLoad(): Promise<void> {
return new Promise(resolve => {
return new Promise((resolve) => {
this.loadedResolver = resolve;
});
}

public async load(): Promise<object | void> {
if (this.isLoading) {
public async load(): Promise<void> {
if (this.isLoading || this.isLoaded) {
Logger.error('Already loading...');
return;
}

if (this.isLoaded) {
Logger.info('Already loaded...');
this.loadedResolver();
return;
}

Expand All @@ -61,7 +66,7 @@ export class FontLoader implements AssetLoaderInterface {

try {
await this.loader.load();
} catch (error) {
} catch {
Logger.error(`Error loading '${this.options.fontFamilyName}'`);
}

Expand Down
16 changes: 12 additions & 4 deletions src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Logger = getLogger('loader');
type AssetData = { [key: string]: Texture } | SoundLibraryItem[] | SpineAsset | FontAsset | undefined;

export interface AssetLoaderInterface {
load(): Promise<object | void>;
load(): Promise<unknown | void>;
unload(): void;
prepareForLoad(): Promise<void>;
data: AssetData;
Expand Down Expand Up @@ -59,6 +59,7 @@ export class AssetLoader {
protected assets: LoaderAssets;
private numberLoaded = 0;
private isLoading = false;
private _isLoaded = false;
private options: LoaderOptions;
private assetsInited = false;
private loaders: AssetLoaderInterface[] = [];
Expand Down Expand Up @@ -96,7 +97,7 @@ export class AssetLoader {
return;
}

let loader: AssetLoaderInterface | undefined = undefined;
let loader: AssetLoaderInterface | undefined;

Logger.debug(`Adding asset of type '${asset.type}'...`);

Expand All @@ -121,10 +122,11 @@ export class AssetLoader {
}

public async load(): Promise<void> {
if (this.isLoading) {
if (this.isLoading || this.isLoaded) {
return;
}

this._isLoaded = false;
this.isLoading = true;
this.numberLoaded = 0;

Expand All @@ -150,6 +152,7 @@ export class AssetLoader {
// done loading
this.loaded.post();
this.isLoading = false;
this._isLoaded = true;
}

private async loadNext(): Promise<void> {
Expand Down Expand Up @@ -181,13 +184,18 @@ export class AssetLoader {
loader.unload();
}
this.isLoading = false;
this._isLoaded = false;
}

public get id(): string | undefined {
return this.options.id;
}

protected getLoaderData(_type: string, _pattern: object): AssetData {
public get isLoaded(): boolean {
return this._isLoaded;
}

protected getLoaderData(_type: string, _pattern: Record<string, unknown>): AssetData {
// haal alle loaders op van dit type
const typeLoaders = filter(this.loaders, { type: _type });

Expand Down
10 changes: 8 additions & 2 deletions src/loaders/sounds-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,23 @@ export class SoundsLoader implements AssetLoaderInterface {
}

public prepareForLoad(): Promise<void> {
return new Promise(resolve => {
return new Promise((resolve) => {
this._loadedResolver = resolve;
});
}

public async load(): Promise<object | void> {
public async load(): Promise<void> {
if (this.isLoading) {
Logger.error('Already loading...');
return;
}

if (this.isLoaded) {
Logger.info('Already loaded...');
this.resolveLoaded();
return;
}

// geluid uit?
if (CoreDebug.disableSounds()) {
this.resolveLoaded();
Expand Down
10 changes: 9 additions & 1 deletion src/loaders/spine-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,20 @@ export class SpineLoader implements AssetLoaderInterface {
});
}

public async load(): Promise<object | undefined> {
public async load(): Promise<SpineAsset | undefined> {
if (this.isLoading) {
Logger.error('Already loading...');

return;
}

if (this.isLoaded) {
Logger.info('Already loaded...');
this.loadedResolver(this._data);

return;
}

this.isLoaded = false;
this.isLoading = true;

Expand Down
12 changes: 9 additions & 3 deletions src/loaders/sprites-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface SpriteAsset {
}

export interface SpriteAssetInfo {
assets: object;
assets: Record<string, unknown>;
fileName: string;
numberOfParts: number;
type: string;
Expand Down Expand Up @@ -56,7 +56,7 @@ export class SpriteLoader implements AssetLoaderInterface {

this.loader = new Loader();
this.loader.onError.add((loader: Loader, resource: LoaderResource) => {
console.error('error loading', loader, resource);
Logger.error('error loading', loader, resource);
});

// this.loader.defaultQueryString = Settings.version ? Settings.version : '';
Expand Down Expand Up @@ -100,12 +100,18 @@ export class SpriteLoader implements AssetLoaderInterface {
});
}

public async load(): Promise<object | undefined> {
public async load(): Promise<{ [key: string]: Texture } | undefined> {
if (this.isLoading) {
Logger.error('Already loading...');
return;
}

if (this.isLoaded) {
Logger.info('Already loaded...');
this.loadedResolver(this.textures);
return;
}

this.isLoaded = false;
this.isLoading = true;

Expand Down
7 changes: 4 additions & 3 deletions src/tween/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class TweenMixin {
durationOrProperties: number | GSAPTweenVars,
propertiesOrSettings?: GSAPTweenVars,
settings?: GSAPTweenVars,
): { target: any; vars: object } {
): { target: any; vars: Record<string, unknown> } {
let target,
duration = 0,
properties = {};
Expand All @@ -39,7 +39,7 @@ export class TweenMixin {
}

// // fix rotation
const rotation = get(properties, 'rotation', undefined);
const rotation = get(properties, 'rotation');
if (typeof rotation === 'number') {
set(properties, 'rotation', rotation * (180 / Math.PI));
}
Expand Down Expand Up @@ -111,6 +111,7 @@ export class TweenMixin {
return tween;
}

// eslint-disable-next-line @typescript-eslint/ban-types
protected killTweens(_target?: object, _propertiesList?: string): void {
for (const tween of this.__tweens) {
tween.kill(_target, _propertiesList);
Expand All @@ -133,7 +134,7 @@ export class TweenMixin {
protected killTweenOf(_target?: GSAPTweenTarget, _vars?: GSAPTweenVars): void {
if (_target) {
// haal uit lijst
const tweensToKill = remove(this.__tweens, item => item.targets().includes(_target));
const tweensToKill = remove(this.__tweens, (item) => item.targets().includes(_target));

// kill
for (const tween of tweensToKill) {
Expand Down

0 comments on commit f7f1933

Please sign in to comment.