Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Reload a list frame with its sort and page, and only once

Guido Boyked4dbdf878d5f
diff --git a/app/javascript/application.js b/app/javascript/application.js
index a3746f0..93ee7c3 100644
--- a/app/javascript/application.js
+++ b/app/javascript/application.js
@@ -9,6 +9,8 @@ import "@hotwired/stimulus"
// Import controllers
import "controllers"
+import { listFrameAddress, reloadWait } from "list_frame_reload"
+
// Debug: Check if Stimulus controllers are loaded
document.addEventListener('DOMContentLoaded', () => {
const ticketRows = document.querySelectorAll('[data-controller="list-row"]');
@@ -94,12 +96,30 @@ Turbo.StreamActions.makeRowsClickable = function () {
// open ticket. The address carries all of them; the frame request through
// index does too. An unchanged src does not fetch, so a frame that already
// stands at that address reloads instead.
+// The first reload runs at once. One that comes too soon waits past the duplicate guard below,
+// and everything that arrives meanwhile shares it: it reads the list after all of them.
+const frameReloads = new WeakMap();
+
Turbo.StreamActions.reloadFrame = function () {
const frame = document.getElementById(this.target);
if (!frame) return;
- const before = frame.getAttribute('src');
- frame.src = window.location.href;
- if (frame.getAttribute('src') === before) frame.reload();
+
+ const state = frameReloads.get(frame) || {};
+ frameReloads.set(frame, state);
+ if (state.timer) return;
+
+ const reload = () => {
+ state.timer = null;
+ state.at = Date.now();
+ // Every assignment fetches, an unchanged value too (Turbo's attributeChangedCallback). A
+ // frame.reload() behind it would fetch again, the guard below blocks that and the frame
+ // stops loading.
+ frame.src = listFrameAddress(window.location.href, frame.getAttribute('src'));
+ };
+
+ const wait = reloadWait(state.at, Date.now());
+ if (wait === 0) reload();
+ else state.timer = setTimeout(reload, wait);
};
// A record created inside a master/detail lane has no selection in the page
diff --git a/app/javascript/lib/list_frame_reload.js b/app/javascript/lib/list_frame_reload.js
new file mode 100644
index 0000000..6deb739
--- /dev/null
+++ b/app/javascript/lib/list_frame_reload.js
@@ -0,0 +1,32 @@
+// The address and the timing of a reload of a list frame (Turbo.StreamActions.reloadFrame).
+//
+// The page's address carries the filters and the open ticket. Sort and page live only in the
+// frame's own src: a click on a column head or on page 2 navigates the frame, not the page.
+// Reloading from the address alone would throw them away, so they come from the frame.
+
+// More than the duplicate guard in application.js blocks (500 ms): the same GET again inside
+// that window is dropped unseen, and the row of the later write would never reach the list.
+export const RELOAD_GAP_MS = 600
+
+const frameOwned = (name) => name === "page" || name.startsWith("sort")
+
+export function listFrameAddress(liveAddress, frameSource) {
+ if (!frameSource) return liveAddress
+
+ const address = new URL(liveAddress)
+ const frame = new URL(frameSource, liveAddress)
+
+ Array.from(address.searchParams.keys()).filter(frameOwned)
+ .forEach((name) => address.searchParams.delete(name))
+ frame.searchParams.forEach((value, name) => {
+ if (frameOwned(name)) address.searchParams.append(name, value)
+ })
+
+ return address.toString()
+}
+
+export function reloadWait(lastReloadAt, now) {
+ if (lastReloadAt === undefined) return 0
+
+ return Math.max(0, lastReloadAt + RELOAD_GAP_MS - now)
+}
diff --git a/config/importmap.rb b/config/importmap.rb
index 9401e30..d25a141 100644
--- a/config/importmap.rb
+++ b/config/importmap.rb
@@ -31,6 +31,7 @@ pin 'form_snapshot', to: 'lib/form_snapshot.js'
pin 'filter_bar/utils', to: 'lib/filter_bar/utils.js'
pin 'filter_bar/url_builder', to: 'lib/filter_bar/url_builder.js'
pin 'filter_bar/menu_renderer', to: 'lib/filter_bar/menu_renderer.js'
+pin 'list_frame_reload', to: 'lib/list_frame_reload.js'
pin 'ticket_selection', to: 'lib/ticket_selection.js'
pin 'ticket_bulk_draft', to: 'lib/ticket_bulk_draft.js'
pin 'new_tag_name', to: 'lib/new_tag_name.js'
diff --git a/spec/javascript/lib/list_frame_reload.test.js b/spec/javascript/lib/list_frame_reload.test.js
new file mode 100644
index 0000000..b7d6ff1
--- /dev/null
+++ b/spec/javascript/lib/list_frame_reload.test.js
@@ -0,0 +1,58 @@
+// What: the address and the timing of a list frame's reload (lib/list_frame_reload.js).
+// Why: the first version reloaded from the page's address and threw away sort and page, which
+// live only in the frame's src. And a second reload inside the 500 ms duplicate guard of
+// application.js was dropped unseen, so the row of the later write never reached the list.
+import { test } from "node:test"
+import assert from "node:assert/strict"
+import { listFrameAddress, reloadWait, RELOAD_GAP_MS } from "../../../app/javascript/lib/list_frame_reload.js"
+
+const live = "https://lalabase.test/org/o/projects/p/tickets?ticket_id=new&search=Filler"
+
+test("without a frame source the live address is the whole answer", () => {
+ assert.equal(listFrameAddress(live, null), live)
+})
+
+test("sort and page come from the frame, they are not in the address", () => {
+ const source = "/org/o/projects/p/tickets?ticket_id=old&search=Filler&sort%5Bcolumn%5D=title&sort%5Bdirection%5D=asc&page=2"
+
+ const address = new URL(listFrameAddress(live, source))
+
+ assert.equal(address.searchParams.get("sort[column]"), "title")
+ assert.equal(address.searchParams.get("sort[direction]"), "asc")
+ assert.equal(address.searchParams.get("page"), "2")
+})
+
+test("filters and the open ticket stay those of the live address", () => {
+ const source = "/org/o/projects/p/tickets?ticket_id=old&search=Stale&page=2"
+
+ const address = new URL(listFrameAddress(live, source))
+
+ assert.equal(address.searchParams.get("ticket_id"), "new")
+ assert.equal(address.searchParams.get("search"), "Filler")
+})
+
+test("a frame back on page one drops a page the address still names", () => {
+ const stale = `${live}&page=3&sort%5Bcolumn%5D=title`
+ const source = "/org/o/projects/p/tickets?ticket_id=new&search=Filler"
+
+ const address = new URL(listFrameAddress(stale, source))
+
+ assert.equal(address.searchParams.has("page"), false)
+ assert.equal(address.searchParams.has("sort[column]"), false)
+})
+
+test("the first reload does not wait", () => {
+ assert.equal(reloadWait(undefined, 1000), 0)
+})
+
+test("a reload inside the gap waits for the rest of it", () => {
+ assert.equal(reloadWait(1000, 1100), RELOAD_GAP_MS - 100)
+})
+
+test("a reload after the gap does not wait", () => {
+ assert.equal(reloadWait(1000, 1000 + RELOAD_GAP_MS), 0)
+})
+
+test("the gap clears the 500 ms duplicate guard in application.js", () => {
+ assert.ok(RELOAD_GAP_MS > 500)
+})