1
0
Fork 0
mirror of https://github.com/icewind1991/clipboard-sync synced 2026-06-03 10:24:06 +02:00

dont make a clipboard context for every incoming message

This commit is contained in:
Robin Appelman 2019-06-15 14:37:21 +02:00
commit 2fa67d45f0
4 changed files with 8 additions and 4 deletions

1
Cargo.lock generated
View file

@ -881,6 +881,7 @@ dependencies = [
"mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)",
"mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl 0.10.23 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -18,5 +18,5 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clipboard = "0.5"
env_logger = "0.6"
ws = {version = "0.8", features = ["native-tls"]}
ws = {version = "0.8", features = ["ssl", "native-tls"]}
err-derive = "0.1"

View file

@ -1,5 +1,6 @@
use crate::common::ClipboardCommand;
use clipboard::{ClipboardContext, ClipboardProvider};
use std::cell::RefCell;
use std::convert::TryFrom;
use std::env;
use std::sync::{Arc, Mutex};
@ -14,7 +15,7 @@ fn handle_command(
ctx: &mut ClipboardContext,
current_clipboard: Arc<Mutex<String>>,
) {
if let ClipboardCommand::Set { value, session: _ } = command {
if let ClipboardCommand::Set { value, .. } = command {
let mut clip = current_clipboard.lock().unwrap();
if *clip != value {
let _ = ctx.set_contents(value.clone());
@ -41,10 +42,11 @@ fn main() {
let current_clipboard = Arc::new(Mutex::new(String::new()));
clipboard_thread(session.clone(), out, current_clipboard.clone());
let ctx: RefCell<ClipboardContext> = RefCell::new(ClipboardProvider::new().unwrap());
move |msg: Message| {
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
if let Ok(command) = ClipboardCommand::try_from(msg) {
handle_command(command, &mut ctx, current_clipboard.clone());
handle_command(command, &mut ctx.borrow_mut(), current_clipboard.clone());
}
Ok(())
}

View file

@ -5,6 +5,7 @@ use std::convert::TryFrom;
use ws::{Error as WsError, ErrorKind, Message};
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
#[serde(rename_all = "lowercase")]
pub enum ClipboardCommand {
Listen { session: String },