Skip to content

Commit

Permalink
lightclient: Construct one time only closures
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Nov 24, 2023
1 parent f3eb4cd commit cfac283
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lightclient/src/platform/wasm_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ struct InnerWasmSocket {
///
/// These need to be kept around until the socket is dropped.
type Callbacks = (
Closure<dyn FnMut()>,
JsValue,
Closure<dyn FnMut(web_sys::MessageEvent)>,
Closure<dyn FnMut(web_sys::Event)>,
Closure<dyn FnMut(web_sys::CloseEvent)>,
JsValue,
JsValue,
);

impl WasmSocket {
Expand All @@ -89,7 +89,7 @@ impl WasmSocket {
waker: None,
}));

let open_callback = Closure::<dyn FnMut()>::new({
let open_callback = Closure::once_into_js({
let inner = inner.clone();
move || {
let mut inner = inner.lock().expect("Mutex is poised; qed");
Expand Down Expand Up @@ -120,9 +120,9 @@ impl WasmSocket {
});
socket.set_onmessage(Some(message_callback.as_ref().unchecked_ref()));

let error_callback = Closure::<dyn FnMut(_)>::new({
let error_callback = Closure::once_into_js({
let inner = inner.clone();
move |_| {
move |_event: web_sys::Event| {
// Callback does not provide useful information, signal it back to the stream.
let mut inner = inner.lock().expect("Mutex is poised; qed");
inner.state = ConnectionState::Error;
Expand All @@ -134,9 +134,9 @@ impl WasmSocket {
});
socket.set_onerror(Some(error_callback.as_ref().unchecked_ref()));

let close_callback = Closure::<dyn FnMut(_)>::new({
let close_callback = Closure::once_into_js({
let inner = inner.clone();
move |_| {
move |_event: web_sys::CloseEvent| {
let mut inner = inner.lock().expect("Mutex is poised; qed");
inner.state = ConnectionState::Closed;

Expand Down

0 comments on commit cfac283

Please sign in to comment.