Mirror / foldkit-mirror

Defaults, codecs, and batching

Because Mirror derives its behavior from your application Schemas, three behaviors work out of the box:

Defaults

App.initial provides the default values (pass initial in the mirror config if App has none). Any field equal to its initial value is omitted from the URL or store—so { filter: 'all', page: 1 } yields /todos, and changing filter yields /todos?filter=active. Opt a field in to stay visible with keys: { page: { keep: true } }.

Codecs

Mirror chooses a compact text representation from each field's encoded type: strings stay plain text, numbers parse numerically, booleans serialize as true or false, and arrays or objects serialize as JSON. Override any key with a custom codec via keys: { tags: { codec: TagsAsCommaSeparatedText } }. Calling mirror.decode(keys) returns both valid decoded values and any issues for invalid inputs.

Batching and loop avoidance

Subscriptions track the encoded slice rather than every Model update, so changing multiple mirrored fields in one transition triggers one write, and transitions that keep the same encoded output skip writing altogether. When popstate reduces a URL back into the Model, the encoded slice already matches the URL, preventing feedback loops.

How state changes here

Normal edits use Message, update, and modifyFields like the rest of your app; the mirror watches and persists them. Restoration runs in reverse only through the writable projection declared up front, so only those linked fields can ever be written.