1111//! storage access.
1212
1313use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
14+ use std:: time:: Duration ;
1415
1516use serde_json:: { json, Value } ;
1617use tauri:: menu:: { MenuBuilder , MenuItemBuilder } ;
@@ -289,6 +290,7 @@ fn apply_side_effects(
289290 . any ( |k| * k == "http_api_on" || * k == "http_api_only_local" ) ;
290291 let touches_locale = touched_keys. iter ( ) . any ( |k| * k == "locale" ) ;
291292 let touches_tray_title = touched_keys. iter ( ) . any ( |k| * k == "show_title_on_tray" ) ;
293+ let touches_auto_update = touched_keys. iter ( ) . any ( |k| * k == "auto_check_update" ) ;
292294
293295 if touches_locale {
294296 if let Err ( e) = app_menu:: refresh ( app) {
@@ -350,6 +352,19 @@ fn apply_side_effects(
350352 http_api:: stop ( ) ;
351353 }
352354 }
355+
356+ if touches_auto_update {
357+ let enabled = {
358+ let cfg = state. config . lock ( ) . expect ( "config mutex poisoned" ) ;
359+ cfg. auto_check_update
360+ } ;
361+ if enabled {
362+ let app = app. clone ( ) ;
363+ tauri:: async_runtime:: spawn ( async move {
364+ run_auto_update_check ( & app) . await ;
365+ } ) ;
366+ }
367+ }
353368}
354369
355370// ---- list / tree -----------------------------------------------------------
@@ -1256,13 +1271,42 @@ async fn fetch_url(client: &reqwest::Client, url: &str) -> Result<String, String
12561271
12571272// ---- updater ---------------------------------------------------------------
12581273
1259- #[ tauri:: command]
1260- pub async fn check_update < R : Runtime > (
1261- app : AppHandle < R > ,
1262- state : State < ' _ , AppState > ,
1263- _args : Args ,
1274+ const AUTO_UPDATE_CHECK_INTERVAL : Duration = Duration :: from_secs ( 12 * 60 * 60 ) ;
1275+
1276+ pub fn start_auto_update_checker < R : Runtime + ' static > ( app : AppHandle < R > ) {
1277+ tauri:: async_runtime:: spawn ( async move {
1278+ run_auto_update_check ( & app) . await ;
1279+
1280+ loop {
1281+ tokio:: time:: sleep ( AUTO_UPDATE_CHECK_INTERVAL ) . await ;
1282+ run_auto_update_check ( & app) . await ;
1283+ }
1284+ } ) ;
1285+ }
1286+
1287+ async fn run_auto_update_check < R : Runtime > ( app : & AppHandle < R > ) {
1288+ let state = app. state :: < AppState > ( ) ;
1289+ let auto_check_update = state
1290+ . config
1291+ . lock ( )
1292+ . map ( |cfg| cfg. auto_check_update )
1293+ . unwrap_or ( false ) ;
1294+
1295+ if !auto_check_update {
1296+ return ;
1297+ }
1298+
1299+ if let Err ( e) = check_update_inner ( app, state. inner ( ) ) . await {
1300+ log:: info!( "automatic update check failed: {e}" ) ;
1301+ }
1302+ }
1303+
1304+ async fn check_update_inner < R : Runtime > (
1305+ app : & AppHandle < R > ,
1306+ state : & AppState ,
12641307) -> Result < Value , String > {
1265- let updater = updater_builder_with_proxy ( & app, state. inner ( ) ) ?
1308+ let _guard = state. update_check_lock . lock ( ) . await ;
1309+ let updater = updater_builder_with_proxy ( app, state) ?
12661310 . build ( )
12671311 . map_err ( |e| e. to_string ( ) ) ?;
12681312 let update = updater. check ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
@@ -1281,12 +1325,22 @@ pub async fn check_update<R: Runtime>(
12811325 }
12821326}
12831327
1328+ #[ tauri:: command]
1329+ pub async fn check_update < R : Runtime > (
1330+ app : AppHandle < R > ,
1331+ state : State < ' _ , AppState > ,
1332+ _args : Args ,
1333+ ) -> Result < Value , String > {
1334+ check_update_inner ( & app, state. inner ( ) ) . await
1335+ }
1336+
12841337#[ tauri:: command]
12851338pub async fn download_update < R : Runtime > (
12861339 app : AppHandle < R > ,
12871340 state : State < ' _ , AppState > ,
12881341 _args : Args ,
12891342) -> Result < Value , String > {
1343+ let _guard = state. update_check_lock . lock ( ) . await ;
12901344 let updater = updater_builder_with_proxy ( & app, state. inner ( ) ) ?
12911345 . build ( )
12921346 . map_err ( |e| e. to_string ( ) ) ?;
0 commit comments