Allow redirects to be canceled by Promise.then() after AJAX call

This commit is contained in:
Kijin Sung 2025-06-18 01:36:46 +09:00
parent 2c5b3e072f
commit f09cce5d1a

View file

@ -262,16 +262,39 @@ Rhymix.isSameHost = function(url) {
* Redirect to a URL, but reload instead if the target is the same as the current page * Redirect to a URL, but reload instead if the target is the same as the current page
* *
* @param string url * @param string url
* @param int delay
* @return void * @return void
*/ */
Rhymix.redirectToUrl = function(url) { Rhymix.redirectToUrl = function(url, delay) {
if (this.isCurrentUrl(url)) { const callback = function() {
if (Rhymix.isCurrentUrl(url)) {
window.location.href = url; window.location.href = url;
window.location.reload(); window.location.reload();
} else { } else {
window.location.href = url; window.location.href = url;
} }
}; };
if (delay) {
this.pendingRedirect = setTimeout(callback, delay);
} else {
callback();
}
};
/**
* Cancel any pending redirect
*
* @return bool
*/
Rhymix.cancelPendingRedirect = function() {
if (this.pendingRedirect) {
clearTimeout(this.pendingRedirect);
this.pendingRedirect = null;
return true;
} else {
return false;
}
};
/** /**
* Open a new window and focus it * Open a new window and focus it
@ -481,8 +504,9 @@ Rhymix.ajax = function(action, params, callback_success, callback_error) {
} }
// If the response contains a redirect URL, follow the redirect. // If the response contains a redirect URL, follow the redirect.
// This can be canceled by Rhymix.cancelPendingRedirect() within 100 milliseconds.
if (data.redirect_url) { if (data.redirect_url) {
Rhymix.redirectToUrl(data.redirect_url.replace(/&/g, '&')); Rhymix.redirectToUrl(data.redirect_url.replace(/&/g, '&'), 100);
return; return;
} }