1
0
Fork 0
mirror of https://github.com/iNavFlight/inav-configurator.git synced 2025-07-14 03:49:53 +03:00

timing methods extracted to separate classes

This commit is contained in:
Pawel Spychalski (DzikuVx) 2017-01-18 15:54:45 +01:00
parent fc11eca5c0
commit d1fcb81017
26 changed files with 262 additions and 219 deletions

161
js/gui.js
View file

@ -11,8 +11,6 @@ var GUI_control = function () {
this.active_tab;
this.tab_switch_in_progress = false;
this.operating_system;
this.interval_array = [];
this.timeout_array = [];
this.defaultAllowedTabsWhenDisconnected = [
'landing',
'firmware_flasher',
@ -51,162 +49,6 @@ var GUI_control = function () {
else this.operating_system = "Unknown";
};
// Timer managing methods
// name = string
// code = function reference (code to be executed)
// interval = time interval in miliseconds
// first = true/false if code should be ran initially before next timer interval hits
GUI_control.prototype.interval_add = function (name, code, interval, first) {
var data = {'name': name, 'timer': null, 'code': code, 'interval': interval, 'fired': 0, 'paused': false};
if (first == true) {
code(); // execute code
data.fired++; // increment counter
}
data.timer = setInterval(function() {
code(); // execute code
data.fired++; // increment counter
}, interval);
this.interval_array.push(data); // push to primary interval array
return data;
};
// name = string
GUI_control.prototype.interval_remove = function (name) {
for (var i = 0; i < this.interval_array.length; i++) {
if (this.interval_array[i].name == name) {
clearInterval(this.interval_array[i].timer); // stop timer
this.interval_array.splice(i, 1); // remove element/object from array
return true;
}
}
return false;
};
// name = string
GUI_control.prototype.interval_pause = function (name) {
for (var i = 0; i < this.interval_array.length; i++) {
if (this.interval_array[i].name == name) {
clearInterval(this.interval_array[i].timer);
this.interval_array[i].paused = true;
return true;
}
}
return false;
};
// name = string
GUI_control.prototype.interval_resume = function (name) {
for (var i = 0; i < this.interval_array.length; i++) {
if (this.interval_array[i].name == name && this.interval_array[i].paused) {
var obj = this.interval_array[i];
obj.timer = setInterval(function() {
obj.code(); // execute code
obj.fired++; // increment counter
}, obj.interval);
obj.paused = false;
return true;
}
}
return false;
};
// input = array of timers thats meant to be kept, or nothing
// return = returns timers killed in last call
GUI_control.prototype.interval_kill_all = function (keep_array) {
var self = this;
var timers_killed = 0;
for (var i = (this.interval_array.length - 1); i >= 0; i--) { // reverse iteration
var keep = false;
if (keep_array) { // only run through the array if it exists
keep_array.forEach(function (name) {
if (self.interval_array[i].name == name) {
keep = true;
}
});
}
if (!keep) {
clearInterval(this.interval_array[i].timer); // stop timer
this.interval_array.splice(i, 1); // remove element/object from array
timers_killed++;
}
}
return timers_killed;
};
// name = string
// code = function reference (code to be executed)
// timeout = timeout in miliseconds
GUI_control.prototype.timeout_add = function (name, code, timeout) {
var self = this;
var data = {'name': name, 'timer': null, 'timeout': timeout};
// start timer with "cleaning" callback
data.timer = setTimeout(function() {
code(); // execute code
// remove object from array
var index = self.timeout_array.indexOf(data);
if (index > -1) self.timeout_array.splice(index, 1);
}, timeout);
this.timeout_array.push(data); // push to primary timeout array
return data;
};
// name = string
GUI_control.prototype.timeout_remove = function (name) {
for (var i = 0; i < this.timeout_array.length; i++) {
if (this.timeout_array[i].name == name) {
clearTimeout(this.timeout_array[i].timer); // stop timer
this.timeout_array.splice(i, 1); // remove element/object from array
return true;
}
}
return false;
};
// no input paremeters
// return = returns timers killed in last call
GUI_control.prototype.timeout_kill_all = function () {
var timers_killed = 0;
for (var i = 0; i < this.timeout_array.length; i++) {
clearTimeout(this.timeout_array[i].timer); // stop timer
timers_killed++;
}
this.timeout_array = []; // drop objects
return timers_killed;
};
// message = string
GUI_control.prototype.log = function (message) {
var command_log = $('div#log');
@ -233,7 +75,8 @@ GUI_control.prototype.log = function (message) {
// default switch doesn't require callback to be set
GUI_control.prototype.tab_switch_cleanup = function (callback) {
MSP.callbacks_cleanup(); // we don't care about any old data that might or might not arrive
GUI.interval_kill_all(); // all intervals (mostly data pulling) needs to be removed on tab switch
helper.interval.killAll();
if (this.active_tab) {
TABS[this.active_tab].cleanup(callback);