Skip to content

Keep edits made just before unload, and leave the editor when its project is deleted in another tab - #1423

Merged
microbit-matt-hillsdon merged 9 commits into
home-pagefrom
home-page-unload-flush
Nov 20, 2026
Merged

microbit-matt-hillsdon merged 9 commits into
home-pagefrom
home-page-unload-flush

Conversation

@microbit-matt-hillsdon

@microbit-matt-hillsdon microbit-matt-hillsdon commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Two fixes to project storage found in review of home-page. They depend on each other.

Keep edits made just before the page unloads

A flush read the project record and wrote once the read came back. An unloading page gets no further turns of the event loop, so a flush from pagehide committed only its read. A reload within the 300ms coalescing delay of an edit lost the edit.

Issuing the writes without a read is not enough on its own: the transaction is only kept if it is committed explicitly in the same turn.

  • No flush reads now. The open project's storage holds the project's name from when it is opened and writes the whole record with the files, through a new ProjectsDatabase.save.
  • A flush from a hidden or unloading page commits at once. Any other flush still checks the project exists, in the same transaction, and aborts if it has gone.
  • pagehide flushes whatever the visibility state. The shared handler skipped it where the page is not yet reported hidden.
  • apply() becomes update(), for the record only. Nothing else used its file changes.
  • A save that cannot start (the browser has closed the connection) or cannot announce itself to other tabs is reported like any other failed save, rather than thrown from the flush.

The existing e2e test passed without exercising this: typeInEditor ends with a 500ms key delay, so the write had already landed by its timer. It now holds the clock so only the unload path can save the edit.

Leave the editor when its project is deleted in another tab

A tab with the deleted project open kept showing it. The next edit failed with the generic "Failed to save" toast, and the next sidebar tab click re-ran the editor's loader, which silently swapped in another project.

  • The projects session stops the project's storage writing and raises openprojectdeleted.
  • The editor route goes to the home page with a message saying what happened.
  • A change notice for a project that has since been deleted is treated the same way. The handler loads the project once and passes what it read to the reload, so a delete cannot land between a check and the reload; one that lands later is caught by the delete notice.
  • A change notice no longer switches back to a project the user has left while it was being handled.

For review

  • Merged with home-page (the fix for a failed load wiping a project). Opening a project follows home-page: switchTo loads the record and files in one read before changing anything, and the storage takes its record from that load. The change notice handler does that load itself and passes the content on. A failed load or reload there is logged and the tab carries on with its copy; the open id is no longer cleared during a reload, so there is nothing to restore. The merge commit is where the two designs meet and is worth reading on its own.
  • Design change: the storage now owns the project's name and writes the whole record. It relies on the existing cross-tab reload to pick up a rename made in another tab.
  • Trade-off: an unload flush skips the existence check, so it can write back a project deleted in the same instant, before this tab hears of the delete.
  • Known limit: a timer flush that is mid-transaction when the page unloads is not committed explicitly, so it can still be lost. The window is the few milliseconds a transaction takes. There's no avoiding this.
  • New strings: project-deleted-elsewhere-title and project-deleted-elsewhere-description. The wording wants a look.
  • Firefox: checked by Claude with a throw-away Playwright run, as CI is Chromium only. With the fix an edit followed by a reload was kept 20 times out of 20; with the unload flush made non-urgent it was lost 8 times out of 10.
  • Safari: only tested manually and it's hard to be confident I was quick enough to have a save at the right point. But I have at least exercised the code.

A flush read the project record and wrote once the read came back. An
unloading page gets no further turns of the event loop, so a flush from
pagehide committed only its read: a reload within the 300ms coalescing
delay of an edit lost the edit. Issuing the writes without a read is not
enough either. In Chromium the transaction is only kept if it is committed
explicitly in the same turn.

So no flush reads now. The open project's storage holds the project's name
from when it is opened and writes the whole record with the files, through
ProjectsDatabase.save, which issues every request before it returns. A flush
from a hidden or unloading page commits at once. Any other flush still
checks the project exists, in the same transaction, and aborts if it has
gone.

pagehide now flushes whatever the visibility state, which the shared
handler skipped where the page is not yet reported hidden. Storage for a
project that has been deleted can be told to stop writing.

apply() becomes update(), for the record only: nothing else used its file
changes.

The e2e test for this passed without exercising it: typeInEditor ends with
a 500ms key delay, so the write had already landed by its timer. It now
holds the clock so only the unload path can save the edit.
A tab with the deleted project open kept showing it. The next edit failed
with the generic "Failed to save" toast, and the next sidebar tab click
re-ran the editor's loader, which silently swapped in another project.

The projects session now stops the project's storage writing and raises
openprojectdeleted, and the editor route goes to the home page with a
message saying what happened. A change notice for a project that has since
been deleted is treated the same way.
@github-actions

Copy link
Copy Markdown

Preview build will be at
https://review-python.microbit.org/home-page-unload-flush/

The change notice handler read the project, cleared the open id and then
reloaded, which read the project again. A delete landing between the two
reads made the reload throw, as an unhandled rejection, with the open id
already cleared: the delete notice that followed found nothing open, so the
tab kept a live storage for a deleted project and was never sent home.

switchTo now takes the record its caller read, so there is one read, and
the open id stays set until the switch, so a delete notice arriving later
finds the project open and lets go of it. The handler also gives up if the
user has opened another project while it waited, rather than switching
back.
save() opened its transaction outside its promise, so a connection the
browser had closed made flush() throw after it had taken the pending
changes: an uncaught error and no save-error toast. A throw from onChange
rejected the flush chain for good, and every later read with it.

Both now reach onError like any other failed save. The comment on flush
also claimed more than it does for a flush already in flight when the page
unloads.
It appears over the home page, where "This project" has nothing to point
at.
@microbit-matt-hillsdon
microbit-matt-hillsdon marked this pull request as ready for review November 20, 2026 11:03

@microbit-robert microbit-robert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

New text seems good. Project deleted behaviour seems good. Project name still updates ok. I don't know how I can practically test that changes within 300ms are kept when closing a tab.

Both sides changed how a project is opened. switchTo keeps home-page's
shape: it takes an id, loads the project's record and files in one read
before changing anything, and accepts content the caller already has. The
storage takes its record from that load, which replaces the separate record
read this branch had added.

The change notice handler loads the project itself and passes the content
on, so there is still one read between checking the project exists and
reloading it. A failed load or reload is logged and the tab carries on with
its copy, as on home-page; the open id is no longer cleared for the reload,
so there is nothing to restore.
Brings in the deferred first-project storage (#1425). The one conflict was
in the unit tests' setup: this branch's openTab helper is kept, with the
deferred storage's narrower type.
A flush writes the project's name as the storage holds it. When a change
notice made a tab reload the open project, the outgoing storage's last
flush still carried the old name, so if that tab had an edit waiting it
undid a rename the other tab had just made, in both tabs.

The outgoing storage now takes the name just read from the database before
it flushes, unless it has an unsaved rename of its own.
@microbit-matt-hillsdon

Copy link
Copy Markdown
Collaborator Author

Ran through manual tests again post-merge and all looks good.

@microbit-matt-hillsdon
microbit-matt-hillsdon merged commit facd7b1 into home-page Nov 20, 2026
3 checks passed
@microbit-matt-hillsdon
microbit-matt-hillsdon deleted the home-page-unload-flush branch November 20, 2026 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants