Skip to content

Commit 9f071a9

Browse files
committed
fix: properly handle FnOnce closure in onopen callback
Use Rc<RefCell<Option<>>> pattern to allow FnOnce closure to work with FnMut requirement, fixing compilation error in River.
1 parent e61a230 commit 9f071a9

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "freenet-stdlib"
3-
version = "0.1.21"
3+
version = "0.1.22"
44
edition = "2021"
55
rust-version = "1.71.1"
66
publish = true

rust/src/client_api/browser.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ impl WebApi {
8888
conn.set_onerror(Some(onerror_callback.as_ref().unchecked_ref()));
8989
onerror_callback.forget();
9090

91+
let onopen_handler = Rc::new(RefCell::new(Some(onopen_handler)));
9192
let onopen_callback = Closure::wrap(Box::new(move || {
92-
onopen_handler();
93+
if let Some(handler) = onopen_handler.borrow_mut().take() {
94+
handler();
95+
}
9396
}) as Box<dyn FnMut()>);
9497
// conn.add_event_listener_with_callback("open", onopen_callback.as_ref().unchecked_ref());
9598
conn.set_onopen(Some(onopen_callback.as_ref().unchecked_ref()));

0 commit comments

Comments
 (0)