* 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;
/**
* The current backend request to get historic data, if any.
*
- * @type {?jqXHR}
+ * @type {?AbortController}
* @private
*/
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;
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));
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);
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();
+ }
+ });
}
/**
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.