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 | /// arrive with the swap because the redirect URL's query params rendered | 201 | /// arrive with the swap because the redirect URL's query params rendered |
| 202 | /// them server-side; `history.replaceState` keeps the address bar honest. | 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 | /// The address bar and the `<html>` attributes are rewritten *before* the | 215 | /// The address bar and the `<html>` attributes are rewritten *before* the |
| 205 | /// new body's scripts run, never after: a swapped-in script that reads | 216 | /// new body's scripts run, never after: a swapped-in script that reads |
| 206 | /// `location.href` (the log-fit reload in `logs.rs`) would otherwise read | 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 | for (i = theirs.length; i < mine.length; i++) { mine[i].remove(); } \ | 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 | function swap(html, url, fresh, push, morphing) { \ | 386 | function swap(html, url, fresh, push, morphing) { \ |
| 373 | var keep = window.__izlekKeep ? captureFields() : null; \ | 387 | var keep = window.__izlekKeep ? captureFields() : null; \ |
| 374 | window.__izlekKeep = false; \ | 388 | window.__izlekKeep = false; \ |
| @@ -403,20 +417,23 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | |||
| 403 | }); \ | 417 | }); \ |
| 404 | } \ | 418 | } \ |
| 405 | window.__izlekGo = function (url) { \ | 419 | window.__izlekGo = function (url) { \ |
| 420 | + var n = navStep(); \ | ||
| 406 | fetch(url).then( \ | 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 | function () { window.location.href = url; } \ | 423 | function () { window.location.href = url; } \ |
| 409 | ); \ | 424 | ); \ |
| 410 | }; \ | 425 | }; \ |
| 411 | window.__izlekRefresh = function () { \ | 426 | window.__izlekRefresh = function () { \ |
| 427 | + var n = window.__izlekNav; \ | ||
| 412 | fetch(window.location.href).then( \ | 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 | function () {} \ | 430 | function () {} \ |
| 415 | ); \ | 431 | ); \ |
| 416 | }; \ | 432 | }; \ |
| 417 | window.__izlekPost = function (action, fields) { \ | 433 | window.__izlekPost = function (action, fields) { \ |
| 434 | + var n = navStep(); \ | ||
| 418 | fetch(action, { method: 'POST', body: new URLSearchParams(fields) }).then( \ | 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 | function () {} \ | 437 | function () {} \ |
| 421 | ); \ | 438 | ); \ |
| 422 | }; \ | 439 | }; \ |
| @@ -424,6 +441,7 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | |||
| 424 | var scrim = document.querySelector('.viewer-scrim'); \ | 441 | var scrim = document.querySelector('.viewer-scrim'); \ |
| 425 | if (!scrim) { return false; } \ | 442 | if (!scrim) { return false; } \ |
| 426 | var back = scrim.querySelector('.viewer-close').getAttribute('href'); \ | 443 | var back = scrim.querySelector('.viewer-close').getAttribute('href'); \ |
| 444 | + navStep(); \ | ||
| 427 | scrim.remove(); \ | 445 | scrim.remove(); \ |
| 428 | sweepPanels(); \ | 446 | sweepPanels(); \ |
| 429 | history.replaceState(null, '', back); \ | 447 | history.replaceState(null, '', back); \ |
| @@ -434,6 +452,7 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | |||
| 434 | window.__izlekCloseModal = function () { \ | 452 | window.__izlekCloseModal = function () { \ |
| 435 | var scrims = document.querySelectorAll('.modal-scrim'); \ | 453 | var scrims = document.querySelectorAll('.modal-scrim'); \ |
| 436 | if (!scrims.length) { return false; } \ | 454 | if (!scrims.length) { return false; } \ |
| 455 | + navStep(); \ | ||
| 437 | scrims.forEach(function (el) { el.remove(); }); \ | 456 | scrims.forEach(function (el) { el.remove(); }); \ |
| 438 | sweepPanels(); \ | 457 | sweepPanels(); \ |
| 439 | var u = new URL(window.location.href); \ | 458 | var u = new URL(window.location.href); \ |
| @@ -455,8 +474,9 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | |||
| 455 | e.preventDefault(); \ | 474 | e.preventDefault(); \ |
| 456 | var data = e.submitter ? new FormData(form, e.submitter) : new FormData(form); \ | 475 | var data = e.submitter ? new FormData(form, e.submitter) : new FormData(form); \ |
| 457 | var multipart = (form.getAttribute('enctype') || '').indexOf('multipart') !== -1; \ | 476 | var multipart = (form.getAttribute('enctype') || '').indexOf('multipart') !== -1; \ |
| 477 | + var n = navStep(); \ | ||
| 458 | fetch(form.getAttribute('action'), { method: 'POST', body: multipart ? data : new URLSearchParams(data) }).then( \ | 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 | function () { form.submit(); } \ | 480 | function () { form.submit(); } \ |
| 461 | ); \ | 481 | ); \ |
| 462 | }, true); \ | 482 | }, true); \ |
| @@ -498,8 +518,9 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | |||
| 498 | var href = link.getAttribute('href'); \ | 518 | var href = link.getAttribute('href'); \ |
| 499 | if (!href || href.charAt(0) !== '/' || href.indexOf('/files/') === 0) { return; } \ | 519 | if (!href || href.charAt(0) !== '/' || href.indexOf('/files/') === 0) { return; } \ |
| 500 | e.preventDefault(); \ | 520 | e.preventDefault(); \ |
| 521 | + var n = navStep(); \ | ||
| 501 | fetch(href).then( \ | 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 | function () { window.location.href = href; } \ | 524 | function () { window.location.href = href; } \ |
| 504 | ); \ | 525 | ); \ |
| 505 | }, true); \ | 526 | }, true); \ |