ソースを参照

feat: add update interval

GyDi 3 年 前
コミット
cb661aaebd

+ 5 - 1
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

+ 9 - 0
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<String, (TaskID, u64)>,
 
+  /// increment id
   timer_count: TaskID,
 
+  /// save the instance of the app
   core: Option<Core>,
 }
 
@@ -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),

+ 23 - 6
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()}
         />
 
         <TextField
@@ -93,6 +94,7 @@ const ProfileEdit = (props: Props) => {
           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 && (
-          <TextField
-            {...textFieldProps}
-            label="User Agent"
-            value={option.user_agent}
-            onChange={(e) => setOption({ user_agent: e.target.value })}
-          />
+          <>
+            <TextField
+              {...textFieldProps}
+              label="User Agent"
+              value={option.user_agent}
+              onChange={(e) => setOption({ user_agent: e.target.value })}
+              onKeyDown={(e) => e.key === "Enter" && onUpdate()}
+            />
+
+            <TextField
+              {...textFieldProps}
+              label="Update Interval (mins)"
+              value={option.update_interval}
+              onChange={(e) => {
+                const str = e.target.value?.replace(/\D/, "");
+                setOption({ update_interval: str != null ? +str : str });
+              }}
+              onKeyDown={(e) => e.key === "Enter" && onUpdate()}
+            />
+          </>
         )}
       </DialogContent>
 

+ 2 - 1
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 {