Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src-tauri/src/filesystem/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use notify::Event;
use std::fs::{self, File};
use std::io::{BufReader, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, MutexGuard};
use std::time::Duration;
use tokio::time;

lazy_static! {
pub static ref CACHE_FILE_PATH: String = {
Expand Down Expand Up @@ -127,17 +127,30 @@ impl FsEventHandler {
/// Starts a constant interval loop where the cache is updated every ~30 seconds.
pub fn run_cache_interval(state_mux: &StateSafe) {
let state_clone = Arc::clone(state_mux);
let task_running = Arc::new(AtomicBool::new(false));

tokio::spawn(async move {
// We use tokio spawn because async closures with std spawn is unstable
let mut interval = time::interval(Duration::from_secs(30));
let mut interval = tokio::time::interval(Duration::from_secs(30));
interval.tick().await; // Wait 30 seconds before doing first re-cache

loop {
interval.tick().await;

let guard = &mut state_clone.lock().unwrap();
save_to_cache(guard);
if !task_running.load(Ordering::SeqCst) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be swap instead?

task_running.store(true, Ordering::SeqCst);

let state_clone = Arc::clone(&state_clone);
let task_running = Arc::clone(&task_running);

tokio::spawn(async move {
let guard = &mut state_clone.lock().unwrap();
save_to_cache(guard);

task_running.store(false, Ordering::SeqCst);
});
} else {
println!("Task running, won't re-cache");
}
}
});
}
Expand Down