Cleaning up the JS code.

This commit is contained in:
Scott Idem
2020-02-19 14:37:40 -05:00
parent 8a6123762b
commit 46f0762511
9 changed files with 1106 additions and 1150 deletions

View File

@@ -99,10 +99,12 @@
<script>const app = require('./js/module_app');</script>
<script>const api = require('./js/module_api');</script>
<script>const idb = require('./js/module_app_idb');</script>
<script>const ui = require('./js/module_app_ui');</script>
<script>const uiv2 = require('./js/module_app_ui_v2');</script>
<!--<script src="js/api.js"></script>-->
<!--<script src="js/script.js"></script>-->
<script src="js/app_ui_misc.js"></script>
<script src="js/app_ui_sessions.js"></script>
<script src="js/app_ui_presentations.js"></script>
<script src="js/app_ui_presenters.js"></script>
<script src="js/app_ui_files.js"></script>
<!--<script src="js/testing.js"></script>-->
<script>
@@ -244,8 +246,7 @@
const api_token_update_loop_interval = 120000; // loop forever
const update_idb_loop_interval = 500; // loop forever; update when check datetime has passed
const idb_to_launcher_loop_interval = 5000; // loop forever; only run when one or more tables are updated
const update_render_loop_interval = 10000; // loop forever; only run when the launcher object is safe
//const update_render_loop_interval = 10000; // loop forever; only run when the launcher object is safe
const check_file_cache_loop_interval = 250; // loop forever; only run after the event_file table has updated
// Axios needs to authenticate first
@@ -269,9 +270,8 @@
idb_event_file_check_datetime = Date.now();
let run_update_idb = false;
let run_idb_to_launcher = false;
let idb_to_launcher_in_progress = false;
let safe_to_render_launcher = false; // currently only set to true in module_app_idb.js
//let run_idb_to_launcher = false;
//let idb_to_launcher_in_progress = false;
let run_check_file_cache = false;
let looping_tbl_event = false;
@@ -281,40 +281,11 @@
let looping_tbl_event_presenter = false;
let looping_tbl_event_file = false;
//const loading_loop_interval = app_config.loading_loop_interval; // in milliseconds
//const loading_loop_interval = 500;
//const main_loop_interval = app_config.main_loop_interval; // in milliseconds
//const main_loop_interval = 1500; //10000; // 10 seconds
//const idb_check_period = app_config.idb_check_period; //30000; // 60 seconds
// The datetime is when it should be checked again, not when it was last checked.
// It may be good to split the object check period into event, location, session, presentation, presenter
//let idb_check_datetime = Date.now(); // default to current datetime in ms
// The datetime is when it should be checked again, not when it was last checked.
// It may be good to split the file check period into event, location, session, presentation, presenter
//let launcher_files_check_datetime = Date.now(); // default to current datetime in ms
const host_file_cache_path = app_config.host_file_cache_path; //'file_cache/';
//const host_file_cache_check_period = app_config.host_file_cache_check_period; //30000; // 30 seconds
let launcher = new Object(); // This will create the something like location > sessions and location files > presenters and session files > presenter files
let idb_to_launcher_result = false;
//let launcher_cache_check_results = false;
let launcher_render_results = false;
//let run_check_file_cache = false;
let run_render_launcher = false;
let idb_tables_have_records = false;
//let safe_to_render_launcher = false;
// Development variables:
//let account_id = 12;
@@ -349,7 +320,6 @@
clearInterval(open_tables_loop);
idb_tables_opened = true;
run_idb_to_launcher = true; // Set to true so that the launcher object is rebuilt
} else {
console.log('IDB tables have not yet been created or opened');
@@ -358,106 +328,6 @@
}, open_tables_loop_interval);
var update_idb_loop = setInterval(async function() {
//if (run_update_idb) {
// console.log('run_update_idb is set to true');
//console.log('FLAG: axios_headers_set='+axios_headers_set);
//console.log('FLAG: idb_tables_opened='+idb_tables_opened);
if (axios_headers_set && idb_tables_opened) {
if (idb_event_check_datetime < Date.now()) {
console.log('Time to check events');
let v2_account_events = await api.v2_get_account_events(axios, account_id)
.then(function (response) {
for (var i in response) {
tbl_event.setItem(response[i].id.toString(), response[i]);
}
run_idb_to_launcher = true; // Set to true so that the launcher object is rebuilt
safe_to_render_launcher = false; // Set to false so that the launcher render only starts after the launcher object has updated
idb_event_check_datetime = Date.now() + idb_event_check_period;
let render_event_records_result = uiv2.render_event_records(); // NOTE: v2 idb to ui
})
.catch(function (error) {
console.log('Trying to check events. Something went wrong. Expired token?');
console.log(error);
});
}
if (idb_event_location_check_datetime < Date.now()) {
console.log('Time to check event locations');
let v2_event_locations = await api.v2_get_event_locations(axios, event_id);
for (var i in v2_event_locations) {
tbl_event_location.setItem(v2_event_locations[i].id.toString(), v2_event_locations[i]);
}
run_idb_to_launcher = true; // Set to true so that the launcher object is rebuilt
safe_to_render_launcher = false; // Set to false so that the launcher render only starts after the launcher object has updated
idb_event_location_check_datetime = Date.now() + idb_event_location_check_period;
let render_event_location_records_result = uiv2.render_event_location_records(); // NOTE: v2 idb to ui
}
if (idb_event_session_check_datetime < Date.now()) {
console.log('**** *** Time to check event sessions *** ****'); // All event sessions, not just location specific.
let v2_event_sessions = await api.v2_get_event_sessions(axios, event_id); // Was v2_get_event_location_sessions
//tbl_event_session.clear();
for (var i in v2_event_sessions) {
tbl_event_session.setItem(v2_event_sessions[i].id.toString(), v2_event_sessions[i]);
}
//run_idb_to_launcher = true; // Set to true so that the launcher object is rebuilt
safe_to_render_launcher = false; // Set to false so that the launcher render only starts after the launcher object has updated
idb_event_session_check_datetime = Date.now() + idb_event_session_check_period;
let render_event_session_records_result = uiv2.render_event_session_records(); // NOTE: v2 idb to ui
}
if (idb_event_presentation_check_datetime < Date.now()) {
console.log('Time to check event location presentations');
let v2_event_presentations = await api.v2_get_event_location_presentations(axios, event_location_id);
for (var i in v2_event_presentations) {
tbl_event_presentation.setItem(v2_event_presentations[i].id.toString(), v2_event_presentations[i]);
}
run_idb_to_launcher = true; // Set to true so that the launcher object is rebuilt
safe_to_render_launcher = false; // Set to false so that the launcher render only starts after the launcher object has updated
idb_event_presentation_check_datetime = Date.now() + idb_event_presentation_check_period;
let render_event_presentation_records_result = await uiv2.render_event_presentation_records(); // NOTE: v2 idb to ui
}
if (idb_event_presenter_check_datetime < Date.now()) {
console.log('Time to check event location presenters');
let v2_event_presenters = await api.v2_get_event_location_presenters(axios, event_location_id);
for (var i in v2_event_presenters) {
tbl_event_presenter.setItem(v2_event_presenters[i].id.toString(), v2_event_presenters[i]);
}
run_idb_to_launcher = true; // Set to true so that the launcher object is rebuilt
safe_to_render_launcher = false; // Set to false so that the launcher render only starts after the launcher object has updated
idb_event_presenter_check_datetime = Date.now() + idb_event_presenter_check_period;
let render_event_presenter_records_result = await uiv2.render_event_presenter_records(); // NOTE: v2 idb to ui
}
if (idb_event_file_check_datetime < Date.now()) {
console.log('Time to check event location files');
let v2_event_files = await api.v2_get_event_location_files(axios, event_location_id);
for (var i in v2_event_files) {
tbl_event_file.setItem(v2_event_files[i].event_file_id.toString(), v2_event_files[i]);
}
run_idb_to_launcher = true; // Set to true so that the launcher object is rebuilt
safe_to_render_launcher = false; // Set to false so that the launcher render only starts after the launcher object has updated
run_check_file_cache = true; // Set to true so that the local file cache will be checked against the updated event files
idb_event_file_check_datetime = Date.now() + idb_event_file_check_period;
let render_event_file_records_result = await uiv2.render_event_file_records(); // NOTE: v2 idb to ui
}
} else {
console.log('Waiting for the Axios headers to be set and/or IDB tables opened...');
}
//} else {
// console.log('run_update_idb is set to false');
//}
}, update_idb_loop_interval);
var api_token_update_loop = setInterval(function() {
api.api_token_request_async(axios, secret_key).then( function(result) {
console.log('Setting temporary API token to axios Authorization header: '+result);
@@ -468,8 +338,115 @@
}, api_token_update_loop_interval);
var update_render_loop = setInterval(async function() {
}, update_render_loop_interval);
var update_idb_loop = setInterval(async function() {
//console.log('FLAG: axios_headers_set='+axios_headers_set);
//console.log('FLAG: idb_tables_opened='+idb_tables_opened);
if (axios_headers_set && idb_tables_opened) {
if (idb_event_check_datetime < Date.now()) {
console.log('**** *** ** * Time to check events');
let v2_account_events = await api.v2_get_account_events(axios, account_id)
.then(function (response) {
for (var i in response) {
tbl_event.setItem(response[i].id.toString(), response[i]);
}
idb_event_check_datetime = Date.now() + idb_event_check_period;
let render_event_records_result = render_event_records(); // NOTE: v2 idb to ui
})
.catch(function (error) {
console.log('Trying to check events. Something went wrong. Expired token?');
console.log(error);
});
}
if (idb_event_location_check_datetime < Date.now()) {
console.log('**** *** ** * Time to check event locations');
let v2_event_locations = await api.v2_get_event_locations(axios, event_id);
for (var i in v2_event_locations) {
tbl_event_location.setItem(v2_event_locations[i].id.toString(), v2_event_locations[i]);
}
idb_event_location_check_datetime = Date.now() + idb_event_location_check_period;
let render_event_location_records_result = render_event_location_records(); // NOTE: v2 idb to ui
}
if (idb_event_session_check_datetime < Date.now()) {
console.log('**** *** ** * Time to check event sessions'); // All event sessions, not just location specific.
let v2_event_sessions = await api.v2_get_event_sessions(axios, event_id); // Was v2_get_event_location_sessions
//tbl_event_session.clear();
for (var i in v2_event_sessions) {
tbl_event_session.setItem(v2_event_sessions[i].id.toString(), v2_event_sessions[i]);
}
idb_event_session_check_datetime = Date.now() + idb_event_session_check_period;
let render_event_session_records_result = render_event_session_records(); // NOTE: v2 idb to ui
}
if (idb_event_presentation_check_datetime < Date.now()) {
console.log('**** *** ** * Time to check event location presentations');
let v2_event_presentations = await api.v2_get_event_location_presentations(axios, event_location_id);
for (var i in v2_event_presentations) {
tbl_event_presentation.setItem(v2_event_presentations[i].id.toString(), v2_event_presentations[i]);
}
idb_event_presentation_check_datetime = Date.now() + idb_event_presentation_check_period;
let render_event_presentation_records_result = await render_event_presentation_records(); // NOTE: v2 idb to ui
}
if (idb_event_presenter_check_datetime < Date.now()) {
console.log('**** *** ** * Time to check event location presenters');
let v2_event_presenters = await api.v2_get_event_location_presenters(axios, event_location_id);
for (var i in v2_event_presenters) {
tbl_event_presenter.setItem(v2_event_presenters[i].id.toString(), v2_event_presenters[i]);
}
idb_event_presenter_check_datetime = Date.now() + idb_event_presenter_check_period;
let render_event_presenter_records_result = await render_event_presenter_records(); // NOTE: v2 idb to ui
}
if (idb_event_file_check_datetime < Date.now()) {
console.log('**** *** ** * Time to check event location files');
let v2_event_files = await api.v2_get_event_files(axios, event_id);
for (var i in v2_event_files) {
tbl_event_file.setItem(v2_event_files[i].id.toString(), v2_event_files[i]);
}
let v2_event_location_files = await api.v2_get_event_location_files(axios, event_location_id);
for (var i in v2_event_location_files) {
tbl_event_file.setItem(v2_event_location_files[i].id.toString(), v2_event_location_files[i]);
}
let v2_event_location_files_sessions = await api.v2_get_event_location_files_sessions(axios, event_location_id);
for (var i in v2_event_location_files_sessions) {
tbl_event_file.setItem(v2_event_location_files_sessions[i].id.toString(), v2_event_location_files_sessions[i]);
}
let v2_event_location_files_presentations = await api.v2_get_event_location_files_presentations(axios, event_location_id);
for (var i in v2_event_location_files_presentations) {
tbl_event_file.setItem(v2_event_location_files_presentations[i].id.toString(), v2_event_location_files_presentations[i]);
}
let v2_event_location_files_presenters = await api.v2_get_event_location_files_presenters(axios, event_location_id);
for (var i in v2_event_location_files_presenters) {
tbl_event_file.setItem(v2_event_location_files_presenters[i].id.toString(), v2_event_location_files_presenters[i]);
}
run_check_file_cache = true; // Set to true so that the local file cache will be checked against the updated event files
idb_event_file_check_datetime = Date.now() + idb_event_file_check_period;
let render_event_file_records_result = await render_event_file_records(); // NOTE: v2 idb to ui
}
} else {
console.log('Waiting for the Axios headers to be set and/or IDB tables opened...');
}
}, update_idb_loop_interval);
/*var update_render_loop = setInterval(async function() {
}, update_render_loop_interval);*/
var check_file_cache_loop = setInterval(async function() {