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
use dioxus_core::{ScopeState, TaskId};
use std::{cell::Cell, future::Future, rc::Rc};

pub fn use_future<'a, T: 'static, F: Future<Output = T> + 'static>(
    cx: &'a ScopeState,
    new_fut: impl FnOnce() -> F,
) -> &'a UseFuture<T> {
    let state = cx.use_hook(move |_| {
        //
        UseFuture {
            update: cx.schedule_update(),
            needs_regen: Cell::new(true),
            slot: Rc::new(Cell::new(None)),
            value: None,
            task: None,
        }
    });

    if let Some(value) = state.slot.take() {
        state.value = Some(value);
        state.task = None;
    }

    if state.needs_regen.get() {
        // We don't need regen anymore
        state.needs_regen.set(false);

        // Create the new future
        let fut = new_fut();

        // Clone in our cells
        let slot = state.slot.clone();
        let updater = state.update.clone();

        state.task = Some(cx.push_future(async move {
            let res = fut.await;
            slot.set(Some(res));
            updater();
        }));
    }

    state
}

pub struct UseFuture<T> {
    update: Rc<dyn Fn()>,
    needs_regen: Cell<bool>,
    value: Option<T>,
    slot: Rc<Cell<Option<T>>>,
    task: Option<TaskId>,
}

impl<T> UseFuture<T> {
    pub fn restart(&self) {
        self.needs_regen.set(true);
        (self.update)();
    }

    // clears the value in the future slot without starting the future over
    pub fn clear(&self) -> Option<T> {
        (self.update)();
        self.slot.replace(None)
    }

    // Manually set the value in the future slot without starting the future over
    pub fn set(&self, new_value: T) {
        self.slot.set(Some(new_value));
        self.needs_regen.set(true);
        (self.update)();
    }

    pub fn value(&self) -> Option<&T> {
        self.value.as_ref()
    }
}