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) { 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(); } } /** * Creates a new EntityStore instance with the given initial entities. * @param initialEntities The initial entities to populate the store with. * @returns - A new EntityStore instance. */ export function createEntityStore(initialEntities: T[] = []) { return new EntityStore(initialEntities); }