Sync / foldkit-sync

What to show the user

The three milestones each have a reader.

Visible is the Model. Nothing to do.

Saved is the persist. Between the edit showing and the persist finishing, a reload would lose what the user saw, so the browser asks before the page unloads (beforeunload) until every shown edit is stored; it usually takes milliseconds. onPersistenceFailure runs when it fails, after the edit has been reverted; the Model it returns is what the user sees next. The error names why: ReplayError for a Message that cannot be durable, StorageError for IndexedDB.

Committed is the replica's status:

ts
const status = Effect.runSync(replica.status)
// { pending, cursor, lastError: string | undefined, rejected: ReadonlyArray<Rejection>, refused, epoch }

pending above zero means edits the server has not confirmed; lastError is the last failed exchange, cleared by the next success; rejected lists recent edits the server refused, most recent first, each { opId, refusal, operation, message }, where refusal is an Option of why (a SyncRefusal, below), operation is the refused operation as it was sent, which the replica has dropped from the outbox, and message its Message encoded at the current version (see changing a durable Message), so a page reads what it changed from here. refused lists the operations this build cannot read at all, kept until replica.dismiss(opId). replica.statusChanges is a Stream of the same, emitted now and after every submit and exchange (subscribed before its first read, so a subscriber slow to take one misses no change); replica.changes pairs it with the optimistic shared value so one subscription sees both.

A refused edit arrives as a rejection on the next exchange, not as an error. The replica drops it and replays the rest; the mount re-installs the shared slice, and the field the user edited reverts. onPersistenceFailure does not run for this, since the local save succeeded. Watch rejected to explain the revert. Each rejection's refusal is a SyncRefusal, in Remote's vocabulary for "not saved", so a page that also shows Remote's refusals handles both with one function:

  • Forbidden { reason }: authorize or admit refused who sent it, or its replica belongs to another actor. reason is the rule's own words when it gave some (a plain false gives none), cut to 500 characters. Remote's Denied.Forbidden has the same shape.
  • Invalid { message, entity, rule, path }: the operation failed. When it breaks one of the contract's laws, rule is the law's id and message and path are the law's own. Otherwise rule and path are empty and message is a fixed sentence: "Not a valid operation" when validate refused or threw, authorize or a law threw, or the reducer could not apply it; "Not an operation this server accepts" when it did not decode or named another document; "Its id was already used for another" for a reused id. journalExchange names no Entity, so entity is empty; the shape is Remote's Refusal.invalid.
  • Unreadable { version }: written at an older Message version that no step of the server's history reads. The replica keeps such an operation in status.refused as well, with its payload (see changing a durable Message).
ts
import { Option } from 'effect'
import { SyncRefusal, type Rejection } from 'foldkit-sync'

const said = (rejection: Rejection): string =>
  Option.match(rejection.refusal, {
    onNone: () => 'Refused by the server',
    onSome: SyncRefusal.match({
      Forbidden: ({ reason }) => Option.getOrElse(reason, () => 'You may not change this'),
      Invalid: ({ message }) => message,
      Unreadable: () => 'Made by another version of the app, so not saved',
    }),
  })

refusal is none when the server named none (an older or a custom server), or named one this build cannot read, which the replica reports as UnknownRefusal and still treats as rejected. The server never sends an error's message, which may carry internals, so a rule a person should read about belongs in authorize with a reason, not in validate. The deprecated reason (the words alone, an Option<string>) is still filled for one release; read refusal instead.

Waiting for the server. The Model shows an edit before the server has it. Something that must not report success until the edit is committed, an agent tool for instance, waits on mounted.committed: the shared slice as the server confirmed it, with no pending edit applied. It is a source, { get, subscribe }, told after every exchange:

ts
const completion = Agent.when({
  source: mounted.committed,
  predicate: (shared, request: { readonly id: string }) =>
    shared.todos.some(todo => todo.id === request.id),
})

That completes after the exchange that commits the todo and never for one the server rejects.