From b602b5022b2f4ece104524e4eb552f008c2fd144 Mon Sep 17 00:00:00 2001 From: Ilia Mashkov Date: Fri, 3 Apr 2026 12:25:38 +0300 Subject: [PATCH] chore(appliedFontsStore): move the FontLoadRequestConfig type and other types from appliedFontsStore into types directory --- src/entities/Font/model/index.ts | 3 +- .../appliedFontsStore.svelte.ts | 37 ++++--------------- src/entities/Font/model/store/index.ts | 5 +-- src/entities/Font/model/types/index.ts | 2 + .../Font/model/types/store/appliedFonts.ts | 30 +++++++++++++++ .../ui/FontVirtualList/FontVirtualList.svelte | 4 +- 6 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 src/entities/Font/model/types/store/appliedFonts.ts diff --git a/src/entities/Font/model/index.ts b/src/entities/Font/model/index.ts index 8ec6e69..90eb6e3 100644 --- a/src/entities/Font/model/index.ts +++ b/src/entities/Font/model/index.ts @@ -8,6 +8,8 @@ export type { FontFeatures, FontFiles, FontItem, + FontLoadRequestConfig, + FontLoadStatus, FontMetadata, FontProvider, // Fontshare API types @@ -37,7 +39,6 @@ export type { export { appliedFontsManager, createUnifiedFontStore, - type FontConfigRequest, type UnifiedFontStore, unifiedFontStore, } from './store'; diff --git a/src/entities/Font/model/store/appliedFontsStore/appliedFontsStore.svelte.ts b/src/entities/Font/model/store/appliedFontsStore/appliedFontsStore.svelte.ts index 903122e..ef2bacb 100644 --- a/src/entities/Font/model/store/appliedFontsStore/appliedFontsStore.svelte.ts +++ b/src/entities/Font/model/store/appliedFontsStore/appliedFontsStore.svelte.ts @@ -1,36 +1,13 @@ import { SvelteMap } from 'svelte/reactivity'; +import type { + FontLoadRequestConfig, + FontLoadStatus, +} from '../../types'; import { getEffectiveConcurrency, yieldToMainThread, } from './utils'; -/** Loading state of a font. Failed loads may be retried up to MAX_RETRIES. */ -export type FontStatus = 'loading' | 'loaded' | 'error'; - -/** Configuration for a font load request. */ -export interface FontConfigRequest { - /** - * Unique identifier for the font (e.g., "lato", "roboto"). - */ - id: string; - /** - * Actual font family name recognized by the browser (e.g., "Lato", "Roboto"). - */ - name: string; - /** - * URL pointing to the font file (typically .ttf or .woff2). - */ - url: string; - /** - * Numeric weight (100-900). Variable fonts load once per ID regardless of weight. - */ - weight: number; - /** - * Variable fonts load once per ID; static fonts load per weight. - */ - isVariable?: boolean; -} - /** * Manages web font loading with caching, adaptive concurrency, and automatic cleanup. * @@ -62,7 +39,7 @@ export class AppliedFontsManager { #usageTracker = new Map(); // Fonts queued for loading by `touch()`, processed by `#processQueue()` - #queue = new Map(); + #queue = new Map(); // Handle for scheduled queue processing (requestIdleCallback or setTimeout) #timeoutId: ReturnType | null = null; @@ -94,7 +71,7 @@ export class AppliedFontsManager { readonly #CACHE_NAME = 'font-cache-v1'; // Versioned for future invalidation // Reactive status map for Svelte components to track font states - statuses = new SvelteMap(); + statuses = new SvelteMap(); // Starts periodic cleanup timer (browser-only). constructor() { @@ -114,7 +91,7 @@ export class AppliedFontsManager { * Retry behavior: 'loaded' and 'loading' fonts are skipped; 'error' fonts retry if count < MAX_RETRIES. * Scheduling: Prefers requestIdleCallback (150ms timeout), falls back to setTimeout(16ms). */ - touch(configs: FontConfigRequest[]) { + touch(configs: FontLoadRequestConfig[]) { if (this.#abortController.signal.aborted) return; const now = Date.now(); diff --git a/src/entities/Font/model/store/index.ts b/src/entities/Font/model/store/index.ts index c110ee4..0e2e684 100644 --- a/src/entities/Font/model/store/index.ts +++ b/src/entities/Font/model/store/index.ts @@ -14,7 +14,4 @@ export { } from './unifiedFontStore.svelte'; // Applied fonts manager (CSS loading - unchanged) -export { - appliedFontsManager, - type FontConfigRequest, -} from './appliedFontsStore/appliedFontsStore.svelte'; +export { appliedFontsManager } from './appliedFontsStore/appliedFontsStore.svelte'; diff --git a/src/entities/Font/model/types/index.ts b/src/entities/Font/model/types/index.ts index 5dfa1c9..b216577 100644 --- a/src/entities/Font/model/types/index.ts +++ b/src/entities/Font/model/types/index.ts @@ -56,3 +56,5 @@ export type { FontCollectionSort, FontCollectionState, } from './store'; + +export * from './store/appliedFonts'; diff --git a/src/entities/Font/model/types/store/appliedFonts.ts b/src/entities/Font/model/types/store/appliedFonts.ts new file mode 100644 index 0000000..a2bae5b --- /dev/null +++ b/src/entities/Font/model/types/store/appliedFonts.ts @@ -0,0 +1,30 @@ +/** + * Configuration for a font load request. + */ +export interface FontLoadRequestConfig { + /** + * Unique identifier for the font (e.g., "lato", "roboto"). + */ + id: string; + /** + * Actual font family name recognized by the browser (e.g., "Lato", "Roboto"). + */ + name: string; + /** + * URL pointing to the font file (typically .ttf or .woff2). + */ + url: string; + /** + * Numeric weight (100-900). Variable fonts load once per ID regardless of weight. + */ + weight: number; + /** + * Variable fonts load once per ID; static fonts load per weight. + */ + isVariable?: boolean; +} + +/** + * Loading state of a font. + */ +export type FontLoadStatus = 'loading' | 'loaded' | 'error'; diff --git a/src/entities/Font/ui/FontVirtualList/FontVirtualList.svelte b/src/entities/Font/ui/FontVirtualList/FontVirtualList.svelte index ba32dbd..9277a38 100644 --- a/src/entities/Font/ui/FontVirtualList/FontVirtualList.svelte +++ b/src/entities/Font/ui/FontVirtualList/FontVirtualList.svelte @@ -15,7 +15,7 @@ import type { import { fade } from 'svelte/transition'; import { getFontUrl } from '../../lib'; import { - type FontConfigRequest, + type FontLoadRequestConfig, type UnifiedFont, appliedFontsManager, unifiedFontStore, @@ -54,7 +54,7 @@ const isLoading = $derived( ); function handleInternalVisibleChange(visibleItems: UnifiedFont[]) { - const configs: FontConfigRequest[] = []; + const configs: FontLoadRequestConfig[] = []; visibleItems.forEach(item => { const url = getFontUrl(item, weight);