diff --git a/app/javascript/controllers/drawer_controller.js b/app/javascript/controllers/drawer_controller.js index db10b79..2ffb00f 100644 --- a/app/javascript/controllers/drawer_controller.js +++ b/app/javascript/controllers/drawer_controller.js @@ -13,7 +13,8 @@ export default class extends Controller { static targets = ["panel", "scrim", "button", "content"] connect() { - this.closeOnVisit = () => this.close() + // A refresh of the current page (the poll controller) is a visit too; it must not close the menu. + this.closeOnVisit = (event) => { if (event.detail?.url !== window.location.href) this.close() } document.addEventListener("turbo:before-visit", this.closeOnVisit) // Past 64rem the CSS shows the panel in place, so an open drawer must // let go of the page (inert, scrim) when the viewport grows past it. diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js index 54f62cf..b58f961 100644 --- a/app/javascript/controllers/index.js +++ b/app/javascript/controllers/index.js @@ -10,4 +10,7 @@ application.register("drawer", DrawerController) import FlashController from "./flash_controller" application.register("flash", FlashController) +import PollController from "./poll_controller" +application.register("poll", PollController) + export { application } diff --git a/app/javascript/controllers/poll_controller.js b/app/javascript/controllers/poll_controller.js new file mode 100644 index 0000000..7ee669c --- /dev/null +++ b/app/javascript/controllers/poll_controller.js @@ -0,0 +1,71 @@ +import { Controller } from "@hotwired/stimulus" +import { Turbo } from "@hotwired/turbo-rails" + +// Refreshes the page every `interval` ms while the element is on it, through Turbo's page +// refresh with morphing: the new page is merged into the old one, scroll and focus stay. The +// archive page renders the element only while captures run, so the first page without it ends +// the polling: the morph removes the element and this controller disconnects. +// +// What the refresh must not do: cancel a visit or a form the person started (Turbo.session.refresh +// skips while a visit is current; forms are checked here), wipe what they typed or close a +//
they opened (the morph attribute hook below), replace the page with an error page +// when the server hiccups (the fetch response hook), or run in a hidden tab. +export default class extends Controller { + static values = { interval: { type: Number, default: 5000 } } + + connect() { + this.onVisibility = () => { if (!document.hidden) this.refresh() } + this.onMorphAttribute = (event) => this.keepLocalState(event) + this.onFetchResponse = (event) => this.skipErrorResponse(event) + document.addEventListener("visibilitychange", this.onVisibility) + document.addEventListener("turbo:before-morph-attribute", this.onMorphAttribute) + document.addEventListener("turbo:before-fetch-response", this.onFetchResponse) + this.schedule() + } + + disconnect() { + document.removeEventListener("visibilitychange", this.onVisibility) + document.removeEventListener("turbo:before-morph-attribute", this.onMorphAttribute) + document.removeEventListener("turbo:before-fetch-response", this.onFetchResponse) + clearTimeout(this.timer) + } + + schedule() { + clearTimeout(this.timer) + if (document.hidden) return + this.timer = setTimeout(() => this.refresh(), this.intervalValue) + } + + refresh() { + const busy = document.hidden || Turbo.navigator.currentVisit || Turbo.navigator.formSubmission + if (!busy) { + this.refreshing = true + Turbo.session.refresh(document.baseURI, { method: "morph", scroll: "preserve" }) + } + // The morph keeps this element, and this controller with it, while captures still run, so + // the next tick is set here; connect() does not run again. When the new page has no poll + // element, the morph removes it and disconnect() clears the timer. + this.schedule() + } + + // A value the person typed, and a
they opened, are theirs, not the server's. + keepLocalState(event) { + const { attributeName } = event.detail + const target = event.target + const typed = attributeName === "value" && (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) + const opened = attributeName === "open" && target instanceof HTMLDetailsElement + if (typed || opened) event.preventDefault() + } + + // A refresh that gets a 502 from a proxy or a 500 from the app must not replace the page with + // the error page: drop that visit, keep what is shown, and try again at the next tick. The + // visit has to be stopped as well, or it stays current and blocks every later refresh. + skipErrorResponse(event) { + const response = event.detail.fetchResponse + if (this.refreshing && response && !response.succeeded) { + event.preventDefault() + Turbo.navigator.stop() + } + this.refreshing = false + } +} diff --git a/app/views/archives/show.html.erb b/app/views/archives/show.html.erb index cc26aa0..22fd50a 100644 --- a/app/views/archives/show.html.erb +++ b/app/views/archives/show.html.erb @@ -68,13 +68,23 @@ <% end %> + <%# + A page refresh (the poll controller, while captures run) morphs this page and keeps the scroll. + Both metas stay on the finished page too: Turbo reads them from the page it refreshes. + %> + + <% content_for :head do %> + + + <% end %> + <% unless @archive.finished? %> -
+
<%= icon(:loader_circle, size: 18, class: "spinner") %>

Captures are running

-

Reload this page in a moment.

+

This page updates itself as they finish.

<% end %> diff --git a/docs/design.md b/docs/design.md index e0023a0..66e579b 100644 --- a/docs/design.md +++ b/docs/design.md @@ -161,6 +161,8 @@ Stash uses `patchwork-central` (not the restricted database), schema `stash`. Ar ## Jobs +While an archive runs, its page refreshes itself every five seconds (the `poll` Stimulus controller, rendered only on an unfinished archive) through Turbo's page refresh with morphing, so the captures fill in without a flash and scroll stays put. The first finished page carries no controller, and the polling ends with it. The refresh never cancels a visit or a form the person started, keeps what they typed and the `
` they opened, leaves the page alone when the server answers an error, and pauses in a hidden tab. + `ArchiveJob` makes the capture rows from the router and enqueues one `CaptureJob` per capture. `CaptureJob` runs one capturer, stores its artifacts, and updates the archive's status. The jobs queue is `captures`, with a small concurrency (`GOOD_JOB_MAX_THREADS`, default 2), because Chromium and yt-dlp are heavy. ## Environments diff --git a/test/controllers/archives_controller_test.rb b/test/controllers/archives_controller_test.rb index 9ee3c17..c209377 100644 --- a/test/controllers/archives_controller_test.rb +++ b/test/controllers/archives_controller_test.rb @@ -27,6 +27,22 @@ class ArchivesControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_includes response.body, archive.public_id assert_not_includes response.body, "Delete this archive" + # Finished: nothing polls. The morph metas stay, so the last refresh of a running page morphs too. + assert_select "[data-controller=poll]", count: 0 + assert_select "meta[name=turbo-refresh-method][content=morph]", count: 1 + end + + test "an archive that is still running polls for its result and morphs the page" do + archive = create_archive_with_captures({ "page" => "running", "git" => "queued" }) + sign_in_as(:viewer) + + get archive_path(archive) + + assert_response :success + assert_select "[data-controller=poll][data-poll-interval-value='5000']", count: 1 + assert_select "meta[name=turbo-refresh-method][content=morph]", count: 1 + assert_select "meta[name=turbo-refresh-scroll][content=preserve]", count: 1 + assert_includes response.body, "This page updates itself as they finish" end test "the list searches by URL, host and note" do