Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3080a58c4a |
@@ -1641,29 +1641,56 @@ SPDX-License-Identifier: GPL-2.0-or-later
|
||||
'settings': ['/vchan.js', '/scheduler.js']
|
||||
};
|
||||
var loaded = new Set();
|
||||
function loadPlugins(tab) {
|
||||
var scripts = pluginScripts[tab];
|
||||
if (!scripts) return;
|
||||
scripts.forEach(function(src) {
|
||||
if (loaded.has(src)) return;
|
||||
loaded.add(src);
|
||||
var loading = new Map();
|
||||
|
||||
function loadScript(src) {
|
||||
if (loaded.has(src)) return Promise.resolve();
|
||||
if (loading.has(src)) return loading.get(src);
|
||||
|
||||
var request = new Promise(function(resolve, reject) {
|
||||
var s = document.createElement('script');
|
||||
s.src = src;
|
||||
s.defer = true;
|
||||
s.onload = function() {
|
||||
loaded.add(src);
|
||||
loading.delete(src);
|
||||
resolve();
|
||||
};
|
||||
s.onerror = function() {
|
||||
loading.delete(src);
|
||||
reject(new Error('Failed to load plugin script: ' + src));
|
||||
};
|
||||
document.body.appendChild(s);
|
||||
});
|
||||
loading.set(src, request);
|
||||
return request;
|
||||
}
|
||||
|
||||
function loadPlugins(tab) {
|
||||
var scripts = pluginScripts[tab];
|
||||
if (!scripts) return Promise.resolve();
|
||||
return scripts.reduce(function(sequence, src) {
|
||||
return sequence.then(function() { return loadScript(src); });
|
||||
}, Promise.resolve());
|
||||
}
|
||||
|
||||
function requestPlugins(tab) {
|
||||
return loadPlugins(tab).catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
// Eager plugin loading is triggered by app.js (after window.trx is set up)
|
||||
// via window.loadEagerPlugins(). Dynamic scripts are effectively async, so
|
||||
// loading them before app.js would cause map-core.js to crash when
|
||||
// window.trx is not yet defined.
|
||||
window.loadEagerPlugins = function() {
|
||||
['digital-modes', 'map-data', 'bookmarks', 'settings'].forEach(loadPlugins);
|
||||
return Promise.all(
|
||||
['digital-modes', 'map-data', 'bookmarks', 'settings'].map(requestPlugins)
|
||||
);
|
||||
};
|
||||
// Load others on tab switch
|
||||
document.addEventListener('click', function(e) {
|
||||
var tab = e.target.closest('[data-tab]');
|
||||
if (tab) loadPlugins(tab.dataset.tab);
|
||||
if (tab) requestPlugins(tab.dataset.tab);
|
||||
});
|
||||
window.loadPluginsForTab = loadPlugins;
|
||||
})();
|
||||
|
||||
@@ -185,38 +185,20 @@ pub fn flush_all(db: &mut PickleDb, rig_id: &str, histories: &Arc<DecoderHistori
|
||||
let _ = db.dump();
|
||||
}
|
||||
|
||||
fn flush_all_rigs(db: &Mutex<PickleDb>, rig_histories: &[(String, Arc<DecoderHistories>)]) {
|
||||
let Ok(mut guard) = db.lock() else {
|
||||
tracing::warn!("history database mutex poisoned; skipping periodic flush");
|
||||
return;
|
||||
};
|
||||
for (rig_id, histories) in rig_histories {
|
||||
flush_all(&mut guard, rig_id, histories);
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn a Tokio task that flushes all rigs' histories to disk every 60 seconds.
|
||||
///
|
||||
/// Snapshot cloning, JSON serialization, and disk I/O run on Tokio's blocking
|
||||
/// pool so a large history database cannot stall an async runtime worker.
|
||||
pub fn spawn_flush_task(
|
||||
db: Arc<Mutex<PickleDb>>,
|
||||
rig_histories: Vec<(String, Arc<DecoderHistories>)>,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
let rig_histories = Arc::new(rig_histories);
|
||||
let mut interval = tokio::time::interval(Duration::from_secs(60));
|
||||
interval.tick().await; // consume the immediate first tick
|
||||
loop {
|
||||
interval.tick().await;
|
||||
let db = Arc::clone(&db);
|
||||
let rig_histories = Arc::clone(&rig_histories);
|
||||
if let Err(err) = tokio::task::spawn_blocking(move || {
|
||||
flush_all_rigs(&db, rig_histories.as_slice());
|
||||
})
|
||||
.await
|
||||
{
|
||||
tracing::warn!(error = %err, "history flush worker failed");
|
||||
if let Ok(mut guard) = db.lock() {
|
||||
for (rig_id, histories) in &rig_histories {
|
||||
flush_all(&mut guard, rig_id, histories);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user