From 61b6a004369e36dbe0cd8499777de3ea17f6ba83 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 20 Dec 2022 23:27:52 +0100 Subject: [PATCH] Replace jQuery ajax() with the fetch API in all of our own code. --- www/js/remoteglot.js | 124 ++++++++++++++++++++++++++----------------- 1 file changed, 76 insertions(+), 48 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index d98c902..7245407 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -193,7 +193,7 @@ var current_display_move = null; * The current backend request to get main analysis (not history), if any, * so that we can abort it. * - * @type {?jqXHR} + * @type {?AbortController} * @private */ var current_analysis_xhr = null; @@ -210,7 +210,7 @@ var current_analysis_request_timer = null; /** * The current backend request to get historic data, if any. * - * @type {?jqXHR} + * @type {?AbortController} * @private */ var current_historic_xhr = null; @@ -218,7 +218,7 @@ var current_historic_xhr = null; /** * The current backend request to get hash probes, if any, so that we can abort it. * - * @type {?jqXHR} + * @type {?AbortController} * @private */ var current_hash_xhr = null; @@ -257,36 +257,52 @@ var get_unique = function() { var request_update = function() { current_analysis_request_timer = null; - current_analysis_xhr = $.ajax({ - url: backend_url + "?ims=" + ims + "&unique=" + unique - }).done(function(data, textstatus, xhr) { - if (delay_ms === 0) { - process_update_response(data, textstatus, xhr); - } else { - setTimeout(function() { process_update_response(data, textstatus, xhr); }, delay_ms); - } + let handle_err = () => { + // Backend error or similar. Wait ten seconds, then try again. + current_analysis_request_timer = setTimeout(function() { request_update(); }, 10000); + }; + + current_analysis_xhr = new AbortController(); + const signal = current_analysis_xhr.signal; + fetch(backend_url + "?ims=" + ims + "&unique=" + unique, { signal }) + .then((response) => response.json().then(data => ({ok: response.ok, headers: response.headers, json: data}))) // ick + .then((obj) => { + if (!obj.ok) { + handle_err(); + return; + } + + if (delay_ms === 0) { + process_update_response(obj.json, obj.headers); + } else { + setTimeout(function() { process_update_response(obj.json, obj.headers); }, delay_ms); + } + + // Next update. + if (!backend_url.match(/history/)) { + var timeout = 100; + current_analysis_request_timer = setTimeout(function() { request_update(); }, timeout); + } + }) + .catch((err) => { + if (err.name === 'AbortError') { + // Aborted because we are switching backends. Abandon and don't retry, + // because another one is already started for us. + } else { + handle_err(err); + } + }) + .finally(() => { + // Display. + document.body.style.opacity = null; + }) - // Next update. - if (!backend_url.match(/history/)) { - var timeout = 100; - current_analysis_request_timer = setTimeout(function() { request_update(); }, timeout); - } - }).fail(function(jqXHR, textStatus, errorThrown) { - document.body.style.opacity = null; - if (textStatus === "abort") { - // Aborted because we are switching backends. Abandon and don't retry, - // because another one is already started for us. - } else { - // Backend error or similar. Wait ten seconds, then try again. - current_analysis_request_timer = setTimeout(function() { request_update(); }, 10000); - } - }); } -var process_update_response = function(data, textstatus, xhr) { - sync_server_clock(xhr.getResponseHeader('Date')); - ims = xhr.getResponseHeader('X-RGLM'); - var num_viewers = xhr.getResponseHeader('X-RGNV'); +var process_update_response = function(data, headers) { + sync_server_clock(headers.get('Date')); + ims = headers.get('X-RGLM'); + var num_viewers = headers.get('X-RGNV'); var new_data; if (Array.isArray(data)) { new_data = JSON.parse(JSON.stringify(current_analysis_data)); @@ -295,7 +311,7 @@ var process_update_response = function(data, textstatus, xhr) { new_data = data; } - var minimum_version = xhr.getResponseHeader('X-RGMV'); + var minimum_version = headers.get('X-RGMV'); if (minimum_version && minimum_version > SCRIPT_VERSION) { // Upgrade to latest version with a force-reload. location.reload(true); @@ -1724,20 +1740,31 @@ var update_historic_analysis = function() { var filename = "/history/move" + (current_display_move + 1) + "-" + hiddenboard.fen().replace(/ /g, '_').replace(/\//g, '-') + ".json"; - current_historic_xhr = $.ajax({ - url: filename - }).done(function(data, textstatus, xhr) { - displayed_analysis_data = data; + let handle_err = () => { + displayed_analysis_data = {'failed': true}; update_board(); - }).fail(function(jqXHR, textStatus, errorThrown) { - if (textStatus === "abort") { - // Aborted because we are switching backends. Don't do anything; - // we will already have been cleared. - } else { - displayed_analysis_data = {'failed': true}; + }; + + current_historic_xhr = new AbortController(); + const signal = current_analysis_xhr.signal; + fetch(filename, { signal }) + .then((response) => response.json().then(data => ({ok: response.ok, json: data}))) // ick + .then((obj) => { + if (!obj.ok) { + handle_err(); + return; + } + displayed_analysis_data = obj.json; update_board(); - } - }); + }) + .catch((err) => { + if (err.name === 'AbortError') { + // Aborted because we are switching backends. Abandon and don't retry, + // because another one is already started for us. + } else { + handle_err(); + } + }); } /** @@ -1919,11 +1946,12 @@ var explore_hash = function(fen) { current_hash_display_timer = null; } $("#refutationlines").empty(); - current_hash_xhr = $.ajax({ - url: backend_hash_url + "?fen=" + fen - }).done(function(data, textstatus, xhr) { - show_explore_hash_results(data, fen); - }); + + current_hash_xhr = new AbortController(); + const signal = current_analysis_xhr.signal; + fetch(backend_hash_url + "?fen=" + fen, { signal }) + .then((response) => response.json()) + .then((data) => { show_explore_hash_results(data, fen); }); } /** Process the JSON response from a hash probe request. -- 2.39.2