diff --git a/src/shared/lib/helpers/createEntityStore/createEntityStore.svelte.ts b/src/shared/lib/helpers/createEntityStore/createEntityStore.svelte.ts new file mode 100644 index 0000000..28afcc9 --- /dev/null +++ b/src/shared/lib/helpers/createEntityStore/createEntityStore.svelte.ts @@ -0,0 +1,80 @@ +import { SvelteMap } from 'svelte/reactivity'; + +export interface Entity { + id: string; +} + +/** + * Svelte 5 Entity Store + * Uses SvelteMap for O(1) lookups and granular reactivity. + */ +export class EntityStore { + // SvelteMap is a reactive version of the native Map + #entities = new SvelteMap(); + + constructor(initialEntities: T[] = []) { + this.setAll(initialEntities); + } + + // --- Selectors (Equivalent to Selectors) --- + + /** Get all entities as an array */ + get all() { + return Array.from(this.#entities.values()); + } + + /** Select a single entity by ID */ + getById(id: string) { + return this.#entities.get(id); + } + + /** Select multiple entities by IDs */ + getByIds(ids: string[]) { + return ids.map(id => this.#entities.get(id)).filter((e): e is T => !!e); + } + + // --- Actions (CRUD) --- + + addOne(entity: T) { + this.#entities.set(entity.id, entity); + } + + addMany(entities: T[]) { + entities.forEach(e => this.addOne(e)); + } + + updateOne(id: string, changes: Partial) { + const entity = this.#entities.get(id); + if (entity) { + // In Svelte 5, updating the object property directly is reactive + // if the object itself was made reactive, but here we replace + // the reference to ensure top-level map triggers. + this.#entities.set(id, { ...entity, ...changes }); + } + } + + removeOne(id: string) { + this.#entities.delete(id); + } + + removeMany(ids: string[]) { + ids.forEach(id => this.#entities.delete(id)); + } + + setAll(entities: T[]) { + this.#entities.clear(); + this.addMany(entities); + } + + has(id: string) { + return this.#entities.has(id); + } + + clear() { + this.#entities.clear(); + } +} + +export function createEntityStore(initialEntities: T[] = []) { + return new EntityStore(initialEntities); +}