Sync / Replicated state

2. Mount the replica in the browser

Open local storage, mount your Foldkit app over the replica, and start the transport sync loop:

ts
import { Effect, Scope } from 'effect'
import { Sync } from 'foldkit-sync'

const container = document.getElementById('app')!
const scope = Effect.runSync(Scope.make())
const replica = await Effect.runPromise(
  Sync.tabStorage('todos').pipe(
    Effect.flatMap(TodoSync.openReplica),
    Effect.provideService(Scope.Scope, scope),
  ),
)

const mounted = Sync.mount(App, TodoSync, {
  replica,
  container,
  view: (model, h) => ({
    title: 'Todos',
    body: h.ul([], model.todos.map(todo => h.li([], [todo.title]))),
  }),
  onPersistenceFailure: (model, error) =>
    modifyFields(model, { lastError: () => Option.some(error.message) }),
})

Effect.runFork(
  Effect.provide(replica.start, Sync.transport.socket({ url: 'wss://example.com/sync' })),
)

Sync.mount runs the standard Foldkit runtime: durable Messages update Model immediately and enqueue to the outbox, while replica.start exchanges pending edits with the server (retrying with backoff) and installs reconciled snapshots back into Model. See the Sync README and Sync runtime binding.