Skip to content

Commit

Permalink
feat(background): cache background urls
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Jun 28, 2023
1 parent 429fbf8 commit 6ee80ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, Input, OnChanges, OnInit } from '@angular/core';
import { BackgroundImageService } from '@services/backgroundimage.service';
import { AssetService } from '@services/asset.service';

@Component({
selector: 'app-background-art',
Expand All @@ -11,13 +11,13 @@ export class BackgroundArtComponent implements OnInit, OnChanges {

public bgUrl: any;

constructor(private backgroundImageService: BackgroundImageService) {}
constructor(private assetService: AssetService) {}

async ngOnInit() {
this.bgUrl = await this.backgroundImageService.getSafeImageUrl(this.sprite);
this.bgUrl = await this.assetService.getBackgroundUrl(this.sprite);
}

async ngOnChanges() {
this.bgUrl = await this.backgroundImageService.getSafeImageUrl(this.sprite);
this.bgUrl = await this.assetService.getBackgroundUrl(this.sprite);
}
}
32 changes: 21 additions & 11 deletions client/src/app/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class AssetService {
private maxPortraits = 0;
private maxBackgrounds = 0;

private backgroundUrls: Record<string, string> = {};
private spritesheetUrlsByQuality: Record<string, Record<string, string>> = {};

public get portraitCount(): number {
Expand All @@ -36,25 +37,30 @@ export class AssetService {

this.maxBackgrounds = manifestData.assets.backgrounds.length;

manifestData.assets.backgrounds.forEach(
async ({ name, path, hash }: any) => {
await Promise.all(
manifestData.assets.backgrounds.map(async ({ name, path, hash }: any) => {
const fullUrl = `${environment.assetsUrl}/${path}`;

const oldImage = await this.backgroundImageService.getImageDataById(
name,
);

if (!oldImage || oldImage.hash !== hash) {
this.backgroundImageService.fetchImage(fullUrl).subscribe((blob) => {
this.backgroundImageService.saveImageToDatabase(
name,
hash,
fullUrl,
blob,
);
});
const blob = await lastValueFrom(
this.imageService.fetchImage(fullUrl),
);

this.backgroundImageService.saveImageToDatabase(
name,
hash,
fullUrl,
blob,
);
}
},

const url = await this.backgroundImageService.getSafeImageUrl(name);
this.backgroundUrls[name] = url;
}),
);

const qualitiesAndSheets = [
Expand Down Expand Up @@ -99,4 +105,8 @@ export class AssetService {
public getSpritesheetUrl(name: string, quality: string): string {
return this.spritesheetUrlsByQuality[quality][name];
}

public getBackgroundUrl(name: string): string {
return this.backgroundUrls[name];
}
}

0 comments on commit 6ee80ac

Please sign in to comment.