Skip to content

The Solid adapters each write their own read, change and If-Match save loop for a document that changes elsewhere #995

Done

Description

@ixuz

The problem

Writes in packages/solid that must hold up while the document changes elsewhere all do the same thing. They read the document, change it, check the write (checkWrite), save it If-Match the read, and on a 412 read it again and apply the change again. Each one writes that loop out by hand:

Where What Tries
typeIndex.ts:666 changeIndex 3
catalogLinks.ts:136 changeCatalog 3
solidAnswerLog.ts:83 removeAnswers 3
solidDeckRepository.ts:336 addDeck 3
solidDeckRepository.ts:375 editDeckTree 3
solidDeckRepository.ts:705 editEntry 3
solidReviewStateRepository.ts:188 writeFsrsStates 3
solidDigestRepository.ts:112 write 6, with a pause

That is eight copies of the same loop and six ATTEMPTS constants, each with its own comment saying the same thing, and eight copies of:

} catch (error) {
  if (!(error instanceof PreconditionFailedError) || attempt === ATTEMPTS) throw error;
}

Each adapter mixes two concerns. One is its own rules, such as "the deck must not be there yet" or "these subjects are checked". The other is the write policy: how many tries, whether to pause between them, which errors call for another try, where checkWrite runs, and a PATCH or a whole PUT. The write policy belongs with saveDataset and PreconditionFailedError in datasets.ts. Where it is now, a change to it must be made eight times, and the tests for it are spread over the adapters' test files.

Your proposal

Write the loop once, in datasets.ts, as one function that edits a document. changeIndex in typeIndex.ts already has this shape (a read, a pure change, nothing saved when nothing changed, and the tries), but only the type index uses it. The proposal is to generalize it:

export type DocumentEdit<R> =
  | { unchanged: R }
  | { dataset: Dataset; subjects?: string[]; result: R; whole?: boolean };

export async function editDocument<R>(
  url: string,
  fetch: Fetch,
  edit: (read: Dataset | null) => DocumentEdit<R>,
  { attempts = 3, pause, checkWrite }: EditPolicy = {},
): Promise<R>
  • edit is pure. It gets the document as read, or null when there is none, and says what to write, or that nothing changes. It may throw an AppError for its own rules, as addDeck and editEntry do now.
  • editDocument makes the tries. For each one it reads, runs edit, checks the subjects it names, saves If-Match the read (a whole PUT with whole), and tries again on a 412 until attempts runs out.
  • pause is the digest's wait between tries. The digest keeps its Refused wrapping and its batches around the call, since only it writes batches.

Each adapter then keeps only its own rules. The policy is tested once, in datasets.test.ts, and each adapter's tests no longer need their own 412 cases.

Move one adapter per commit, starting with typeIndex.ts, so each step is a small change that npm run check covers on its own. Nothing the app writes to a pod changes.

Alternatives

  • Keep the loops as they are. They work and they are covered, but each new write that must hold up under change copies the loop again, and a change to the policy (more tries, or a pause for every write as the digest has) has to be made in eight places.
  • Share only the error test (retriesOn412(error, attempt, attempts)). That removes the catch lines but not the read, check and save steps around them, so the adapters would still mix their rules with the policy.

Left out on purpose, for now:

  • gradeReviewState first tries the dataset it already has, without reading the document. It makes one try, not a loop, so it can stay as it is, or editDocument could later take a first dataset.
  • removeOwned in datasets.ts tries again once and turns a write outside the pod into "kept". It could use editDocument with attempts: 2, but it works as it is.
  • The four preference writes in solidPreferencesRepository.ts repeat the read, change, check and whole-PUT steps but never try again on a 412. editDocument with attempts: 1, whole: true would remove that repetition. Whether preferences should try again too is a separate question.

Data

  • This changes the vocabulary (ns/vocab/), the shapes (ns/shapes/) or the data the app writes to a pod.

Would you like to make it?

Yes, I would like to open a pull request

Activity

  1. added
    enhancement
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions