observable_react/
react.rs

1// use std::convert::TryInto;
2
3// use js_sys::Function;
4// use serde::{de::DeserializeOwned, Serialize};
5use wasm_bindgen::prelude::*;
6
7#[wasm_bindgen(module = "react")]
8extern "C" {
9    /// Binding to React.useState
10    #[wasm_bindgen(js_name = useState)]
11    fn js_use_state(initial_value: JsValue) -> js_sys::Array;
12
13    /// Binding to React.useEffect
14    #[wasm_bindgen(js_name = useEffect)]
15    fn js_use_effect(effect: &Closure<dyn FnMut()>, bindings: js_sys::Array) -> js_sys::Array;
16
17    /// Binding to React.useReducer
18    #[wasm_bindgen(js_name = useReducer)]
19    fn js_use_reducer(reducer: &Closure<dyn FnMut()>, initial_value: JsValue) -> js_sys::Array;
20}
21
22// Duck type for React components
23#[wasm_bindgen]
24extern "C" {
25    pub type ReactComponent;
26
27    #[wasm_bindgen(structural, method)]
28    pub fn forceUpdate(this: &ReactComponent);
29}
30
31// /// Oxidized interface to React.useState
32// pub fn use_state<T>(initial_value: T) -> (T, impl Fn(T))
33// where
34//     T: Into<JsValue> + DeserializeOwned,
35// {
36//     #[allow(unused_unsafe)]
37//     let jsa = unsafe { js_use_state(initial_value.into()) };
38
39//     let current = jsa.get(0).into_serde().unwrap();
40//     let update: Function = jsa.get(1).try_into().unwrap();
41
42//     let cb = move |value: T| {
43//         // unimplemented!()
44//         update.call1(&JsValue::UNDEFINED, &value.into()).unwrap();
45//     };
46
47//     (current, cb)
48// }