How to correctly implement a Closure from once_into_js to be called multiple times? #3780
-
SummaryI am new in the field of wasm-bindgen and am stuck at an issue which I can't seem to implement correctly. Right now, I have a Rust CallBack function, which should be called from an external JS library. In order to pass the callback to JS, I used the following call which returns a JsValue:
As I understand the documentation, this will basically destroy the Rust Closure object and pass the closure into JS, were it will be automatically destroyed after being called/executed. Works like a charm. Since I don't want to render my component after each click, I am trying to implement this callback for the components lifecycle. So I tried to use Closure::new() but to no avail. I can only get it to compile inside a #[wasm_bindgen] block, but there I can't use the Event nor the CallBack struct.
The idea is to keep the instance of Closures around during the lifecycle of the component and implement a Destroy function to free the Rust Closure. Passing it into JS was then hopefully working like:
However, Rust complains with the following error:
I read through many articles and how-tos but they either use closures with no arguments or native arguments like u32. I need to pass the Event struct back into the Rust callback somehow. Any help would be highly appreciated. Additional Detailsserde = { version = "1.0.195", features = ["derive"] } |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Are you trying to instantiate What issue exactly do you have when trying to use |
Beta Was this translation helpful? Give feedback.
Are you trying to instantiate
Closures
from JS?Because if not you should not use
#[wasm_bindgen]
on theimpl
block.What issue exactly do you have when trying to use
Closure::new()
without#[wasm_bindgen]
? As far as I could see from your code you just have to use the same parameters as you defined inClosures::onClick_c
. E.g.Closure::new(|| ...)
instead ofClosure::new(|_| ...)
.