refactor(comparison): replace comparisonStore singleton with lazy getComparisonStore

Mirror the font-catalog change in ComparisonView: expose getComparisonStore()
(plus __resetComparisonStore for tests) instead of an eager comparisonStore
singleton, and consume getFontCatalog() internally. Update the model barrel and
all UI consumers (Sidebar, FontList, Header, Line, SliderArea); Character no
longer needs the store and reads everything from props.

Update both specs to the accessor: comparisonStore.test mocks getFontCatalog
with a writable stub (the real store's fonts is getter-only) and resets the
catalog between cases; Sidebar.svelte.test resolves the store via the accessor.

Also document Character's props.
This commit is contained in:
Ilia Mashkov
2026-06-01 17:25:05 +03:00
parent 10603d18bf
commit 1ad015aed6
10 changed files with 107 additions and 55 deletions
@@ -19,9 +19,10 @@ import {
getFontUrl,
} from '$entities/Font';
import {
type FontCatalogStore,
FontsByIdsStore,
fontCatalogStore,
fontLifecycleManager,
getFontCatalog,
} from '$entities/Font/model';
import { typographySettingsStore } from '$features/AdjustTypography/model';
import { createPersistentStore } from '$shared/lib';
@@ -98,10 +99,13 @@ export class ComparisonStore {
*/
#fontsByIdsStore: FontsByIdsStore;
#fontCatalog: FontCatalogStore;
constructor() {
// Synchronously seed the batch store with any IDs already in storage
const { fontAId, fontBId } = storage.value;
this.#fontsByIdsStore = new FontsByIdsStore(fontAId && fontBId ? [fontAId, fontBId] : []);
this.#fontCatalog = getFontCatalog();
$effect.root(() => {
// Sync batch results → fontA / fontB
@@ -173,7 +177,7 @@ export class ComparisonStore {
return;
}
const fonts = fontCatalogStore.fonts;
const fonts = this.#fontCatalog.fonts;
if (fonts.length < 2) {
return;
@@ -356,7 +360,14 @@ export class ComparisonStore {
}
}
/**
* Singleton comparison store instance
*/
export const comparisonStore = new ComparisonStore();
let _comparisonStore: ComparisonStore | undefined;
export function getComparisonStore(): ComparisonStore {
return (_comparisonStore ??= new ComparisonStore());
}
// test-only reset, so specs don't share a live observer
export function __resetComparisonStore() {
_comparisonStore?.resetAll();
_comparisonStore = undefined;
}