tauri-apps / wry

Cross-platform WebView library in Rust for Tauri.
Apache License 2.0
3.73k stars 284 forks source link

Allow overriding `ipc_handler` on WebView instance #1407

Closed twlite closed 2 weeks ago

twlite commented 3 weeks ago

Is your feature request related to a problem? Please describe.
I'm unable to modify or replace the ipc_handler directly on an existing WebView instance. While <WebViewBuilder>.with_ipc_handler(handler) allows setting the handler during the building phase, I haven't found a way to change it afterwards, which limits flexibility when the ipc_handler needs to be updated dynamically.

Describe the solution you'd like
I would like to have an option to replace or override the ipc_handler on an existing WebView instance, allowing updates to the handler without needing to rebuild the entire WebView.

Describe alternatives you've considered
I've reviewed the available documentation and attempted various approaches but couldn't find any method to achieve this. Rebuilding the WebView is possible, but it’s inefficient and not feasible in cases where the handler needs to be updated frequently.

Additional context
This feature would improve flexibility when managing IPC communication in a dynamic environment. Any pointers to a workaround, if available, would also be appreciated.

amrbashir commented 2 weeks ago

Generally you don't need to replace the ipc_handler you can just replace the data captured in the closure, for example:

struct MyData {};

let x = Rc::new(RefCell::new(None::<MyData>));

let x_c = x.clone();
let ipc_handler = || {
    if let Some(my_data) = x_c.borrow() {

    }
};

// some_logic

// then set data inside `x`
*x.borrow_mut() = Some(MyData {})