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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#![warn(clippy::pedantic)]

use dioxus_core::prelude::*;
use std::{
    cell::{RefCell, RefMut},
    fmt::{Debug, Display},
    ops::{Add, Div, Mul, Not, Sub},
    rc::Rc,
    sync::Arc,
};

/// Store state between component renders.
///
/// ## Dioxus equivalent of useState, designed for Rust
///
/// The Dioxus version of `useState` for state management inside components. It allows you to ergonomically store and
/// modify state between component renders. When the state is updated, the component will re-render.
///
///
/// ```ignore
/// const Example: Component = |cx| {
///     let count = use_state(cx, || 0);
///
///     cx.render(rsx! {
///         div {
///             h1 { "Count: {count}" }
///             button { onclick: move |_| *count.modify() += 1, "Increment" }
///             button { onclick: move |_| *count.modify() -= 1, "Decrement" }
///         }
///     ))
/// }
/// ```
#[must_use]
pub fn use_state<T: 'static>(
    cx: &ScopeState,
    initial_state_fn: impl FnOnce() -> T,
) -> &UseState<T> {
    let hook = cx.use_hook(move || {
        let current_val = Rc::new(initial_state_fn());
        let update_callback = cx.schedule_update();
        let slot = Rc::new(RefCell::new(current_val.clone()));
        let setter = Rc::new({
            to_owned![update_callback, slot];
            move |new| {
                {
                    let mut slot = slot.borrow_mut();

                    // if there's only one reference (weak or otherwise), we can just swap the values
                    // Typically happens when the state is set multiple times - we don't want to create a new Rc for each new value
                    if let Some(val) = Rc::get_mut(&mut slot) {
                        *val = new;
                    } else {
                        *slot = Rc::new(new);
                    }
                }
                update_callback();
            }
        });

        UseState {
            current_val,
            update_callback,
            setter,
            slot,
        }
    });

    hook.current_val = hook.slot.borrow().clone();

    hook
}

pub struct UseState<T: 'static> {
    pub(crate) current_val: Rc<T>,
    pub(crate) update_callback: Arc<dyn Fn()>,
    pub(crate) setter: Rc<dyn Fn(T)>,
    pub(crate) slot: Rc<RefCell<Rc<T>>>,
}

impl<T: 'static> UseState<T> {
    /// Set the state to a new value.
    pub fn set(&self, new: T) {
        (self.setter)(new);
    }

    /// Get the current value of the state by cloning its container Rc.
    ///
    /// This is useful when you are dealing with state in async contexts but need
    /// to know the current value. You are not given a reference to the state.
    ///
    /// # Examples
    /// An async context might need to know the current value:
    ///
    /// ```rust, ignore
    /// fn component(cx: Scope) -> Element {
    ///     let count = use_state(cx, || 0);
    ///     cx.spawn({
    ///         let set_count = count.to_owned();
    ///         async move {
    ///             let current = set_count.current();
    ///         }
    ///     })
    /// }
    /// ```
    #[must_use]
    pub fn current(&self) -> Rc<T> {
        self.slot.borrow().clone()
    }

    /// Get the `setter` function directly without the `UseState` wrapper.
    ///
    /// This is useful for passing the setter function to other components.
    ///
    /// However, for most cases, calling `to_owned` on the state is the
    /// preferred way to get "another" state handle.
    ///
    ///
    /// # Examples
    /// A component might require an `Rc<dyn Fn(T)>` as an input to set a value.
    ///
    /// ```rust, ignore
    /// fn component(cx: Scope) -> Element {
    ///     let value = use_state(cx, || 0);
    ///
    ///     rsx!{
    ///         Component {
    ///             handler: value.setter()
    ///         }
    ///     }
    /// }
    /// ```
    #[must_use]
    pub fn setter(&self) -> Rc<dyn Fn(T)> {
        self.setter.clone()
    }

    /// Set the state to a new value, using the current state value as a reference.
    ///
    /// This is similar to passing a closure to React's `set_value` function.
    ///
    /// # Examples
    ///
    /// Basic usage:
    /// ```rust, ignore
    /// # use dioxus_core::prelude::*;
    /// # use dioxus_hooks::*;
    /// fn component(cx: Scope) -> Element {
    ///     let value = use_state(cx, || 0);
    ///
    ///     // to increment the value
    ///     value.modify(|v| v + 1);
    ///
    ///     // usage in async
    ///     cx.spawn({
    ///         let value = value.to_owned();
    ///         async move {
    ///             value.modify(|v| v + 1);
    ///         }
    ///     });
    ///
    ///     # todo!()
    /// }
    /// ```
    pub fn modify(&self, f: impl FnOnce(&T) -> T) {
        let new_val = {
            let current = self.slot.borrow();
            f(current.as_ref())
        };
        (self.setter)(new_val);
    }

    /// Get the value of the state when this handle was created.
    ///
    /// This method is useful when you want an `Rc` around the data to cheaply
    /// pass it around your app.
    ///
    /// ## Warning
    ///
    /// This will return a stale value if used within async contexts.
    ///
    /// Try `current` to get the real current value of the state.
    ///
    /// ## Example
    ///
    /// ```rust, ignore
    /// # use dioxus_core::prelude::*;
    /// # use dioxus_hooks::*;
    /// fn component(cx: Scope) -> Element {
    ///     let value = use_state(cx, || 0);
    ///
    ///     let as_rc = value.get();
    ///     assert_eq!(as_rc.as_ref(), &0);
    ///
    ///     # todo!()
    /// }
    /// ```
    #[must_use]
    pub fn get(&self) -> &T {
        &self.current_val
    }

    #[must_use]
    pub fn get_rc(&self) -> &Rc<T> {
        &self.current_val
    }

    /// Mark the component that create this [`UseState`] as dirty, forcing it to re-render.
    ///
    /// ```rust, ignore
    /// fn component(cx: Scope) -> Element {
    ///     let count = use_state(cx, || 0);
    ///     cx.spawn({
    ///         let count = count.to_owned();
    ///         async move {
    ///             // for the component to re-render
    ///             count.needs_update();
    ///         }
    ///     })
    /// }
    /// ```
    pub fn needs_update(&self) {
        (self.update_callback)();
    }
}

