Skip to main content

Server state under overlapping requests

Not released yet (comes after 1.7.0)

A click can send a request before the one before it has answered. These rules keep the screen and the server in agreement when that happens. The models that check them are in verification/web-ui/ (see Formal verification).

Per-request callbacks: useSettleEach​

React Query 5 runs the callbacks passed to mutate(variables, { onSuccess, onError }) only for the newest call of that useMutation: a second call before the first answered drops the first call's callbacks. Work that must happen for every request goes in the hook's own onSuccess/onSettled, or through the function useSettleEach() from src/api_client/useSettleEach.ts returns: settleEach(mutateAsync, variables, { onSuccess, onError }) settles each request's promise on its own and never leaves a rejection unhandled. Like mutate()'s own callbacks, they are skipped once the component that sent the request has unmounted, so a trash that answers after the viewer closed does not step the viewer and open it again; the mutation's own onSuccess (the cache updates) still runs. The QueryClient still reports a failure once.

It is used where requests overlap by design:

  • the face dashboard (useFaceActions, useSelectedFaceActions): every face whose request answered loses its pending state, and the faces a toolbar action took leave the selection, also when another action went out before it answered;
  • the viewer's trash (usePhotoActions): the viewer steps past every trashed photo;
  • the photo list's display preferences (usePhotoListPreferences), whose save promise always settles.

Checklists saved on every tick: useChecklistSave​

An album's collections and a person's groups are checklists saved on every tick (components/album/useChecklistSave.ts). One PUT is in flight at a time, of the newest list; when it answers and the list changed meanwhile, the newest list is sent next. A refused PUT shows the last stored list again. Add files the new collection or group into the list as it is when the create answers, not as it was when Add was clicked. Two PUTs can therefore never reach the server out of order. A PUT that has not answered after 30 seconds (settleWithin in src/util/settleWithin.ts) counts as refused, so a lost request cannot block the list for good.

A trash while a list loads​

applyTrashToggleToGrids (src/api_client/photos/removeFromDateAlbums.ts) takes a trashed photo out of the cached day lists in place. A day list that is being fetched at that moment is fetched again with cancelRefetch: React Query drops the running fetch's answer, which the server may have read before the trash, and reads the list again.

Undo toasts​

A change the user can take back shows its toast through successCounted (src/service/notifications/toast.ts) with an undo. While the toast is up, the next change of the same kind adds up in it:

  • Full time. Every change the toast adds starts its 8 seconds again. Mantine restarts a toast's timer only when its autoClose changes, so undoAutoClose alternates between 8000 and 8001 ms. The toast stays while its Undo button has the focus.
  • Undo takes back what the toast says. A change without an undo (a select-all change: the server names no photos) is counted in a toast of its own.
  • No click is lost. The undos belong to the toast's body, so a click while it already fades out still counts, once. They run one after the other, the latest first, and one that fails (the QueryClient reports it) does not stop the rest. Nor does one that has not answered after 30 seconds: a warning says that it is not confirmed yet.

The photo flags (favorite, hidden, archived, public: useBulkPhotoToggle) and the category send their changes and Undos through inTurn(line, send) (src/api_client/inTurn.ts): a request of a line goes out once the one before it has answered, or 30 seconds after that one was sent. The server therefore ends on the last click, also when an Undo is followed at once by the same change again. Favorites and star ratings share one line, since a favorite is a rating.

The Undo of a favorite change sends the answer's previous_ratings and new_rating to /api/photosedit/restore-ratings/. It restores exactly what was stored, also a rating that only a file can give (Adobe Bridge and darktable write -1 for a rejected photo) and that /api/photosedit/rating/ refuses. A photo whose rating is no longer new_rating was rated again since: the server leaves it alone, and the toast says how many there are.

Stars and favorites go out one request at a time, so a star given while a rating request hangs waits for it, up to 30 seconds from when that request was sent.

The Undo of a face labelling sends labelled_as, the person the labelling gave, to /api/restore-face-labels/. The server puts back only the faces that still have that person, under the same row locks as a labelling, and the toast says how many were labelled again and stay. A toast must not be shown from mutate()'s own onSuccess: it is lost when the component unmounts or sends another request first. Show it from the hook's onSuccess, or, as CategoryBadge does, from the promise of mutateAsync.

A selection while its list changes​

usePhotoListSelection drops a selected photo once the complete list no longer holds it (an Undo, another tab, a job): the selection bar counts, and its actions reach, only photos that are on the list. A list with placeholders for pages that are still loading drops nothing, as the photo may be on one of them.

A photo left out of Select all stays left out when it leaves the list, since it can come back to it before the action runs (hidden and shown again in another tab). The selection bar only stops counting it (unlisted).

Select all reaches what the list shows. A list that loads page by page gives PhotoListView a photosetQuery, which the server turns into the photos when an action runs, so that query has to match exactly the photos the list can show (api/tests/photos/test_select_all_reaches_the_list.py compares them). The day lists leave out the photos below the top of a stack and photos without a main file, so a query without show_all_stack_photos does too.

Some answers are cut off: a size sort lists at most 1,000 photos, and a filter without words at most 5,000 with semantic search on. Such a page passes the server's count as sortTotal. While it is larger than the list, PhotoListView leaves the query out, Select the loaded photos picks the listed photos one by one, and the bar says 1,000 of 1,011 selected. A selection ends when a refetch cuts the list off or makes it whole again.

Drafts of server values: useServerDraft​

