Improved the management of the async requests of the visual console client

This commit is contained in:
Alejandro Gallardo Escobar 2019-05-17 14:09:30 +02:00
parent a25a08f48d
commit 34218fa7eb
6 changed files with 4293 additions and 31 deletions

View File

@ -34,7 +34,9 @@ function createVisualConsole(
var visualConsole = null;
var asyncTaskManager = new AsyncTaskManager();
function updateVisualConsole(visualConsoleId, updateInterval) {
function updateVisualConsole(visualConsoleId, updateInterval, tts) {
if (tts == null) tts = 0; // Time to start.
asyncTaskManager.add(
"visual-console",
function(done) {
@ -88,7 +90,26 @@ function createVisualConsole(
updateInterval
);
asyncTaskManager.init("visual-console");
asyncTaskManager.add("visual-console-start", function(done) {
var ref = setTimeout(function() {
asyncTaskManager.init("visual-console");
done();
}, tts);
return {
cancel: function() {
clearTimeout(ref);
}
};
});
if (tts > 0) {
// Wait to start the fetch interval.
asyncTaskManager.init("visual-console-start");
} else {
// Start the fetch interval immediately.
asyncTaskManager.init("visual-console");
}
}
// Initialize the Visual Console.
@ -112,8 +133,10 @@ function createVisualConsole(
}
});
// Start an interval to update the Visual Console.
updateVisualConsole(props.id, updateInterval);
if (updateInterval != null && updateInterval > 0) {
// Start an interval to update the Visual Console.
updateVisualConsole(props.id, updateInterval, updateInterval);
}
} catch (error) {
console.log("[ERROR]", "[VISUAL-CONSOLE-CLIENT]", error.message);
}
@ -121,7 +144,17 @@ function createVisualConsole(
return {
visualConsole: visualConsole,
changeUpdateInterval: function(updateInterval) {
updateVisualConsole(visualConsole.props.id, updateInterval);
if (updateInterval != null && updateInterval > 0) {
updateVisualConsole(
visualConsole.props.id,
updateInterval,
updateInterval
);
} else {
// Update interval disabled. Cancel possible pending tasks.
asyncTaskManager.cancel("visual-console");
asyncTaskManager.cancel("visual-console-start");
}
}
};
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -97,7 +97,17 @@ echo '<li class="nomn">';
echo '<div class="vc-refr">';
echo '<div id="vc-refr-form">';
echo __('Refresh').':';
echo html_print_select(get_refresh_time_array(), 'refr', $refr, '', '', 0, true, false, false);
echo html_print_select(
get_refresh_time_array(),
'vc-refr',
$refr,
'',
'',
0,
true,
false,
false
);
echo '</div>';
echo '</div>';
echo '</li>';
@ -188,7 +198,7 @@ $visualConsoleItems = VisualConsole::getItemsFromDB(
}
}
}
var visualConsole = createVisualConsole(
var visualConsoleManager = createVisualConsole(
container,
props,
items,
@ -197,13 +207,20 @@ $visualConsoleItems = VisualConsole::getItemsFromDB(
handleUpdate
);
$(document).ready (function () {
var refr = <?php echo (int) $refr; ?>;
var href = "<?php echo ui_get_url_refresh($ignored_params); ?>";
// Update the data fetch interval.
$('select#vc-refr').change(function(event) {
var refr = Number.parseInt(event.target.value);
$('select#refr').change(function (event) {
url = js_html_entity_decode( href ) + $('select#refr').val();
$(document).attr ("location", url);
});
if (!Number.isNaN(refr)) {
visualConsoleManager.changeUpdateInterval(refr * 1000); // To ms.
// Change the URL (if the browser has support).
if ("history" in window) {
var regex = /(refr=)\d+(&?)/gi;
var replacement = '$1' + refr + '$2';
var href = window.location.href.replace(regex, replacement);
window.history.replaceState({}, document.title, href);
}
}
});
</script>

View File

@ -177,7 +177,17 @@ if ($pure === true) {
echo '<div class="vc-refr">';
echo '<div id="vc-refr-form">';
echo __('Refresh').':';
echo html_print_select(get_refresh_time_array(), 'refr', $refr, '', '', 0, true, false, false);
echo html_print_select(
get_refresh_time_array(),
'vc-refr',
$refr,
'',
'',
0,
true,
false,
false
);
echo '</div>';
echo '</div>';
echo '</li>';
@ -296,7 +306,7 @@ $visualConsoleItems = VisualConsole::getItemsFromDB(
}
}
}
var visualConsole = createVisualConsole(
var visualConsoleManager = createVisualConsole(
container,
props,
items,
@ -305,16 +315,20 @@ $visualConsoleItems = VisualConsole::getItemsFromDB(
handleUpdate
);
$(document).ready (function () {
var refr = <?php echo (int) $refr; ?>;
var pure = <?php echo (int) $config['pure']; ?>;
var href = "<?php echo ui_get_url_refresh($ignored_params); ?>";
// Update the data fetch interval.
$('select#vc-refr').change(function(event) {
var refr = Number.parseInt(event.target.value);
if (pure) {
$('select#refr').change(function (event) {
url = js_html_entity_decode( href ) + $('select#refr').val();
$(document).attr ("location", url);
});
if (!Number.isNaN(refr)) {
visualConsoleManager.changeUpdateInterval(refr * 1000); // To ms.
// Change the URL (if the browser has support).
if ("history" in window) {
var regex = /(refr=)\d+(&?)/gi;
var replacement = '$1' + refr + '$2';
var href = window.location.href.replace(regex, replacement);
window.history.replaceState({}, document.title, href);
}
}
});
</script>

View File

@ -127,14 +127,14 @@ export default class AsyncTaskManager {
public add(
identifier: string,
taskInitiator: AsyncTaskInitiator,
period: number | null = null
period: number = 0
): AsyncTask {
if (this.tasks[identifier] && this.tasks[identifier].status === "started") {
this.tasks[identifier].cancel();
}
const asyncTask =
period !== null
period > 0
? asyncPeriodic(new AsyncTask(taskInitiator), period)
: new AsyncTask(taskInitiator);
@ -149,7 +149,12 @@ export default class AsyncTaskManager {
* @param identifier Unique identifier.
*/
public init(identifier: string) {
if (this.tasks[identifier] && this.tasks[identifier].status === "waiting") {
if (
this.tasks[identifier] &&
(this.tasks[identifier].status === "waiting" ||
this.tasks[identifier].status === "cancelled" ||
this.tasks[identifier].status === "finished")
) {
this.tasks[identifier].init();
}
}