impl<T: Clone> UseState<T> {
    /// Get a mutable handle to the value by calling `ToOwned::to_owned` on the
    /// current value.
    ///
    /// This is essentially cloning the underlying value and then setting it,
    /// giving you a mutable handle in the process. This method is intended for
    /// types that are cheaply cloneable.
    ///
    /// If you are comfortable dealing with `RefMut`, then you can use `make_mut` to get
    /// the underlying slot. However, be careful with `RefMut` since you might panic
    /// if the `RefCell` is left open.
    ///
    /// # Examples
    ///
    /// ```rust, ignore
    /// let val = use_state(cx, || 0);
    ///
    /// val.with_mut(|v| *v = 1);
    /// ```
    pub fn with_mut(&self, apply: impl FnOnce(&mut T)) {
        let mut slot = self.slot.borrow_mut();
        let mut inner = slot.as_ref().to_owned();

        apply(&mut inner);

        if let Some(new) = Rc::get_mut(&mut slot) {
            *new = inner;
        } else {
            *slot = Rc::new(inner);
        }

        self.needs_update();
    }

    /// Get a mutable handle to the value by calling `ToOwned::to_owned` on the
    /// current value.
    ///
    /// This is essentially cloning the underlying value and then setting it,
    /// giving you a mutable handle in the process. This method is intended for
    /// types that are cheaply cloneable.
    ///
    /// # Warning
    /// Be careful with `RefMut` since you might panic if the `RefCell` is left open!
    ///
    /// # Examples
    ///
    /// ```rust, ignore
    /// let val = use_state(cx, || 0);
    ///
    /// *val.make_mut() += 1;
    /// ```
    #[must_use]
    #[allow(clippy::missing_panics_doc)]
    pub fn make_mut(&self) -> RefMut<T> {
        let mut slot = self.slot.borrow_mut();

        self.needs_update();

        if Rc::strong_count(&*slot) > 0 {
            *slot = Rc::new(slot.as_ref().to_owned());
        }

        RefMut::map(slot, |rc| Rc::get_mut(rc).expect("the hard count to be 0"))
    }

    /// Convert this handle to a tuple of the value and the handle itself.
    #[must_use]
    pub fn split(&self) -> (&T, &Self) {
        (&self.current_val, self)
    }
}

impl<T: 'static> Clone for UseState<T> {
    fn clone(&self) -> Self {
        UseState {
            current_val: self.current_val.clone(),
            update_callback: self.update_callback.clone(),
            setter: self.setter.clone(),
            slot: self.slot.clone(),
        }
    }
}

impl<T: 'static + Display> std::fmt::Display for UseState<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.current_val)
    }
}

impl<T: std::fmt::Binary> std::fmt::Binary for UseState<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:b}", self.current_val.as_ref())
    }
}

impl<T: PartialEq> PartialEq<T> for UseState<T> {
    fn eq(&self, other: &T) -> bool {
        self.current_val.as_ref() == other
    }
}

// todo: this but for more interesting conrete types
impl PartialEq<bool> for &UseState<bool> {
    fn eq(&self, other: &bool) -> bool {
        self.current_val.as_ref() == other
    }
}

impl<T> PartialEq<UseState<T>> for UseState<T> {
    fn eq(&self, other: &UseState<T>) -> bool {
        Rc::ptr_eq(&self.current_val, &other.current_val)
    }
}

impl<T: std::cmp::PartialOrd> PartialOrd<T> for UseState<T> {
    fn ge(&self, other: &T) -> bool {
        *self.current_val >= *other
    }

    fn gt(&self, other: &T) -> bool {
        *self.current_val > *other
    }

