#Closure that lives for the life of the app

1 messages · Page 1 of 1 (latest)

tame quest
#

Im trying to pass a wasm-bindgen closure to a JS class that will call it when there are updates. The Closure needs to live for the duration of the program so it is not invalidated at the end of the component execution. I though I would use use_state to hold the Closure but I keep getting errors:

error[E0507]: cannot move out of dereference of `UseStateHandle<wasm_bindgen::prelude::Closure<dyn FnMut(wasm_bindgen::JsValue)>>`
  --> src/main.rs:32:21
   |
32 |             let f = *f;
   |                     ^^ move occurs because value has type `wasm_bindgen::prelude::Closure<dyn FnMut(wasm_bindgen::JsValue)>`, which does not implement the `Copy` trait

I tried wrapping it in Cell and Arc but get similar errors. What can I do to get the Closure to outlive the component?

spare thunder
#

what about storing in a Box?

#

Another idea, the error mentions copy, have you tried cloning instead?

tame quest
#

Thanks for the suggestion. I ended up finding a solution that used Box::leak to get a static reference, and then passed the reference to the yew app as a prop instead of passing it in by value.

frank whale
#

you tried UseStateHandle<Rc<RefCell<closure>>> you say?

#

why does it want to move it out anyway