chore(appliedFontsStore): move FontBufferCache, FontEvicionPolicy and FontLoadQueue to appliedFontsStore/utils
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
/** @vitest-environment jsdom */
|
/** @vitest-environment jsdom */
|
||||||
import { AppliedFontsManager } from './appliedFontsStore.svelte';
|
import { AppliedFontsManager } from './appliedFontsStore.svelte';
|
||||||
import { FontFetchError } from './errors';
|
import { FontFetchError } from './errors';
|
||||||
import { FontEvictionPolicy } from './fontEvictionPolicy/FontEvictionPolicy';
|
import { FontEvictionPolicy } from './utils/fontEvictionPolicy/FontEvictionPolicy';
|
||||||
|
|
||||||
// ── Fake collaborators ────────────────────────────────────────────────────────
|
// ── Fake collaborators ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
@@ -7,15 +7,15 @@ import {
|
|||||||
FontFetchError,
|
FontFetchError,
|
||||||
FontParseError,
|
FontParseError,
|
||||||
} from './errors';
|
} from './errors';
|
||||||
import { FontBufferCache } from './fontBufferCache/FontBufferCache';
|
|
||||||
import { FontEvictionPolicy } from './fontEvictionPolicy/FontEvictionPolicy';
|
|
||||||
import { FontLoadQueue } from './fontLoadQueue/FontLoadQueue';
|
|
||||||
import {
|
import {
|
||||||
generateFontKey,
|
generateFontKey,
|
||||||
getEffectiveConcurrency,
|
getEffectiveConcurrency,
|
||||||
loadFont,
|
loadFont,
|
||||||
yieldToMainThread,
|
yieldToMainThread,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
import { FontBufferCache } from './utils/fontBufferCache/FontBufferCache';
|
||||||
|
import { FontEvictionPolicy } from './utils/fontEvictionPolicy/FontEvictionPolicy';
|
||||||
|
import { FontLoadQueue } from './utils/fontLoadQueue/FontLoadQueue';
|
||||||
|
|
||||||
interface AppliedFontsManagerDeps {
|
interface AppliedFontsManagerDeps {
|
||||||
cache?: FontBufferCache;
|
cache?: FontBufferCache;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/** @vitest-environment jsdom */
|
/** @vitest-environment jsdom */
|
||||||
import { FontFetchError } from '../errors';
|
import { FontFetchError } from '../../errors';
|
||||||
import { FontBufferCache } from './FontBufferCache';
|
import { FontBufferCache } from './FontBufferCache';
|
||||||
|
|
||||||
const makeBuffer = () => new ArrayBuffer(8);
|
const makeBuffer = () => new ArrayBuffer(8);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { FontFetchError } from '../errors';
|
import { FontFetchError } from '../../errors';
|
||||||
|
|
||||||
type Fetcher = (url: string, init?: RequestInit) => Promise<Response>;
|
type Fetcher = (url: string, init?: RequestInit) => Promise<Response>;
|
||||||
|
|
||||||
@@ -40,7 +40,9 @@ export class FontBufferCache {
|
|||||||
async get(url: string, signal?: AbortSignal): Promise<ArrayBuffer> {
|
async get(url: string, signal?: AbortSignal): Promise<ArrayBuffer> {
|
||||||
// Tier 1: in-memory (fastest, no I/O)
|
// Tier 1: in-memory (fastest, no I/O)
|
||||||
const inMemory = this.#buffersByUrl.get(url);
|
const inMemory = this.#buffersByUrl.get(url);
|
||||||
if (inMemory) return inMemory;
|
if (inMemory) {
|
||||||
|
return inMemory;
|
||||||
|
}
|
||||||
|
|
||||||
// Tier 2: Cache API
|
// Tier 2: Cache API
|
||||||
try {
|
try {
|
||||||
@@ -48,8 +48,12 @@ export class FontEvictionPolicy {
|
|||||||
*/
|
*/
|
||||||
shouldEvict(key: string, now: number): boolean {
|
shouldEvict(key: string, now: number): boolean {
|
||||||
const lastUsed = this.#usageTracker.get(key);
|
const lastUsed = this.#usageTracker.get(key);
|
||||||
if (lastUsed === undefined) return false;
|
if (lastUsed === undefined) {
|
||||||
if (this.#pinnedFonts.has(key)) return false;
|
return false;
|
||||||
|
}
|
||||||
|
if (this.#pinnedFonts.has(key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return now - lastUsed >= this.#TTL;
|
return now - lastUsed >= this.#TTL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { FontLoadRequestConfig } from '../../../types';
|
import type { FontLoadRequestConfig } from '../../../../types';
|
||||||
import { FontLoadQueue } from './FontLoadQueue';
|
import { FontLoadQueue } from './FontLoadQueue';
|
||||||
|
|
||||||
const config = (id: string): FontLoadRequestConfig => ({
|
const config = (id: string): FontLoadRequestConfig => ({
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { FontLoadRequestConfig } from '../../../types';
|
import type { FontLoadRequestConfig } from '../../../../types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages the font load queue and per-font retry counts.
|
* Manages the font load queue and per-font retry counts.
|
||||||
@@ -17,7 +17,9 @@ export class FontLoadQueue {
|
|||||||
* @returns `true` if the key was newly enqueued, `false` if it was already present.
|
* @returns `true` if the key was newly enqueued, `false` if it was already present.
|
||||||
*/
|
*/
|
||||||
enqueue(key: string, config: FontLoadRequestConfig): boolean {
|
enqueue(key: string, config: FontLoadRequestConfig): boolean {
|
||||||
if (this.#queue.has(key)) return false;
|
if (this.#queue.has(key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
this.#queue.set(key, config);
|
this.#queue.set(key, config);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user