Skip to content

Commit

Permalink
fix: callbacks are fnmut
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Nov 27, 2024
1 parent 88140ab commit 4de126a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
41 changes: 25 additions & 16 deletions examples/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
2 changes: 1 addition & 1 deletion src/cdp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 5 additions & 3 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
};

type IpcRegistrationMap =
Arc<Mutex<HashMap<String, Vec<Box<dyn Fn(Value) -> Result<Value, CrowserError> + Send + Sync>>>>>;
Arc<Mutex<HashMap<String, Vec<Box<dyn FnMut(Value) -> Result<Value, CrowserError> + Send + Sync + 'static>>>>>;

#[derive(Clone)]
pub struct BrowserIpc {
Expand Down Expand Up @@ -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();
Expand All @@ -345,7 +347,7 @@ impl BrowserIpc {
pub fn register_command(
&mut self,
name: impl AsRef<str>,
callback: fn(Value) -> Result<Value, CrowserError>,
callback: impl FnMut(Value) -> Result<Value, CrowserError> + Send + Sync + 'static,
) -> Result<(), CrowserError> {
let mut commands = self.commands.lock().unwrap();

Expand All @@ -363,7 +365,7 @@ impl BrowserIpc {
pub fn listen(
&mut self,
name: impl AsRef<str>,
callback: Box<dyn Fn(Value) -> Result<Value, CrowserError> + Send + Sync>,
callback: Box<dyn FnMut(Value) -> Result<Value, CrowserError> + Send + Sync + 'static>,
) -> Result<(), CrowserError> {
let mut listeners = self.listeners.lock().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl WindowIpc {
pub fn register_command(
&self,
name: impl AsRef<str>,
callback: fn(Value) -> Result<Value, CrowserError>,
callback: impl FnMut(Value) -> Result<Value, CrowserError> + Send + Sync + 'static,
) -> Result<(), CrowserError> {
let mut ipc = self.inner.lock().unwrap();

Expand Down

0 comments on commit 4de126a

Please sign in to comment.