fix(user-settings): fix settings loading errors (#527)
* refactor(user): optimize detail fetching with custom repository implementation * refactor(fonts): fix font loading and improve custom font handling * refactor(user-data): update userSettings to use input() for better reactivity * refactor(fonts): improve font loading logic and improve readi ness state management again * refactor(user-data): simplify user fetching logic and improve batch loading * refactor(custom-fonts): improve font loading logic with run ID for better state management
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {computed, effect, inject, Injectable} from '@angular/core';
|
||||
import {computed, signal, effect, inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {lastValueFrom, Observable, tap} from 'rxjs';
|
||||
import {CustomFont} from '../model/custom-font.model';
|
||||
@@ -13,6 +13,7 @@ const CUSTOM_FONTS_QUERY_KEY = ['customFonts'] as const;
|
||||
})
|
||||
export class CustomFontService {
|
||||
private apiUrl = `${API_CONFIG.BASE_URL}/api/v1/custom-fonts`;
|
||||
private loadAllFontsRunId = 0;
|
||||
private loadedFonts = new Set<string>();
|
||||
private http = inject(HttpClient);
|
||||
private authService = inject(AuthService);
|
||||
@@ -27,11 +28,15 @@ export class CustomFontService {
|
||||
fonts = computed(() => this.fontsQuery.data() ?? []);
|
||||
isFontsLoading = computed(() => !!this.token() && this.fontsQuery.isPending());
|
||||
|
||||
private readonly isFontsReadyInternal = signal(false);
|
||||
isFontsReady = this.isFontsReadyInternal.asReadonly();
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
const token = this.token();
|
||||
if (token === null) {
|
||||
this.queryClient.removeQueries({queryKey: CUSTOM_FONTS_QUERY_KEY});
|
||||
this.isFontsReadyInternal.set(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -125,8 +130,29 @@ export class CustomFontService {
|
||||
}
|
||||
|
||||
async loadAllFonts(fonts: CustomFont[]): Promise<void> {
|
||||
const runId = ++this.loadAllFontsRunId;
|
||||
this.isFontsReadyInternal.set(false);
|
||||
|
||||
if (fonts.length === 0) {
|
||||
if (runId === this.loadAllFontsRunId) {
|
||||
this.isFontsReadyInternal.set(true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const loadPromises = fonts.map(font => this.loadFontFace(font));
|
||||
await Promise.allSettled(loadPromises);
|
||||
const results = await Promise.allSettled(loadPromises);
|
||||
|
||||
if (runId !== this.loadAllFontsRunId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasFailures = results.some(r => r.status === 'rejected');
|
||||
if (hasFailures) {
|
||||
const failures = results.filter(r => r.status === 'rejected');
|
||||
console.error(`Failed to load ${failures.length} fonts out of ${fonts.length}`);
|
||||
}
|
||||
this.isFontsReadyInternal.set(true);
|
||||
}
|
||||
|
||||
isFontLoaded(fontName: string): boolean {
|
||||
|
||||
Reference in New Issue
Block a user