consecuit_html/
callback.rs1use std::{ops::Deref, rc::Rc};
2
3use consecuit::prelude::batch_updates;
4use js_sys::Function;
5use wasm_bindgen::{convert::FromWasmAbi, prelude::Closure, JsCast};
6
7#[derive(Clone)]
24pub struct Callback<E: FromWasmAbi + 'static>(Rc<Closure<dyn Fn(E)>>);
25
26impl<E: FromWasmAbi + 'static> PartialEq for Callback<E> {
27 fn eq(&self, other: &Self) -> bool {
28 Rc::ptr_eq(&self.0, &other.0)
29 }
30}
31
32impl<E: FromWasmAbi + 'static> Callback<E> {
33 pub fn new<F: Fn(E) + 'static>(f: F) -> Self {
34 Self(Rc::new(Closure::wrap(Box::new(move |e: E| {
35 batch_updates(|| {
36 f(e);
37 })
38 }))))
39 }
40 pub fn as_websys_function(&self) -> &Function {
41 self.0.deref().as_ref().unchecked_ref()
42 }
43}