Primitives / foldkit-primitives

Rules every primitive keeps

These guarantees hold across every subpath:

  • Unchanged facts preserve Model identity. When a resize, start, or update produces the same value, the placement returns the parent Model unchanged so Foldkit skips re-rendering and createLazy views stay cached.
  • Initial values are safe defaults, not live reads. Slices start at safe values (matches: false, online: true, 0) for server rendering; the subscription updates them once mounted in a browser.
  • Missing platform APIs degrade gracefully. Streams stay empty, Commands yield failure Messages (CopyFailed, ShareFailed), and Mounts stay idle rather than throwing.
  • One placement observes once. Share a single placed field across components that read the same query; assemble refuses duplicate module-scoped resource tags such as sockets or camera streams.
  • Settled values require onOut. Primitives that emit outcomes (debounce, Throttle, Press, DismissLayer) require onOut when placed, or Bundle.ignore to opt out explicitly.
  • Timed work runs on Effect's clock. Timers, tweens, debounces, and idle detectors advance deterministically under TestClock.adjust.
  • Reduced motion uses the Motion service. Provide Motion.live in your runtime resources, or Motion.reduced / Motion.full in tests; full motion is the default when omitted.
  • DOM elements stay out of the Model. Focus and mutation reports carry serializable ids and metadata ({ tag, id }) rather than element references.

Testing a placement

Three patterns cover primitive tests without global mocks:

  1. Substitute the environment. Factories accept create (streams, devices, permissions, sockets) or request (camera) doubles so tests pass deterministic fakes.
  2. Advance time with TestClock.adjust. Yield once after forking a subscription so listeners register before advancing the clock.
  3. Bound stream reads. Collect test outputs with a bounded Stream.take so a stalled stream fails quickly with a clear message.

Composing with Surface and Mirror

Placed primitive state lives in your ordinary Model, so you can read it in a foldkit-surface projection, expose it to an agent, or persist it in the URL with Mirror.url. For state that must survive page reloads via KeyValueStore, pair it with Mirror.kv; primitives track live session facts only.

Limits

  • Built-in presets cover common media queries; custom queries pass args.
  • Virtual windows a single scroll axis; 2D virtualized grids are outside its scope.
  • Collaborative merging and offline outboxes belong in foldkit-sync.

Next