Repository navigation
Description
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>editis 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 anAppErrorfor its own rules, asaddDeckandeditEntrydo now.editDocumentmakes the tries. For each one it reads, runsedit, checks the subjects it names, saves If-Match the read (a whole PUT withwhole), and tries again on a 412 untilattemptsruns out.pauseis the digest's wait between tries. The digest keeps itsRefusedwrapping 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 thecatchlines 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:
gradeReviewStatefirst tries the dataset it already has, without reading the document. It makes one try, not a loop, so it can stay as it is, oreditDocumentcould later take a first dataset.removeOwnedindatasets.tstries again once and turns a write outside the pod into"kept". It could useeditDocumentwithattempts: 2, but it works as it is.- The four preference writes in
solidPreferencesRepository.tsrepeat the read, change, check and whole-PUT steps but never try again on a 412.editDocumentwithattempts: 1, whole: truewould 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