Skip to content

Commit

Permalink
feat: add update interval
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi authored Apr 21, 2022
1 parent 5735719 commit cb661aa
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src-tauri/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src-tauri/src/core/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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()))
Expand All @@ -136,6 +144,7 @@ impl Timer {
}
}

#[derive(Debug)]
enum DiffFlag {
Del(TaskID),
Add(TaskID, u64),
Expand Down
29 changes: 23 additions & 6 deletions src/components/profile/profile-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ const ProfileEdit = (props: Props) => {
label="Name"
value={form.name}
onChange={(e) => setForm({ name: e.target.value })}
onKeyDown={(e) => e.key === "Enter" && onUpdate()}
/>

<TextField
{...textFieldProps}
label="Descriptions"
value={form.desc}
onChange={(e) => setForm({ desc: e.target.value })}
onKeyDown={(e) => e.key === "Enter" && onUpdate()}
/>

{type === "remote" && (
Expand All @@ -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>

Expand Down
3 changes: 2 additions & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Some interface for clash api
*/
export namespace ApiType {
export namespace ApiType {
export interface ConfigData {
port: number;
mode: string;
Expand Down Expand Up @@ -113,6 +113,7 @@ export namespace CmdType {
export interface ProfileOption {
user_agent?: string;
with_proxy?: boolean;
update_interval?: number;
}

export interface ProfilesConfig {
Expand Down

0 comments on commit cb661aa

Please sign in to comment.