From: Steinar H. Gunderson Date: Sat, 29 Oct 2022 15:14:26 +0000 (+0200) Subject: Move response processing down to a function. X-Git-Url: https://git.sesse.net/?p=remoteglot;a=commitdiff_plain;h=6323eec915c1c992ede706723a0a78d1de5099ae Move response processing down to a function. --- diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index d102ad0..a03bad6 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -257,48 +257,11 @@ var request_update = function() { current_analysis_xhr = $.ajax({ url: backend_url + "?ims=" + ims + "&unique=" + unique }).done(function(data, textstatus, xhr) { - sync_server_clock(xhr.getResponseHeader('Date')); - ims = xhr.getResponseHeader('X-RGLM'); - var num_viewers = xhr.getResponseHeader('X-RGNV'); - var new_data; - if (Array.isArray(data)) { - new_data = JSON.parse(JSON.stringify(current_analysis_data)); - JSON_delta.patch(new_data, data); - } else { - new_data = data; - } - - var minimum_version = xhr.getResponseHeader('X-RGMV'); - if (minimum_version && minimum_version > SCRIPT_VERSION) { - // Upgrade to latest version with a force-reload. - location.reload(true); - } - - // Verify that the PV makes sense. - var valid = true; - if (new_data['pv']) { - var hiddenboard = new Chess(new_data['position']['fen']); - for (var i = 0; i < new_data['pv'].length; ++i) { - if (hiddenboard.move(new_data['pv'][i]) === null) { - valid = false; - break; - } - } - } - - var timeout = 100; - if (valid) { - possibly_play_sound(current_analysis_data, new_data); - current_analysis_data = new_data; - update_board(); - update_num_viewers(num_viewers); - } else { - console.log("Received invalid update, waiting five seconds and trying again."); - setTimeout(function() { location.reload(true); }, 5000); - } + process_update_response(data, textstatus, xhr); // Next update. if (!backend_url.match(/history/)) { + var timeout = 100; current_analysis_request_timer = setTimeout(function() { request_update(); }, timeout); } }).fail(function(jqXHR, textStatus, errorThrown) { @@ -313,6 +276,47 @@ var request_update = function() { }); } +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 new_data; + if (Array.isArray(data)) { + new_data = JSON.parse(JSON.stringify(current_analysis_data)); + JSON_delta.patch(new_data, data); + } else { + new_data = data; + } + + var minimum_version = xhr.getResponseHeader('X-RGMV'); + if (minimum_version && minimum_version > SCRIPT_VERSION) { + // Upgrade to latest version with a force-reload. + location.reload(true); + } + + // Verify that the PV makes sense. + var valid = true; + if (new_data['pv']) { + var hiddenboard = new Chess(new_data['position']['fen']); + for (var i = 0; i < new_data['pv'].length; ++i) { + if (hiddenboard.move(new_data['pv'][i]) === null) { + valid = false; + break; + } + } + } + + if (valid) { + possibly_play_sound(current_analysis_data, new_data); + current_analysis_data = new_data; + update_board(); + update_num_viewers(num_viewers); + } else { + console.log("Received invalid update, waiting five seconds and trying again."); + setTimeout(function() { location.reload(true); }, 5000); + } +} + var possibly_play_sound = function(old_data, new_data) { if (!enable_sound) { return;