managarten/apps/mana/apps/web/src/lib/data/scope/per-space-seeds.ts
Till JS c73f93ff12 refactor(workbench): central per-space-seeds registry + deterministic Home id
Replaces three race-prone seeding paths in `workbench-scenes.svelte.ts`
(count==0 init seed, replay-on-register seed, per-Space-change seed)
with a single registry pattern:

- `data/scope/per-space-seeds.ts` — registry of `Seeder` callbacks,
  fired by `setActiveSpace` whenever the active Space id changes.
  Errors are isolated per seeder so one module's bug can't block the
  others.
- `data/seeds/workbench-home.ts` — registers the workbench Home seeder.
  Uses the deterministic id `seed-home-${spaceId}` and a get-then-add
  guard, making the seed structurally idempotent: re-running for the
  same Space is a no-op regardless of timing. Replaces the old
  random-UUID + check-by-presence pattern that the seeding race could
  defeat.
- `data/seeds/index.ts` — side-effect barrel imported once at the top
  of `(app)/+layout.svelte` so every module's seeder is in the
  registry before the first `loadActiveSpace` fires.
- `active-space.svelte.ts` — both `setActiveSpace` and `loadActiveSpace`
  funnel through a private `applyActiveSpace` helper that calls
  `notifyHandlers` AND `runSpaceSeeds` in one place. No more divergent
  state-update paths.
- `workbench-scenes.svelte.ts` — `ensureSeedScene` and the two
  ad-hoc seed calls deleted. The store now only owns rendering and
  user-driven CRUD; seeding lives in the registry.

This is Schicht B + C of the broader cleanup plan
(docs/plans/workbench-seeding-cleanup.md). Schicht D-soft already
collapsed existing duplicates; this PR prevents new ones from forming.
The tests cover the registry contract (register/run/idempotency/error
isolation), the deterministic-id helper, and the seeder against an
isolated Dexie fixture (race-safety, no-overwrite of customised rows,
per-Space isolation).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 14:21:25 +02:00

60 lines
2 KiB
TypeScript

/**
* Per-Space Seeds Registry — single chokepoint for "what does every Space
* get pre-populated with on first visit?".
*
* Why a registry:
* - Replaces the three race-prone ad-hoc seeding paths in the scenes
* store (count==0 init seed + replay-on-register seed + per-Space-
* change seed) with one deterministic call from `setActiveSpace`.
* - Each seeder is responsible for its own idempotency (deterministic
* primary key + Dexie `put` upsert). The registry stays
* pattern-agnostic — it only iterates and isolates errors.
*
* Modules register themselves via side-effect imports (see
* `data/seeds/index.ts`). The +layout boot path imports the seeds
* barrel before `loadActiveSpace`, so by the time `setActiveSpace`
* fires, every seeder is already in the map.
*
* See docs/plans/workbench-seeding-cleanup.md §"Schicht B + C".
*/
type Seeder = (spaceId: string) => Promise<void>;
const seeders = new Map<string, Seeder>();
/**
* Register a per-Space seeder. The `name` is used purely for diagnostic
* logging — duplicate names overwrite, so re-registering the same
* seeder during HMR is safe.
*/
export function registerSpaceSeed(name: string, fn: Seeder): void {
seeders.set(name, fn);
}
/**
* Run every registered seeder against the given Space id. Errors are
* caught per-seeder and logged — a failure in one module's seed must
* not prevent the others from running.
*
* Fire-and-forget by convention: callers shouldn't block UI on seeds.
* The active-space switch propagates immediately; seeds catch up
* asynchronously.
*/
export async function runSpaceSeeds(spaceId: string): Promise<void> {
for (const [name, fn] of seeders) {
try {
await fn(spaceId);
} catch (err) {
console.error(`[per-space-seeds] '${name}' failed for space ${spaceId}:`, err);
}
}
}
/**
* Test-only: drop every registered seeder. Production code never needs
* this — vitest suites that exercise the registry use it to keep
* tests independent.
*/
export function __resetSpaceSeedsForTests(): void {
seeders.clear();
}