mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-03-24 13:41:02 +03:00
Optimize clipboard change listener
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use super::*;
|
||||
use std::{
|
||||
sync::{self, mpsc::Sender},
|
||||
thread::{self, JoinHandle},
|
||||
time,
|
||||
};
|
||||
@@ -153,6 +154,39 @@ impl<T: Subscriber + From<ConnInner>> ServiceTmpl<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn listen<S, N, F>(&self, notify: N, callback: F)
|
||||
where
|
||||
N: 'static + FnMut(Sender<bool>) -> ResultType<()> + Send,
|
||||
F: 'static + FnMut(Self, &mut S) -> ResultType<()> + Send,
|
||||
S: 'static + Default + Reset,
|
||||
{
|
||||
let mut notify = notify;
|
||||
let mut callback = callback;
|
||||
let sp = self.clone();
|
||||
let (tx, rx) = sync::mpsc::channel();
|
||||
thread::spawn(move || {
|
||||
let _ = notify(tx);
|
||||
});
|
||||
|
||||
let thread = thread::spawn(move || {
|
||||
let mut state = S::default();
|
||||
while let Ok(changed) = rx.recv() {
|
||||
if changed && sp.active() {
|
||||
if sp.has_subscribes() {
|
||||
if let Err(err) = callback(sp.clone(), &mut state) {
|
||||
log::error!("Error of {} service: {}", sp.name(), err);
|
||||
#[cfg(windows)]
|
||||
crate::platform::windows::try_change_desktop();
|
||||
}
|
||||
} else {
|
||||
state.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
self.0.write().unwrap().handle = Some(thread);
|
||||
}
|
||||
|
||||
pub fn repeat<S, F>(&self, interval_ms: u64, callback: F)
|
||||
where
|
||||
F: 'static + FnMut(Self, &mut S) -> ResultType<()> + Send,
|
||||
|
||||
Reference in New Issue
Block a user