From cb661aaebde80e8c6dcca1eba996053fc81de25e Mon Sep 17 00:00:00 2001 From: GyDi Date: Thu, 21 Apr 2022 14:26:41 +0800 Subject: [PATCH] feat: add update interval --- src-tauri/src/cmds.rs | 6 ++++- src-tauri/src/core/timer.rs | 9 ++++++++ src/components/profile/profile-edit.tsx | 29 ++++++++++++++++++++----- src/services/types.ts | 3 ++- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index 9d4b6b3..5edc60c 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -112,8 +112,12 @@ pub fn delete_profile(index: String, core: State<'_, Core>) -> CmdResult { #[tauri::command] pub fn patch_profile(index: String, profile: PrfItem, core: State<'_, Core>) -> CmdResult { let mut profiles = core.profiles.lock(); + wrap_err!(profiles.patch_item(index, profile))?; + drop(profiles); - wrap_err!(profiles.patch_item(index, profile)) + // update cron task + let mut timer = core.timer.lock(); + wrap_err!(timer.refresh()) } /// run vscode command to edit the profile diff --git a/src-tauri/src/core/timer.rs b/src-tauri/src/core/timer.rs index e857e9d..5bb0bbd 100644 --- a/src-tauri/src/core/timer.rs +++ b/src-tauri/src/core/timer.rs @@ -7,12 +7,16 @@ use std::collections::HashMap; type TaskID = u64; pub struct Timer { + /// cron manager delay_timer: DelayTimer, + /// save the current state timer_map: HashMap, + /// increment id timer_count: TaskID, + /// save the instance of the app core: Option, } @@ -41,12 +45,15 @@ impl Timer { for (uid, diff) in diff_map.into_iter() { match diff { DiffFlag::Del(tid) => { + let _ = self.timer_map.remove(&uid); log_if_err!(self.delay_timer.remove_task(tid)); } DiffFlag::Add(tid, val) => { + let _ = self.timer_map.insert(uid.clone(), (tid, val)); log_if_err!(self.add_task(uid, tid, val)); } DiffFlag::Mod(tid, val) => { + let _ = self.timer_map.insert(uid.clone(), (tid, val)); log_if_err!(self.delay_timer.remove_task(tid)); log_if_err!(self.add_task(uid, tid, val)); } @@ -116,6 +123,7 @@ impl Timer { let task = TaskBuilder::default() .set_task_id(tid) + .set_maximum_parallel_runnable_num(1) .set_frequency_repeated_by_minutes(minutes) // .set_frequency_repeated_by_seconds(minutes) // for test .spawn_async_routine(move || Self::async_task(core.clone(), uid.clone())) @@ -136,6 +144,7 @@ impl Timer { } } +#[derive(Debug)] enum DiffFlag { Del(TaskID), Add(TaskID, u64), diff --git a/src/components/profile/profile-edit.tsx b/src/components/profile/profile-edit.tsx index 871d69a..c3f8abd 100644 --- a/src/components/profile/profile-edit.tsx +++ b/src/components/profile/profile-edit.tsx @@ -86,6 +86,7 @@ const ProfileEdit = (props: Props) => { label="Name" value={form.name} onChange={(e) => setForm({ name: e.target.value })} + onKeyDown={(e) => e.key === "Enter" && onUpdate()} /> { label="Descriptions" value={form.desc} onChange={(e) => setForm({ desc: e.target.value })} + onKeyDown={(e) => e.key === "Enter" && onUpdate()} /> {type === "remote" && ( @@ -101,16 +103,31 @@ const ProfileEdit = (props: Props) => { label="Subscription Url" value={form.url} onChange={(e) => setForm({ url: e.target.value })} + onKeyDown={(e) => e.key === "Enter" && onUpdate()} /> )} {showOpt && ( - setOption({ user_agent: e.target.value })} - /> + <> + setOption({ user_agent: e.target.value })} + onKeyDown={(e) => e.key === "Enter" && onUpdate()} + /> + + { + const str = e.target.value?.replace(/\D/, ""); + setOption({ update_interval: str != null ? +str : str }); + }} + onKeyDown={(e) => e.key === "Enter" && onUpdate()} + /> + )} diff --git a/src/services/types.ts b/src/services/types.ts index 4858679..9e76a8e 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -1,7 +1,7 @@ /** * Some interface for clash api */ -export namespace ApiType { + export namespace ApiType { export interface ConfigData { port: number; mode: string; @@ -113,6 +113,7 @@ export namespace CmdType { export interface ProfileOption { user_agent?: string; with_proxy?: boolean; + update_interval?: number; } export interface ProfilesConfig {