    fn le(&self, other: &T) -> bool {
        *self.current_val <= *other
    }

    fn lt(&self, other: &T) -> bool {
        *self.current_val < *other
    }

    fn partial_cmp(&self, other: &T) -> Option<std::cmp::Ordering> {
        (*self.current_val).partial_cmp(other)
    }
}

impl<T: std::cmp::PartialOrd> PartialOrd<UseState<T>> for UseState<T> {
    fn ge(&self, other: &UseState<T>) -> bool {
        self.current_val >= other.current_val
    }

    fn gt(&self, other: &UseState<T>) -> bool {
        self.current_val > other.current_val
    }

    fn le(&self, other: &UseState<T>) -> bool {
        self.current_val <= other.current_val
    }

    fn lt(&self, other: &UseState<T>) -> bool {
        self.current_val < other.current_val
    }

    fn partial_cmp(&self, other: &UseState<T>) -> Option<std::cmp::Ordering> {
        self.current_val.partial_cmp(&other.current_val)
    }
}

impl<T: Debug> Debug for UseState<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.current_val)
    }
}

impl<T> std::ops::Deref for UseState<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.current_val.as_ref()
    }
}

impl<T: Not + Copy> std::ops::Not for &UseState<T> {
    type Output = <T as std::ops::Not>::Output;

    fn not(self) -> Self::Output {
        self.current_val.not()
    }
}

impl<T: Not + Copy> std::ops::Not for UseState<T> {
    type Output = <T as std::ops::Not>::Output;

    fn not(self) -> Self::Output {
        self.current_val.not()
    }
}

impl<T: std::ops::Add + Copy> std::ops::Add<T> for &UseState<T> {
    type Output = <T as std::ops::Add>::Output;

    fn add(self, other: T) -> Self::Output {
        *self.current_val.as_ref() + other
    }
}
impl<T: std::ops::Sub + Copy> std::ops::Sub<T> for &UseState<T> {
    type Output = <T as std::ops::Sub>::Output;

    fn sub(self, other: T) -> Self::Output {
        *self.current_val.as_ref() - other
    }
}

impl<T: std::ops::Div + Copy> std::ops::Div<T> for &UseState<T> {
    type Output = <T as std::ops::Div>::Output;

    fn div(self, other: T) -> Self::Output {
        *self.current_val.as_ref() / other
    }
}

impl<T: std::ops::Mul + Copy> std::ops::Mul<T> for &UseState<T> {
    type Output = <T as std::ops::Mul>::Output;

    fn mul(self, other: T) -> Self::Output {
        *self.current_val.as_ref() * other
    }
}

impl<T: Add<Output = T> + Copy> std::ops::AddAssign<T> for &UseState<T> {
    fn add_assign(&mut self, rhs: T) {
        self.set((*self.current()) + rhs);
    }
}

impl<T: Sub<Output = T> + Copy> std::ops::SubAssign<T> for &UseState<T> {
    fn sub_assign(&mut self, rhs: T) {
        self.set((*self.current()) - rhs);
    }
}

impl<T: Mul<Output = T> + Copy> std::ops::MulAssign<T> for &UseState<T> {
    fn mul_assign(&mut self, rhs: T) {
        self.set((*self.current()) * rhs);
    }
}

impl<T: Div<Output = T> + Copy> std::ops::DivAssign<T> for &UseState<T> {
    fn div_assign(&mut self, rhs: T) {
        self.set((*self.current()) / rhs);
    }
}

impl<T: Add<Output = T> + Copy> std::ops::AddAssign<T> for UseState<T> {
    fn add_assign(&mut self, rhs: T) {
        self.set((*self.current()) + rhs);
    }
}

impl<T: Sub<Output = T> + Copy> std::ops::SubAssign<T> for UseState<T> {
    fn sub_assign(&mut self, rhs: T) {
        self.set((*self.current()) - rhs);
    }
}

impl<T: Mul<Output = T> + Copy> std::ops::MulAssign<T> for UseState<T> {
    fn mul_assign(&mut self, rhs: T) {
        self.set((*self.current()) * rhs);
    }
}

impl<T: Div<Output = T> + Copy> std::ops::DivAssign<T> for UseState<T> {
    fn div_assign(&mut self, rhs: T) {
        self.set((*self.current()) / rhs);
    }
}

#[test]
fn api_makes_sense() {
    #[allow(unused)]
    fn app(cx: Scope) -> Element {
        let val = use_state(cx, || 0);

        val.set(0);
        val.modify(|v| v + 1);
        let real_current = val.current();

        match val.get() {
            10 => {
                val.set(20);
                val.modify(|v| v + 1);
            }
            20 => {}
            _ => {
                println!("{real_current}");
            }
        }

        cx.spawn({
            to_owned![val];
            async move {
                val.modify(|f| f + 1);
            }
        });

        // cx.render(LazyNodes::new(|f| f.static_text("asd")))

        todo!()
    }
}