refactor: extract #processFont and #scheduleProcessing from touch and #processQueue
This commit is contained in:
@@ -131,26 +131,31 @@ export class AppliedFontsManager {
|
|||||||
hasNewItems = true;
|
hasNewItems = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Schedule queue processing if we have new items and no existing timer
|
|
||||||
if (hasNewItems && !this.#timeoutId) {
|
if (hasNewItems && !this.#timeoutId) {
|
||||||
// Prefer requestIdleCallback for better performance (waits for browser idle)
|
this.#scheduleProcessing();
|
||||||
if (typeof requestIdleCallback !== 'undefined') {
|
|
||||||
this.#timeoutId = requestIdleCallback(
|
|
||||||
() => this.#processQueue(),
|
|
||||||
{ timeout: 150 },
|
|
||||||
) as unknown as ReturnType<typeof setTimeout>;
|
|
||||||
this.#pendingType = 'idle';
|
|
||||||
} else {
|
|
||||||
// Fallback to setTimeout with ~60fps timing
|
|
||||||
this.#timeoutId = setTimeout(() => this.#processQueue(), 16);
|
|
||||||
this.#pendingType = 'timeout';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedules `#processQueue()` via `requestIdleCallback` (150ms timeout) when available,
|
||||||
|
* falling back to `setTimeout(16ms)` for ~60fps timing.
|
||||||
|
*/
|
||||||
|
#scheduleProcessing(): void {
|
||||||
|
if (typeof requestIdleCallback !== 'undefined') {
|
||||||
|
this.#timeoutId = requestIdleCallback(
|
||||||
|
() => this.#processQueue(),
|
||||||
|
{ timeout: 150 },
|
||||||
|
) as unknown as ReturnType<typeof setTimeout>;
|
||||||
|
this.#pendingType = 'idle';
|
||||||
|
} else {
|
||||||
|
this.#timeoutId = setTimeout(() => this.#processQueue(), 16);
|
||||||
|
this.#pendingType = 'timeout';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns true if data-saver mode is enabled (defers non-critical weights). */
|
/** Returns true if data-saver mode is enabled (defers non-critical weights). */
|
||||||
#shouldDeferNonCritical(): boolean {
|
#shouldDeferNonCritical(): boolean {
|
||||||
return (navigator as any).connection?.saveData === true;
|
return (navigator as any).connection?.saveData === true;
|
||||||
@@ -229,19 +234,7 @@ export class AppliedFontsManager {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
await this.#processFont(key, config, buffer);
|
||||||
// Parse buffer into FontFace and register with document
|
|
||||||
const font = await loadFont(config, buffer);
|
|
||||||
this.#loadedFonts.set(key, font);
|
|
||||||
this.#urlByKey.set(key, config.url);
|
|
||||||
this.statuses.set(key, 'loaded');
|
|
||||||
} catch (e) {
|
|
||||||
if (e instanceof FontParseError) {
|
|
||||||
console.error(`Font parse failed: ${config.name}`, e);
|
|
||||||
this.statuses.set(key, 'error');
|
|
||||||
this.#queue.incrementRetry(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Yield to main thread if needed (prevents UI blocking)
|
// Yield to main thread if needed (prevents UI blocking)
|
||||||
// Chromium: use isInputPending() for optimal responsiveness
|
// Chromium: use isInputPending() for optimal responsiveness
|
||||||
@@ -257,6 +250,25 @@ export class AppliedFontsManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a fetched buffer into a {@link FontFace}, registers it with `document.fonts`,
|
||||||
|
* and updates reactive status. On failure, sets status to `'error'` and increments the retry count.
|
||||||
|
*/
|
||||||
|
async #processFont(key: string, config: FontLoadRequestConfig, buffer: ArrayBuffer): Promise<void> {
|
||||||
|
try {
|
||||||
|
const font = await loadFont(config, buffer);
|
||||||
|
this.#loadedFonts.set(key, font);
|
||||||
|
this.#urlByKey.set(key, config.url);
|
||||||
|
this.statuses.set(key, 'loaded');
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof FontParseError) {
|
||||||
|
console.error(`Font parse failed: ${config.name}`, e);
|
||||||
|
this.statuses.set(key, 'error');
|
||||||
|
this.#queue.incrementRetry(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Removes fonts unused within TTL (LRU-style cleanup). Runs every PURGE_INTERVAL. Pinned fonts are never evicted. */
|
/** Removes fonts unused within TTL (LRU-style cleanup). Runs every PURGE_INTERVAL. Pinned fonts are never evicted. */
|
||||||
#purgeUnused() {
|
#purgeUnused() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|||||||
Reference in New Issue
Block a user