1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// use std::convert::TryInto;

// use js_sys::Function;
// use serde::{de::DeserializeOwned, Serialize};
use wasm_bindgen::prelude::*;

#[wasm_bindgen(module = "react")]
extern "C" {
    /// Binding to React.useState
    #[wasm_bindgen(js_name = useState)]
    fn js_use_state(initial_value: JsValue) -> js_sys::Array;

    /// Binding to React.useEffect
    #[wasm_bindgen(js_name = useEffect)]
    fn js_use_effect(effect: &Closure<dyn FnMut()>, bindings: js_sys::Array) -> js_sys::Array;

    /// Binding to React.useReducer
    #[wasm_bindgen(js_name = useReducer)]
    fn js_use_reducer(reducer: &Closure<dyn FnMut()>, initial_value: JsValue) -> js_sys::Array;
}

// Duck type for React components
#[wasm_bindgen]
extern "C" {
    pub type ReactComponent;

    #[wasm_bindgen(structural, method)]
    pub fn forceUpdate(this: &ReactComponent);
}

// /// Oxidized interface to React.useState
// pub fn use_state<T>(initial_value: T) -> (T, impl Fn(T))
// where
//     T: Into<JsValue> + DeserializeOwned,
// {
//     #[allow(unused_unsafe)]
//     let jsa = unsafe { js_use_state(initial_value.into()) };

//     let current = jsa.get(0).into_serde().unwrap();
//     let update: Function = jsa.get(1).try_into().unwrap();

//     let cb = move |value: T| {
//         // unimplemented!()
//         update.call1(&JsValue::UNDEFINED, &value.into()).unwrap();
//     };

//     (current, cb)
// }