hooks/useServerDraft.ts holds a draft over a value the server stores (site settings, user settings, photo list preferences, first-time setup):

  • One save at a time. commit() queues its save() behind the saves before it, so the server stores the values in the order they were committed. Only the newest save settles the draft. A save that has not answered after 30 seconds counts as refused and lets the next one go; its request is not aborted, so in the rare case it still lands later it can overwrite a newer value until the next fetch shows it.
  • Rebase. The draft keeps the server value it started from (base). With the rebase option the value shown is the server's current value with the edited fields on top; rebaseFields(server, base, draft) does that for an object field by field. The Settings page (useUserSettingsDraft) uses it and sends only the fields that differ from the stored profile, so Save changes never reverts a field another tab, the mobile app, the photo order or the timeline's default filter changed after the first edit.
  • save(value, stored) gets the value the client last knew stored when the save is sent: the last save that answered, or the last fetch. A patch of the changed fields is built against it, so a value set back while the save of another was out is still sent. A diff against the value at commit time left it out, and the server kept the older value.
  • Conflicts. rebaseFields keeps the user's value for every field they edited, also when the server changed the same field meanwhile: the save then writes the user's value over the other change, without a notice. Fields the user did not edit always follow the server.

The viewer's slideshow and steps​

useSlideshow (components/lightbox/useSlideshow.ts) keeps one state per item: loading, showing, unplayable, done. The media on the main slide drives it through mediaEvents:

  • onSettled: the photo has loaded or failed, and its time starts. Without it the time starts after 15 seconds (PHOTO_MAX_WAIT_MS).
  • onUnplayable: the video cannot play, or the browser refused to start it by itself (NotAllowedError, useAutoplay). It gets a photo's time.
  • onPlaying: the video plays, also after one of the two above or after it had ended. It is waited for again.
  • onEnded: also kept while the slideshow is off or held, so starting it on a video that has ended moves on.

An item that is done steps to the next one, or stops the slideshow at the end of the list. nextLoading (neighbourLoading in lightboxNavigation.ts) says that the next item is a placeholder for a page still loading: that is not the end, and the step follows when the page arrives.

A swipe steps when its slide is over, up to 0.3 seconds after the finger lifted. It goes to the photo that slid in, also when a slideshow step came in between, and not at all once the viewer closed: a step writes the photo to the address, and an address with a photo opens the viewer.

A photo turned in the viewer​

photoTurns (components/lightbox/photoTurns.ts) holds, per photo and for as long as the page is open, the turns that the picture on screen does not have yet, and the version in the picture's address. usePhotoRotation shows their sum. A turn goes through three steps:

  • sent: the request is on its way. Each one has its own promise (mutateAsync), because the callbacks of mutate only run for the newest click.
  • taken: the server has made it. Once no turn is sent, the version rises and the browser loads the picture again.
  • loading: the picture on its way has it. onImageLoad ends these turns and no others.

A refused turn (a 4xx) ends. A request that got no answer may have reached the server, or may still reach it: the turn stays on screen, and the viewer asks for the photo at once and then again after 1, 2, 4, 8, 15 and 30 seconds. When the photo has another thumbnail_revision, the picture loads again and the turn ends; when it still has the old one after a minute, the turn ends without. A turn the server only makes after that minute is not shown until the photo is loaded again. After an error of the server (a 5xx) the turn ends at once, and the viewer asks in the same way.

The version has to outlive the viewer: a browser uses a picture it has already loaded on the page again without asking, so without the version a photo turned, left and opened again shows the old one. The slides beside the photo (NeighbourMedia.tsx) use the same version.

Whether the details on the screen are older than the last turn is a question of its own (turnedSinceDetails): the zoomed original waits for new details after a turn, and is back as soon as they are there.

The tags editor​

TagsSection.tsx compares the draft with draftBase, the tags the photo had when the editor opened, and not with the query, which a scan or another tab can change meanwhile. Every change is tried and settles before the editor says anything, so a refused one does not skip the others. Each tag whose request went through moves in draftBase too, so that a save of which a part failed sends only the rest the next time. Taking off a tag the photo no longer has answers 200. A 404 means that the tag or the photo is gone, and counts as done. A tag that is added or still being typed while the save runs keeps the editor open, as an unsaved change.

linkWrites (api_client/linkWrites.ts) sends the changes of one public link, of a photo or of an album. To the server every change is "open this link, with this expiry or address", so one that arrives after a revoke opens the link again.

  • change waits for the changes before it and then asks whether the link is still open. If not, it is not sent (LinkClosedError), and the dialog says that the link was revoked somewhere else.
  • close does not wait. It is sent at once, drops the changes that wait (LinkWriteDroppedError, which shows no message), and is sent once more when the changes already on their way have their answer.
  • open is the user's own click on the switch or on Create link. It asks nothing.

A link revoked in another window between the question and the change is opened again. Only the server could refuse that.

The caption editor​

components/lightbox/Description.tsx keeps a typed draft when the saved caption changes (another tab, a caption job, the server merging captions under its lock, see Captions under concurrent writes) and shows The caption was changed somewhere else while you edited it. with Use that caption. An open editor whose text was not changed follows the saved caption.

Photos that appear while a scan runs​

components/preparation/LibraryRefresher.tsx is mounted once for the signed-in app and reads the photo lists again for a scan. It asks GET /api/library/preparation/, which answers with the state of the scan, the recognition and the model download without listing photos.

  • When it asks. Every two seconds while a scan is waiting or running, every 15 on an empty timeline or a search page, otherwise only when the job queue changes or the window gets the focus.
  • When it refreshes. Once when another scan has finished (last_finished_job), and during a scan when the number of photos read has grown by half and by at least 20 (nextRefresh). A library of 200,000 photos is read again about 25 times during its first scan, not once per photo.
  • An empty library takes any finished scan as news, so a scan that ended between the empty list and the first answer is not missed. verification/web-core/ScanRefresh.tla checks this.