refactor: adopt createSingleton across the remaining stores
Replace the hand-rolled let _x / getX / __resetX boilerplate with the createSingleton helper in all nine remaining singleton stores. Exposed accessor names (getX, __resetX) are unchanged, so consumers and specs are unaffected. Teardown wired to each stores destroy() where it has one (fontCatalog, fontLifecycle, typography, availableFilter, theme, layout, scrollBreadcrumbs); sort and appliedFilter have no teardown. Also merges layoutStores duplicate $shared/lib imports.
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
DEFAULT_QUERY_STALE_TIME_MS,
|
||||
getQueryClient,
|
||||
} from '$shared/api/queryClient';
|
||||
import { createSingleton } from '$shared/lib/helpers/createSingleton/createSingleton';
|
||||
import {
|
||||
type InfiniteData,
|
||||
InfiniteQueryObserver,
|
||||
@@ -483,14 +484,12 @@ export class FontCatalogStore {
|
||||
}
|
||||
}
|
||||
|
||||
let _catalog: FontCatalogStore | undefined;
|
||||
const catalog = createSingleton(
|
||||
() => new FontCatalogStore({ limit: 50 }),
|
||||
instance => instance.destroy(),
|
||||
);
|
||||
|
||||
export function getFontCatalog(): FontCatalogStore {
|
||||
return (_catalog ??= new FontCatalogStore({ limit: 50 }));
|
||||
}
|
||||
export const getFontCatalog = catalog.get;
|
||||
|
||||
// test-only reset, so specs don't share a live observer
|
||||
export function __resetFontCatalog() {
|
||||
_catalog?.destroy();
|
||||
_catalog = undefined;
|
||||
}
|
||||
export const __resetFontCatalog = catalog.reset;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { createSingleton } from '$shared/lib/helpers/createSingleton/createSingleton';
|
||||
import { SvelteMap } from 'svelte/reactivity';
|
||||
import {
|
||||
type FontLoadRequestConfig,
|
||||
@@ -419,18 +420,16 @@ export class FontLifecycleManager {
|
||||
}
|
||||
}
|
||||
|
||||
let _fontLifecycleManager: FontLifecycleManager | undefined;
|
||||
|
||||
/**
|
||||
* App-wide font lifecycle manager, created on first access. Lazy so its
|
||||
* AbortController / FontFace bookkeeping isn't set up at module load.
|
||||
*/
|
||||
export function getFontLifecycleManager(): FontLifecycleManager {
|
||||
return (_fontLifecycleManager ??= new FontLifecycleManager());
|
||||
}
|
||||
const fontLifecycleManager = createSingleton(
|
||||
() => new FontLifecycleManager(),
|
||||
instance => instance.destroy(),
|
||||
);
|
||||
|
||||
export const getFontLifecycleManager = fontLifecycleManager.get;
|
||||
|
||||
// test-only reset, so specs don't share loaded-font/eviction state
|
||||
export function __resetFontLifecycleManager() {
|
||||
_fontLifecycleManager?.destroy();
|
||||
_fontLifecycleManager = undefined;
|
||||
}
|
||||
export const __resetFontLifecycleManager = fontLifecycleManager.reset;
|
||||
|
||||
Reference in New Issue
Block a user