Pending edits
Keeping unanswered edits across page reloads and crashes with persistence.
An edit is pending from the moment it is made until the server answers it. By default pending edits live in memory, so closing the page before the server answers them loses them. This page shows how to keep them in durable storage so that a later page load sends them.
Enabling persistence
Pass persistence with a key to createSyncClientDocument:
import { createSyncClientDocument } from "@voidhash/mimic/sync";
const document = createSyncClientDocument({
primitive: BoardDocument,
url,
database: databaseId,
collection: collectionId,
document: documentId,
credential,
persistence: { documentKey: `${userId}:${documentId}` },
onRejection: (_transactionId, reason) => showUndoneEdit(reason),
});By default the client keeps pending edits in IndexedDB, in a database named
mimic-pending-v2, and uses the Web Locks API to tell which pages are still
open.
How it works
Every edit gets its transaction id and its request (the commands, and the revision and schema version they were made on) when it is made, and keeps them until it is answered. With persistence, the client writes the edit to the store before it sends it, so an edit a later page finds is the very request that was sent, if any was.
Each instance of a document keeps its own queue under the documentKey, and
marks itself alive while its page is open. When a document starts, it
adopts the queues of instances that are no longer alive (pages that were
closed or crashed), and sends their edits, with their original transaction
ids, after its own sent edits and before its own unsent ones. It sends
nothing until adoption ends, or for at most 5 seconds. Because the server
evaluates each transaction id at most once, an edit that had in fact reached
the server before the page closed is answered with its recorded outcome, not
applied a second time.
A queue is adopted at most once, and the queue of a page that is still open is never taken, so two tabs on the same document do not send each other's edits.
Call dispose() when you are done with a document. Edits it still has
pending stay stored, and the next instance adopts them.
Choosing the document key
Every instance that may adopt another's edits must use the same key, and no unrelated instance may share it. Include everything that scopes the edits:
- the document id, so edits go to the document they were made in;
- the signed-in user, so one user's unsent edits are never sent under another user's session on a shared computer.
Edits that are not sent
Adopted edits are dropped, and reported to onRejection, in these cases:
- Too old. The server answers a transaction id from its record only
within its retry horizon (30 days by default). An edit older than the
horizon the server last reported is not sent: if it was never sent, it is
reported with the reason
"expired"; if it was, it is reported with the reason"unknown", because it may have applied. - Made on an older schema version. An edit made before the collection
was migrated can never apply to the migrated document: it is dropped with
the reason
"migration_version". See Schema versions. - Unreadable. A stored edit that does not decode is dropped with the
reason
"invalid".
An adopted edit that the server rejects is reported to onRejection like any
other rejection.
Options
| Name | Type | Description |
|---|---|---|
documentKey * | string | Identifies the document, and whatever else scopes its edits, in the store. |
store | PendingStore | Where pending edits are kept. Defaults to IndexedDB, database mimic-pending-v2. |
legacy | PendingStore | { databaseName } | false | A store an earlier client kept its pending edits in, adopted one way. Defaults to IndexedDB's mimic-pending-transactions; false for none. |
onError | (error: PendingStoreError) => void | Called when the store fails, once until it works again. Defaults to reporting the error outside the document's call stack. |
A failing store does not affect editing or syncing. Only surviving a closed page is affected: edits are then sent without a durable record.
An application with no earlier client's edits to carry over passes
legacy: false, so the client does not open the older store.
Other storage
PendingStore is an interface with five methods: markAlive, load,
save, remove and claimOrphans. Implement it to keep pending edits
somewhere else. The important contract is in claimOrphans: it must move the
queues of clients that are no longer alive atomically, and each queue at most
once, even when several callers race.