Skip to content

Commit

Permalink
Fixed race condition caused by auto refresh updates during form submi…
Browse files Browse the repository at this point in the history
…ssion
  • Loading branch information
mpscholten committed Jan 22, 2021
1 parent 68ed2d6 commit d5a7e87
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/IHP/static/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ window.submitForm = function (form, possibleClickedButton) {
alert.classList.add('dismiss');
}
}

if (window['pauseAutoRefresh']) {
window.pauseAutoRefresh();
}
};

function initToggle() {
Expand Down Expand Up @@ -398,6 +402,11 @@ window.transitionToNewPage = function (newHtml) {
newHtml.head.querySelector('meta[property="ihp-auto-refresh-id"]');

if (ihpAutoRefreshId) {
var prevIhpAutoRefreshId = document.head.querySelector('meta[property="ihp-auto-refresh-id"]')
if (prevIhpAutoRefreshId) {
prevIhpAutoRefreshId.remove();
}

document.head.appendChild(ihpAutoRefreshId);
}

Expand Down
14 changes: 14 additions & 0 deletions lib/IHP/static/ihp-auto-refresh.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var socket = null;
var sessionId = null;
var autoRefreshPaused = false;

function autoRefreshView() {
var metaTag = document.querySelector('meta[property="ihp-auto-refresh-id"]');

Expand Down Expand Up @@ -27,6 +29,8 @@ function autoRefreshView() {
sessionId = metaTag.content;
}

autoRefreshPaused = false;

socket.onopen = function (event) {
socket.send(metaTag.content);
};
Expand All @@ -35,6 +39,11 @@ function autoRefreshView() {
var html = event.data;
var parser = new DOMParser();
var dom = parser.parseFromString(html, 'text/html');

if (autoRefreshPaused) {
return;
}

morphdom(document.body, dom.body, {
getNodeKey: function (el) {

Expand Down Expand Up @@ -68,6 +77,11 @@ function autoRefreshView() {
};
}

/* Called by helpers.js when a form was just submitted and we're waiting for a response from the server */
window.pauseAutoRefresh = function () {
autoRefreshPaused = true;
};

if (window.Turbolinks) {
document.addEventListener('turbolinks:load', autoRefreshView);
} else {
Expand Down

2 comments on commit d5a7e87

@harryaskham
Copy link

Choose a reason for hiding this comment

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

Is there a possibility that this workaround breaks for Actions that finish with a call to redirectTo? I'm finding that submitting a form whose Action redirects causes autoRefreshPaused to become stuck at "true" until manually refreshing the page. Does autoRefreshPaused only reset for Actions that return 200 (rather than say 302)?

@mpscholten
Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for reporting! Sounds like a bug to me. Could you open an issue and produce a code example (best would be to use ihp-new and put it into a fresh project) where this bug is triggered? Then i could dig deeper into this :)

Please sign in to comment.