feat: add update interval

This commit is contained in:
GyDi 2022-04-21 14:26:41 +08:00 committed by GitHub
parent 573571978c
commit cb661aaebd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 8 deletions

View File

@ -112,8 +112,12 @@ pub fn delete_profile(index: String, core: State<'_, Core>) -> CmdResult {
#[tauri::command] #[tauri::command]
pub fn patch_profile(index: String, profile: PrfItem, core: State<'_, Core>) -> CmdResult { pub fn patch_profile(index: String, profile: PrfItem, core: State<'_, Core>) -> CmdResult {
let mut profiles = core.profiles.lock(); 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 /// run vscode command to edit the profile

View File

@ -7,12 +7,16 @@ use std::collections::HashMap;
type TaskID = u64; type TaskID = u64;
pub struct Timer { pub struct Timer {
/// cron manager
delay_timer: DelayTimer, delay_timer: DelayTimer,
/// save the current state
timer_map: HashMap<String, (TaskID, u64)>, timer_map: HashMap<String, (TaskID, u64)>,
/// increment id
timer_count: TaskID, timer_count: TaskID,
/// save the instance of the app
core: Option<Core>, core: Option<Core>,
} }
@ -41,12 +45,15 @@ impl Timer {
for (uid, diff) in diff_map.into_iter() { for (uid, diff) in diff_map.into_iter() {
match diff { match diff {
DiffFlag::Del(tid) => { DiffFlag::Del(tid) => {
let _ = self.timer_map.remove(&uid);
log_if_err!(self.delay_timer.remove_task(tid)); log_if_err!(self.delay_timer.remove_task(tid));
} }
DiffFlag::Add(tid, val) => { DiffFlag::Add(tid, val) => {
let _ = self.timer_map.insert(uid.clone(), (tid, val));
log_if_err!(self.add_task(uid, tid, val)); log_if_err!(self.add_task(uid, tid, val));
} }
DiffFlag::Mod(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.delay_timer.remove_task(tid));
log_if_err!(self.add_task(uid, tid, val)); log_if_err!(self.add_task(uid, tid, val));
} }
@ -116,6 +123,7 @@ impl Timer {
let task = TaskBuilder::default() let task = TaskBuilder::default()
.set_task_id(tid) .set_task_id(tid)
.set_maximum_parallel_runnable_num(1)
.set_frequency_repeated_by_minutes(minutes) .set_frequency_repeated_by_minutes(minutes)
// .set_frequency_repeated_by_seconds(minutes) // for test // .set_frequency_repeated_by_seconds(minutes) // for test
.spawn_async_routine(move || Self::async_task(core.clone(), uid.clone())) .spawn_async_routine(move || Self::async_task(core.clone(), uid.clone()))
@ -136,6 +144,7 @@ impl Timer {
} }
} }
#[derive(Debug)]
enum DiffFlag { enum DiffFlag {
Del(TaskID), Del(TaskID),
Add(TaskID, u64), Add(TaskID, u64),

View File

@ -86,6 +86,7 @@ const ProfileEdit = (props: Props) => {
label="Name" label="Name"
value={form.name} value={form.name}
onChange={(e) => setForm({ name: e.target.value })} onChange={(e) => setForm({ name: e.target.value })}
onKeyDown={(e) => e.key === "Enter" && onUpdate()}
/> />
<TextField <TextField
@ -93,6 +94,7 @@ const ProfileEdit = (props: Props) => {
label="Descriptions" label="Descriptions"
value={form.desc} value={form.desc}
onChange={(e) => setForm({ desc: e.target.value })} onChange={(e) => setForm({ desc: e.target.value })}
onKeyDown={(e) => e.key === "Enter" && onUpdate()}
/> />
{type === "remote" && ( {type === "remote" && (
@ -101,16 +103,31 @@ const ProfileEdit = (props: Props) => {
label="Subscription Url" label="Subscription Url"
value={form.url} value={form.url}
onChange={(e) => setForm({ url: e.target.value })} onChange={(e) => setForm({ url: e.target.value })}
onKeyDown={(e) => e.key === "Enter" && onUpdate()}
/> />
)} )}
{showOpt && ( {showOpt && (
<TextField <>
{...textFieldProps} <TextField
label="User Agent" {...textFieldProps}
value={option.user_agent} label="User Agent"
onChange={(e) => setOption({ user_agent: e.target.value })} 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> </DialogContent>

View File

@ -1,7 +1,7 @@
/** /**
* Some interface for clash api * Some interface for clash api
*/ */
export namespace ApiType { export namespace ApiType {
export interface ConfigData { export interface ConfigData {
port: number; port: number;
mode: string; mode: string;
@ -113,6 +113,7 @@ export namespace CmdType {
export interface ProfileOption { export interface ProfileOption {
user_agent?: string; user_agent?: string;
with_proxy?: boolean; with_proxy?: boolean;
update_interval?: number;
} }
export interface ProfilesConfig { export interface ProfilesConfig {