Mirror / foldkit-mirror
Writing and persistence behavior
Writes are throttled by default to 50 ms for URL mirrors, 250 ms for key-value mirrors, and 0 ms for cookie mirrors. Rapid changes within that window coalesce into a single write via Foldkit's subscription restart behavior.
A key-value mirror stores one versioned JSON document under key, optionally isolated per user or tenant with scope:
Mirror.kv(App, {
key: 'todo/prefs',
scope: principal.userId,
fields: Projection.pick(App.model.sidebar, App.model.draft),
})Documents with another version or scope, or malformed JSON, are discarded and removed so stale preferences never crash the app. If you need to keep data from an older format, write an explicit importer or store under a new key instead of relying on decode failure. Store errors are absorbed, and returning every field to its default removes the stored document.
Diagnostics
Any store failure or oversized cookie is reported as a typed diagnostic through Diagnostics.Reporter (foldkit-surface), defaulting to Effect's logger:
_tag | level | When |
|---|---|---|
StoreUnread | warning | The store failed a read: the mirror restored nothing. |
StoreUnwritten | warning | The store failed a write, or the removal of a discarded document. |
StoreMalformed | warning | Key held invalid JSON or shape; wrong version/scope discards silently. |
CookieTooLarge | warning | Slice encoded past maxBytes, so browser write skipped; carries name, bytes, maxBytes. |
Each key-value diagnostic carries key and either error or reason. Redirect diagnostics in runtime resources via Layer.succeed(Diagnostics.Reporter, report), or capture them in tests with Diagnostics.collect().report. Invalid URL query values are left for mirror.decode(keys).issues rather than reported as diagnostics.
Contracts and Module
Every mirror exposes a contract of kind 'mirror'. Because the local application Model owns the field and the mirror only observes it, Module.manifest keeps the field listed as local with the mirror attached beside it.
A key-value mirror declares MirrorRestored in its contract, allowing Module.validate to catch a forgotten ...Mirror.messages spread. Each URL key may belong to only one URL mirror per application; declaring a duplicate key throws and names the conflicting mirror.