Skip to content

Extract a shared reactive Collection-view helper — 5x duplicated Yjs observer boilerplate #103

Description

@brylie

Problem

TableCollectionView.svelte, BoardCollectionView.svelte, CalendarCollectionView.svelte, FieldManagerDialog.svelte, and /table/[id]/+page.svelte each independently re-implement the same boilerplate for reading a live Collection out of Yjs:

  1. Call getCollectionView(doc, collectionId) (or getCollection) to get the current snapshot.
  2. Mirror pieces of it (schema, rows, and — since Make the Collection primary field and Board card title explicit #96primaryFieldKey) into local $state variables.
  3. Subscribe collectionsMap.observeDeep/recordsMap.observeDeep in onMount to re-run step 1–2 on every Yjs change.
  4. Unsubscribe on unmount.

This predates #96, but implementing #96's primaryFieldKey meant touching all five copies again to add one more mirrored field. Each copy is a small but exact duplicate of the others (see src/lib/components/BoardCollectionView.svelte's refresh()/onMount for the canonical shape), which is exactly the kind of drift risk this repo's service-layer/shared-projection conventions (docs/specifications/collection-views.md §3, service-layer.md) already try to avoid for other cross-cutting concerns.

Scope

  • Extract a shared reactive helper (e.g. useCollectionView(collectionId: () => string) in src/lib/data/ or src/lib/client/, using Svelte 5 runes) that encapsulates: initial read, Yjs observer subscription/cleanup, and exposes { schema, rows, primaryFieldKey, collection } as reactive state.
  • Migrate all five existing call sites (TableCollectionView, BoardCollectionView, CalendarCollectionView, FieldManagerDialog, /table/[id]/+page.svelte) to use it, deleting their individual refresh()/onMount observer boilerplate.
  • No behavior change — existing component tests should pass unmodified (or with only import/setup changes, not assertion changes).

Non-goals

  • Not a broader state-management rewrite (no store library, no global cache) — just deduplicating this one specific pattern.
  • Not changing what data is exposed (still schema/rows/primaryFieldKey) — a future PR can extend the helper if a sixth derived field shows up.

Done when

There is exactly one implementation of "read a Collection reactively from Yjs and keep it live," all five current call sites use it, and the existing test suite passes with no behavior change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions