Skip to content

Commit c765f0b

Browse files
committed
refactor(monitor): improve process refresh logic and extract refresh kind
Extract process refresh kind into a dedicated function and simplify refresh checks by directly checking process existence after refresh. This makes the code more maintainable and reduces redundant refresh calls.
1 parent 0c1816e commit c765f0b

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/monitor.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::Path;
22

3-
use sysinfo::{Pid, ProcessRefreshKind, ProcessesToUpdate, System};
3+
use sysinfo::{Pid, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind};
44
use log::info;
55

66
pub(crate) struct ProcessMonitor {
@@ -41,7 +41,7 @@ impl ProcessMonitor {
4141

4242
// Slow path: full system scan (when we don't have cached PIDs or they became invalid)
4343
self.system
44-
.refresh_processes_specifics(ProcessesToUpdate::All, true, ProcessRefreshKind::nothing()); // we need nothing, no cpu, no memory, just basics
44+
.refresh_processes_specifics(ProcessesToUpdate::All, true, process_refresh_kind());
4545
let normalized_project_path = normalize_path(&self.target_project_path.as_str());
4646

4747
if self.unity_pid().is_none() {
@@ -101,10 +101,10 @@ impl ProcessMonitor {
101101

102102
let unity_pid = self.unity_pid().unwrap();
103103

104-
if self
104+
self
105105
.system
106-
.refresh_processes_specifics(ProcessesToUpdate::Some(&[unity_pid]), false, ProcessRefreshKind::nothing()) == 0
107-
{
106+
.refresh_processes_specifics(ProcessesToUpdate::Some(&[unity_pid]), true, process_refresh_kind());
107+
if self.system.process(unity_pid).is_none() {
108108
self.set_unity_pid(None);
109109
}
110110
}
@@ -119,10 +119,10 @@ impl ProcessMonitor {
119119

120120
let hot_reload_pid = self.hot_reload_pid().unwrap();
121121

122-
if self
122+
self
123123
.system
124-
.refresh_processes_specifics(ProcessesToUpdate::Some(&[hot_reload_pid]), false, ProcessRefreshKind::nothing()) == 0
125-
{
124+
.refresh_processes_specifics(ProcessesToUpdate::Some(&[hot_reload_pid]), true, process_refresh_kind());
125+
if self.system.process(hot_reload_pid).is_none() {
126126
self.set_hot_reload_pid(None);
127127
}
128128
}
@@ -267,3 +267,9 @@ pub(crate) fn extract_hot_reload_project_path(process: &sysinfo::Process) -> Opt
267267

268268
None
269269
}
270+
271+
/// create process refresh kind
272+
fn process_refresh_kind() -> ProcessRefreshKind {
273+
// we need nothing, no cpu, no memory, just basics
274+
ProcessRefreshKind::nothing().with_cmd(UpdateKind::Always)
275+
}

0 commit comments

Comments
 (0)