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
use crate::{
    component::{Checklist, Command as CommandTrait, Comp, Component},
    CallbackArg, Command,
};
use std::marker::PhantomData;
use wasm_bindgen::UnwrapThrowExt;

pub struct Future<F> {
    future: F,
}

impl<F, A> Future<F>
where
    A: 'static,
    F: 'static + std::future::Future<Output = A>,
{
    pub fn new(future: F) -> Self {
        Self { future }
    }

    pub fn with_fn<C, Cl, Cb>(self, callback: Cb) -> Command<C>
    where
        C: Component,
        Cl: 'static + Into<Checklist<C>>,
        Cb: 'static + FnOnce(&mut C, A) -> Cl,
    {
        FcFn {
            future: self.future,
            callback,
            phantom: PhantomData,
        }
        .into()
    }

    pub fn with_callback<C>(self, callback: CallbackArg<A>) -> Command<C>
    where
        C: Component,
    {
        FcCb {
            future: self.future,
            callback,
            phantom: PhantomData,
        }
        .into()
    }
}

struct FutureCallbackFn<F, A, C, Cl, Cb>(Option<FcFn<F, A, C, Cl, Cb>>);

struct FcFn<F, A, C, Cl, Cb> {
    future: F,
    callback: Cb,
    phantom: PhantomData<fn(C, A) -> Cl>,
}

impl<F, A, C, Cl, Cb> CommandTrait<C> for FutureCallbackFn<F, A, C, Cl, Cb>
where
    A: 'static,
    F: 'static + std::future::Future<Output = A>,
    C: Component,
    Cl: 'static + Into<Checklist<C>>,
    Cb: 'static + FnOnce(&mut C, A) -> Cl,
{
    fn execute(&mut self, comp: &Comp<C>, _state: &mut C) {
        let FcFn {
            phantom: _,
            future,
            callback,
        } = self
            .0
            .take()
            .expect_throw("Internal error: Why FutureCallback is executed twice?");

        let callback = comp.callback_once_arg_mut(callback);
        let f = async move {
            let rs = future.await;
            callback.call(rs); // .queue(rs) does not work in future, there is no way to execute the update queue now.
        };
        wasm_bindgen_futures::spawn_local(f);
    }
}

impl<F, A, C, Cl, Cb> From<FcFn<F, A, C, Cl, Cb>> for Command<C>
where
    A: 'static,
    F: 'static + std::future::Future<Output = A>,
    C: Component,
    Cl: 'static + Into<Checklist<C>>,
    Cb: 'static + FnOnce(&mut C, A) -> Cl,
{
    fn from(fca: FcFn<F, A, C, Cl, Cb>) -> Self {
        Command(Box::new(FutureCallbackFn(Some(fca))))
    }
}

struct FutureCallback<F, A, C>(Option<FcCb<F, A, C>>);

struct FcCb<F, A, C> {
    future: F,
    callback: CallbackArg<A>,
    phantom: PhantomData<C>,
}

impl<F, A, C> CommandTrait<C> for FutureCallback<F, A, C>
where
    A: 'static,
    F: 'static + std::future::Future<Output = A>,
    C: Component,
{
    fn execute(&mut self, _comp: &Comp<C>, _state: &mut C) {
        let FcCb {
            phantom: _,
            future,
            callback,
        } = self
            .0
            .take()
            .expect_throw("Internal error: Why FutureCallback is executed twice?");

        let f = async move {
            let rs = future.await;
            callback.call(rs); // .queue(rs) does not work in future, there is no way to execute the update queue now.
        };
        wasm_bindgen_futures::spawn_local(f);
    }
}

impl<F, A, C> From<FcCb<F, A, C>> for Command<C>
where
    A: 'static,
    F: 'static + std::future::Future<Output = A>,
    C: Component,
{
    fn from(fca: FcCb<F, A, C>) -> Self {
        Command(Box::new(FutureCallback(Some(fca))))
    }
}