The newest paint is the newest intent
Browse files at this commitee25951765f42b12dc76e8704e182b18c68bf40dParent: de54fef- fix
- the newest paint is the newest intent
1 changed file +26 -5
crates/izlek-web/src/layout.rsmodified+26 -5
| @@ -201,6 +201,17 @@ pub async fn topbar_nav(cx: &Cx, active: NavPage, role: izlek_core::Role, lang: | ||
| 201 | 201 | /// arrive with the swap because the redirect URL's query params rendered |
| 202 | 202 | /// them server-side; `history.replaceState` keeps the address bar honest. |
| 203 | 203 | /// |
| 204 | +/// Every fetch that ends in a swap carries the navigation counter it started | |
| 205 | +/// under (`window.__izlekNav`), and its answer is thrown away if that counter | |
| 206 | +/// has moved. Without it the newest paint is not the newest intent: the live | |
| 207 | +/// refresh fetches the URL it was on, and a click landing during that flight | |
| 208 | +/// pushed a new page that the refresh's answer then overwrote — the page | |
| 209 | +/// arriving and reverting to the previous one two hundred milliseconds later. | |
| 210 | +/// Navigations, form posts and the overlay closes each step the counter, so a | |
| 211 | +/// second click also wins over a first one still in flight, and a refresh — | |
| 212 | +/// which is not an intent, only an update of what is already on screen — | |
| 213 | +/// steps nothing and lands only if nothing else has happened. | |
| 214 | +/// | |
| 204 | 215 | /// The address bar and the `<html>` attributes are rewritten *before* the |
| 205 | 216 | /// new body's scripts run, never after: a swapped-in script that reads |
| 206 | 217 | /// `location.href` (the log-fit reload in `logs.rs`) would otherwise read |
| @@ -369,6 +380,9 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | ||
| 369 | 380 | } \ |
| 370 | 381 | for (i = theirs.length; i < mine.length; i++) { mine[i].remove(); } \ |
| 371 | 382 | } \ |
| 383 | + window.__izlekNav = 0; \ | |
| 384 | + function navStep() { return ++window.__izlekNav; } \ | |
| 385 | + function stillCurrent(n) { return window.__izlekNav === n; } \ | |
| 372 | 386 | function swap(html, url, fresh, push, morphing) { \ |
| 373 | 387 | var keep = window.__izlekKeep ? captureFields() : null; \ |
| 374 | 388 | window.__izlekKeep = false; \ |
| @@ -403,20 +417,23 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | ||
| 403 | 417 | }); \ |
| 404 | 418 | } \ |
| 405 | 419 | window.__izlekGo = function (url) { \ |
| 420 | + var n = navStep(); \ | |
| 406 | 421 | fetch(url).then( \ |
| 407 | - function (r) { return r.text().then(function (t) { swap(t, r.url); }); }, \ | |
| 422 | + function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url); } }); }, \ | |
| 408 | 423 | function () { window.location.href = url; } \ |
| 409 | 424 | ); \ |
| 410 | 425 | }; \ |
| 411 | 426 | window.__izlekRefresh = function () { \ |
| 427 | + var n = window.__izlekNav; \ | |
| 412 | 428 | fetch(window.location.href).then( \ |
| 413 | - function (r) { return r.text().then(function (t) { swap(t, r.url, false, false, true); }); }, \ | |
| 429 | + function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url, false, false, true); } }); }, \ | |
| 414 | 430 | function () {} \ |
| 415 | 431 | ); \ |
| 416 | 432 | }; \ |
| 417 | 433 | window.__izlekPost = function (action, fields) { \ |
| 434 | + var n = navStep(); \ | |
| 418 | 435 | fetch(action, { method: 'POST', body: new URLSearchParams(fields) }).then( \ |
| 419 | - function (r) { return r.text().then(function (t) { swap(t, r.url); }); }, \ | |
| 436 | + function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url); } }); }, \ | |
| 420 | 437 | function () {} \ |
| 421 | 438 | ); \ |
| 422 | 439 | }; \ |
| @@ -424,6 +441,7 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | ||
| 424 | 441 | var scrim = document.querySelector('.viewer-scrim'); \ |
| 425 | 442 | if (!scrim) { return false; } \ |
| 426 | 443 | var back = scrim.querySelector('.viewer-close').getAttribute('href'); \ |
| 444 | + navStep(); \ | |
| 427 | 445 | scrim.remove(); \ |
| 428 | 446 | sweepPanels(); \ |
| 429 | 447 | history.replaceState(null, '', back); \ |
| @@ -434,6 +452,7 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | ||
| 434 | 452 | window.__izlekCloseModal = function () { \ |
| 435 | 453 | var scrims = document.querySelectorAll('.modal-scrim'); \ |
| 436 | 454 | if (!scrims.length) { return false; } \ |
| 455 | + navStep(); \ | |
| 437 | 456 | scrims.forEach(function (el) { el.remove(); }); \ |
| 438 | 457 | sweepPanels(); \ |
| 439 | 458 | var u = new URL(window.location.href); \ |
| @@ -455,8 +474,9 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | ||
| 455 | 474 | e.preventDefault(); \ |
| 456 | 475 | var data = e.submitter ? new FormData(form, e.submitter) : new FormData(form); \ |
| 457 | 476 | var multipart = (form.getAttribute('enctype') || '').indexOf('multipart') !== -1; \ |
| 477 | + var n = navStep(); \ | |
| 458 | 478 | fetch(form.getAttribute('action'), { method: 'POST', body: multipart ? data : new URLSearchParams(data) }).then( \ |
| 459 | - function (r) { return r.text().then(function (t) { swap(t, r.url); }); }, \ | |
| 479 | + function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url); } }); }, \ | |
| 460 | 480 | function () { form.submit(); } \ |
| 461 | 481 | ); \ |
| 462 | 482 | }, true); \ |
| @@ -498,8 +518,9 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | ||
| 498 | 518 | var href = link.getAttribute('href'); \ |
| 499 | 519 | if (!href || href.charAt(0) !== '/' || href.indexOf('/files/') === 0) { return; } \ |
| 500 | 520 | e.preventDefault(); \ |
| 521 | + var n = navStep(); \ | |
| 501 | 522 | fetch(href).then( \ |
| 502 | - function (r) { return r.text().then(function (t) { swap(t, r.url, true, true); }); }, \ | |
| 523 | + function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url, true, true); } }); }, \ | |
| 503 | 524 | function () { window.location.href = href; } \ |
| 504 | 525 | ); \ |
| 505 | 526 | }, true); \ |