From 4de126a16f46cee25f31bd35169b41d5bc6c135f Mon Sep 17 00:00:00 2001 From: SpikeHD <25207995+SpikeHD@users.noreply.github.com> Date: Wed, 27 Nov 2024 10:17:59 -0800 Subject: [PATCH] fix: callbacks are fnmut --- examples/commands.rs | 41 +++++++++++++++++++++++++---------------- src/cdp/mod.rs | 2 +- src/ipc.rs | 8 +++++--- src/lib.rs | 2 +- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/examples/commands.rs b/examples/commands.rs index 6c7a9f0..5be0738 100644 --- a/examples/commands.rs +++ b/examples/commands.rs @@ -11,28 +11,37 @@ fn main() -> Result<(), CrowserError> { let mut window = Window::new(config, None, profile_dir)?; let ipc = window.ipc(); + let ipc_thread = std::thread::spawn(move || setup_commands(&ipc)); + window.clear_profile().unwrap_or_default(); std::thread::spawn(move || { - ipc.block_until_initialized().unwrap_or_default(); - - ipc - .register_command("hello", |_| { - println!("Got hello command"); - Ok(serde_json::json!("Hello from Crowser!")) - }) - .unwrap_or_default(); - - std::thread::sleep(std::time::Duration::from_secs(1)); - - // Eval some JS that calls that command - let result = ipc - .eval("window.__CROWSER.ipc.invoke('hello')") - .unwrap_or_default(); - println!("Result: {:?}", result); + }); window.create()?; + ipc_thread.join().expect("Failed to join IPC thread").expect("IPC thread panicked"); + Ok(()) } + +fn setup_commands(ipc: &crowser::WindowIpc) -> Result<(), CrowserError> { + ipc.block_until_initialized()?; + + ipc + .register_command("hello", |_| { + println!("Got hello command"); + Ok(serde_json::json!("Hello from Crowser!")) + })?; + + std::thread::sleep(std::time::Duration::from_secs(1)); + + println!("Waiting for result..."); + // Eval some JS that calls that command + let result = ipc + .eval("window.__CROWSER.ipc.invoke('hello')")?; + println!("Result: {:?}", result); + + Ok(()) +} \ No newline at end of file diff --git a/src/cdp/mod.rs b/src/cdp/mod.rs index f01c555..e4cb58c 100644 --- a/src/cdp/mod.rs +++ b/src/cdp/mod.rs @@ -167,7 +167,7 @@ impl Cdp { let timeout = timeout.unwrap_or(std::time::Duration::from_secs(1)); let now = std::time::Instant::now(); - // Wait for a response in the manager using try_lock and the timeout + // Wait for a response loop { // Wait an amount of time, to prevent a tight loop std::thread::sleep(std::time::Duration::from_millis(1)); diff --git a/src/ipc.rs b/src/ipc.rs index c25bf3a..c4043be 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -17,7 +17,7 @@ use crate::{ }; type IpcRegistrationMap = - Arc Result + Send + Sync>>>>>; + Arc Result + Send + Sync + 'static>>>>>; #[derive(Clone)] pub struct BrowserIpc { @@ -324,7 +324,9 @@ impl BrowserIpc { }; let cmd = CDPCommand::new("Runtime.evaluate", params, Some(self.session_id.clone())); + println!("Before send: {:?}", cmd); let result = cdp.send(cmd, None)?; + println!("After send: {:?}", result["result"]["result"]); let res_type = result["result"]["result"]["type"] .as_str() .unwrap_or_default(); @@ -345,7 +347,7 @@ impl BrowserIpc { pub fn register_command( &mut self, name: impl AsRef, - callback: fn(Value) -> Result, + callback: impl FnMut(Value) -> Result + Send + Sync + 'static, ) -> Result<(), CrowserError> { let mut commands = self.commands.lock().unwrap(); @@ -363,7 +365,7 @@ impl BrowserIpc { pub fn listen( &mut self, name: impl AsRef, - callback: Box Result + Send + Sync>, + callback: Box Result + Send + Sync + 'static>, ) -> Result<(), CrowserError> { let mut listeners = self.listeners.lock().unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 1a16469..57a5bcc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -256,7 +256,7 @@ impl WindowIpc { pub fn register_command( &self, name: impl AsRef, - callback: fn(Value) -> Result, + callback: impl FnMut(Value) -> Result + Send + Sync + 'static, ) -> Result<(), CrowserError> { let mut ipc = self.inner.lock().unwrap();