Skip to content

Use cases edit instance metadata, the catalog and a deck's entry through a read port and a separate save port, so the If-Match never covers the read the change was made from #997

Done

Description

@ixuz

The problem

Some use cases edit a document by calling two ports, one to read and one to save, wrapped in again() (writes.ts:33), which tries again on changedElsewhere:

// instances.ts, renaming an instance
await again(async () => {
  const meta = await instanceRepository.readMeta(instance.url);
  if (meta === null) throw new AppError("noMetaToUpdate", { url: instance.url });
  if (meta.name !== name) await instanceRepository.saveMeta(instance.url, { ...meta, name });
});

The same shape appears in three more places:

Where Read Save
instances.ts:448 readMeta saveMeta
instances.ts:453 the catalog, renamed saveCatalog
instances.ts:468 readCatalog saveCatalog
decks.ts:191 readDeck saveDeck

There are two problems with this.

The If-Match does not cover the read the change was made from. Each save port reads the document again itself, then saves If-Match that second read:

What the save port writes, though, is the record the use case built from the first read. When another tab or device changes the document between the use case's read and the save port's read, the whole record goes back as the first read had it, and no 412 is ever raised. The window is two requests in a row, but again() cannot close it: the 412 it waits for does not happen there.

The write policy is split over two layers. After #995, editDocument in packages/solid re-applies the edits of a deck's catalog entry, the deck groups, the catalog's links and the rest. The metadata edits above are re-applied in packages/application instead, with a count of their own (METADATA_ATTEMPTS). The ports already have the right shape for some edits: editDeckTree and completeChapter take what to change, not the whole record. These four do not.

The preference writes have the same split. markConnectedScreen is retried by again() plus a separate retry on createdElsewhere (preferences.ts:333). The other preference writes and savePalette are written by hand in packages/solid and never tried again on a 412.

Your proposal

Give these edits ports that take a change, as editDeckTree and completeChapter do, and build each one on editDocument (#995):

editMeta(instanceUrl: string, change: (meta: InstanceMeta) => InstanceMeta | null): Promise<InstanceMeta | null>;
editCatalog(instanceUrl: string, change: (catalog: Catalog) => Catalog | null): Promise<Catalog | null>;
editDeck(deckUrl: string, change: (deck: Deck) => Deck | null): Promise<Deck>;
  • change gets the record as read and returns the record to write, or null for no change, which writes nothing. It may throw the use case's own errors (noMetaToUpdate, catalogMisfit, deckGone), which are thrown at once, as editDocument throws an edit's errors.
  • The adapter reads, builds the dataset from that same read, checks it, saves it If-Match, and on a 412 reads and changes again. So the record written is always the one built from the read the save is conditional on.
  • The use cases keep only their rules: no meta, a misfit catalog, nothing to change.
  • again() and METADATA_ATTEMPTS go, once nothing calls them.
  • The preference writes and savePalette become edits made by editDocument too, so whether they try again on a 412 is stated in one place. markConnectedScreen loses its two retries in preferences.ts.

Make it one port per commit (meta, catalog, deck, then preferences and palette), after #995's pull request (#996) is merged. Each read* port stays for the screens that only read. Each save* port the use cases no longer call is removed, as npm run check:exports would ask.

Alternatives

  • Pass the version read to the save port, as prepareCardChanges takes wholeAt(read.version). That closes the gap too, but every use case then carries a version from its read to its write, and the retry stays in packages/application.
  • Keep the ports and make the gap smaller, for example by having the save port compare what it read with what the use case read. That is more code in every save port for the same result as an edit port.
  • Leave it. The window is two requests long, and the record written comes from a read made a moment before. But it is the one place in the app where a write can silently undo a change made elsewhere, which the write discipline says never happens.

The bulk card edits, card transfers and deck imports in cards.ts are left out. They coordinate two documents (cards and reviews), plan between tries, and already pass the version they read on to the write.

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