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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use super::{use_ref, RefContainer};
use crate::{
  callback::{Callable, Void},
  react_bindings, Persisted, PersistedOrigin,
};
use js_sys::Function;
use std::cell::Ref;
use wasm_bindgen::UnwrapThrowExt;

/// Allows access to the underlying state data persisted with [`use_state()`].
#[derive(Debug)]
pub struct State<T> {
  ref_container: RefContainer<Option<T>>,
  update: Function,
}

impl<T: 'static> State<T> {
  /// Returns a reference to the value of the state.
  pub fn value(&self) -> Ref<'_, T> {
    Ref::map(self.ref_container.current(), |x| {
      x.as_ref().expect_throw("no state value available")
    })
  }

  /// Sets the state to the return value of the given mutator closure and
  /// rerenders the component.
  ///
  /// # Panics
  ///
  /// Panics if the value is currently borrowed.
  pub fn set(&mut self, mutator: impl FnOnce(T) -> T) {
    let value = self.ref_container.current_mut().take();
    let new_value = value.map(|value| mutator(value));

    self.ref_container.set_current(new_value);
    self
      .update
      .call(&Void.into())
      .expect_throw("unable to call state update");
  }
}

impl<T: 'static> Persisted for State<T> {
  fn ptr(&self) -> PersistedOrigin {
    self.ref_container.ptr()
  }
}

impl<T> Clone for State<T> {
  fn clone(&self) -> Self {
    Self {
      ref_container: self.ref_container.clone(),
      update: self.update.clone(),
    }
  }
}

/// Persist stateful data of the component.
///
/// Unlike the [`use_ref()`] hook, updating the state will automatically trigger
/// a rerender of the component.
///
/// Unlike its React counterpart, setting the state will mutate the underlying
/// data immediately.
///
/// # Example
///
/// ```
/// # use wasm_react::{*, hooks::*};
/// #
/// # struct State { greet: &'static str }
/// # struct C;
/// # impl C {
/// fn render(&self) -> VNode {
///   let state = use_state(|| State { greet: "Hello!" });
///
///   use_effect({
///     let mut state = state.clone();
///
///     move || {
///       state.set(|mut state| {
///         state.greet = "Welcome!";
///         state
///       });
///
///       || ()
///     }
///   }, Deps::some(( /* ... */ )));
///
///   h!(div).build(c![state.value().greet])
/// }
/// # }
/// ```
pub fn use_state<T: 'static>(init: impl FnOnce() -> T) -> State<T> {
  let mut ref_container = use_ref(None);

  if ref_container.current().is_none() {
    ref_container.set_current(Some(init()));
  }

  let update = react_bindings::use_rust_state();

  State {
    ref_container,
    update,
  }
}