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
use crate::agents::live::wire::{DoAction, Subscription, WireEnvelope};
use crate::agents::live::{LiveAgent, LiveResponse};
//use crate::common;
use crate::widget::wired_bridge::AgentHook;
use crate::widget::{Context, OnWireEvent, Widget};
use anyhow::Error;
use rill_protocol::diff::diff;
use rill_protocol::flow::core::Flow;
use rill_protocol::io::client::{ClientReqId, ClientResponse};
use rill_protocol::io::provider::Path;
use std::collections::{HashMap, HashSet};
use yew::{Callback, Properties};

pub trait WiredWidget<M>: Widget<Tag = Option<Path>, Meta = M> {
    type Flow: Flow;

    fn state_changed(&mut self, _reloaded: bool, _ctx: &mut Context<Self>) {}

    fn state_update(
        &mut self,
        _tag: &Path,
        _event: &<Self::Flow as Flow>::Event,
        _reloaded: &mut bool,
        _ctx: &mut Context<Self>,
    ) {
    }
}

#[derive(Properties, Clone, PartialEq)]
pub struct SingleFlowProps {
    pub path: Path,
    #[prop_or_default]
    pub triggered: Option<Callback<()>>,
}

impl SingleFlowProps {
    pub fn notify(&self) {
        if let Some(callback) = self.triggered.as_ref() {
            callback.emit(());
        }
    }
}

pub struct SingleFlowMeta<T: WiredWidget<Self>> {
    state: Option<T::Flow>,
    wire: Option<Path>,
}

impl<T> Default for SingleFlowMeta<T>
where
    T: WiredWidget<Self>,
{
    fn default() -> Self {
        Self {
            state: None,
            wire: None,
        }
    }
}

impl<T> SingleFlowMeta<T>
where
    T: WiredWidget<Self>,
{
    pub fn state(&self) -> Option<&T::Flow> {
        self.state.as_ref()
    }

    /*
    pub fn active(&self) -> Option<&Path> {
        self.wire.as_ref()
    }
    */
}

impl<T> Context<T>
where
    T: WiredWidget<SingleFlowMeta<T>>,
{
    pub fn do_action(&mut self, action: <T::Flow as Flow>::Action) {
        if let Some(path) = self.meta().wire.clone() {
            let do_action = DoAction::<T::Flow>::new(path, action);
            self.live().wire(None, do_action);
            // TODO: Redraw is not needed here. Remove.
            //self.redraw();
        } else {
            log::error!("No path to do action: {:?}", action);
        }
    }

    /// SingleFlow mode also compatible with any other actions.
    pub fn do_action_of<F: Flow>(&mut self, path: Path, action: F::Action) {
        let do_action = DoAction::<F>::new(path, action);
        self.live().wire(None, do_action);
    }

    /// It unwires automatically.
    pub fn rewire(&mut self, path: Path) {
        // Don't rewire if path the same
        if self.meta().wire.as_ref() != Some(&path) {
            self.unwire();
            self.meta_mut().state.take();
            let wire_task = Subscription::new(path.clone());
            let new_wire = Some(path);
            self.meta_mut().wire = new_wire.clone();
            self.live().wire(new_wire, wire_task);
            self.redraw();
        }
    }

    pub fn unwire(&mut self) {
        if let Some(path) = self.meta_mut().wire.take() {
            self.live().unwire(&Some(path));
        }
    }
}

/*
pub fn loading_view() -> Html {
    html! {
        <common::Spinner />
    }
}
*/

pub struct SingleHook;

impl AgentHook for SingleHook {
    type Agent = LiveAgent;
}

impl<T> OnWireEvent<SingleHook> for T
where
    T: WiredWidget<SingleFlowMeta<Self>>,
{
    fn on_wire(
        &mut self,
        tag: &Self::Tag,
        event: WireEnvelope<ClientReqId, LiveResponse>,
        ctx: &mut Context<Self>,
    ) -> Result<(), Error> {
        match (tag.as_ref(), event.data) {
            (Some(path), event) => {
                if let LiveResponse::Forwarded(response) = event {
                    let mut reloaded = false;
                    match response {
                        ClientResponse::State(data) => {
                            let res = T::Flow::unpack_state(&data);
                            match res {
                                Ok(state) => {
                                    ctx.meta_mut().state = Some(state);
                                    reloaded = true;
                                }
                                Err(err) => {
                                    log::error!("Can't unpack the state: {}", err);
                                }
                            }
                        }
                        ClientResponse::Delta(data) => {
                            let res = T::Flow::unpack_event(&data);
                            match res {
                                Ok(event) => {
                                    self.state_update(path, &event, &mut reloaded, ctx);
                                    if let Some(state) = ctx.meta_mut().state.as_mut() {
                                        state.apply(event);
                                    }
                                }
                                Err(err) => {
                                    log::error!("Can't unpack the delta: {}", err);
                                }
                            }
                        }
                        ClientResponse::Done => {
                            // TODO: What to do when the stream is finished completely?
                        }
                        other => {
                            log::error!("Unexpected message for the single flow: {:?}", other);
                        }
                    }
                    self.state_changed(reloaded, ctx);
                }
            }
            (None, _) => {
                // Redraw on action
                ctx.redraw();
            }
        }
        Ok(())
    }
}

#[derive(Properties, Clone, PartialEq)]
pub struct MultiFlowProps {
    pub paths: HashSet<Path>,
}

pub struct MultiFlowMeta<T: WiredWidget<Self>> {
    states: HashMap<Path, T::Flow>,
    wires: HashSet<Path>,
}

impl<T> Default for MultiFlowMeta<T>
where
    T: WiredWidget<Self>,
{
    fn default() -> Self {
        Self {
            states: HashMap::new(),
            wires: HashSet::new(),
        }
    }
}

impl<T> MultiFlowMeta<T>
where
    T: WiredWidget<Self>,
{
    pub fn states(&self) -> &HashMap<Path, T::Flow> {
        &self.states
    }
}

impl<T> Context<T>
where
    T: WiredWidget<MultiFlowMeta<T>>,
{
    pub fn rewire_many(&mut self, paths: &HashSet<Path>) {
        let (to_add, to_remove) = diff(&self.meta().wires, paths);
        for path in to_add {
            let wire_task = Subscription::new(path.clone());
            self.live().wire(Some(path.clone()), wire_task);
            self.meta_mut().wires.insert(path);
            // `State/Flow` will be received using `wire`
            self.redraw();
        }
        for path in to_remove {
            self.meta_mut().wires.remove(&path);
            self.meta_mut().states.remove(&path);
            self.live().unwire(&Some(path));
            self.redraw();
        }
    }
}

pub struct MultiHook;

impl AgentHook for MultiHook {
    type Agent = LiveAgent;
}

impl<T> OnWireEvent<MultiHook> for T
where
    T: WiredWidget<MultiFlowMeta<Self>>,
{
    fn on_wire(
        &mut self,
        tag: &Self::Tag,
        event: WireEnvelope<ClientReqId, LiveResponse>,
        ctx: &mut Context<Self>,
    ) -> Result<(), Error> {
        match (tag.as_ref(), event.data) {
            (Some(path), event) => {
                if let LiveResponse::Forwarded(response) = event {
                    let mut reloaded = false;
                    match response {
                        ClientResponse::State(data) => {
                            let state = T::Flow::unpack_state(&data).unwrap();
                            ctx.meta_mut().states.insert(path.clone(), state);
                            reloaded = true;
                        }
                        ClientResponse::Delta(data) => {
                            // TODO: Don't `unwrap` here
                            let event = T::Flow::unpack_event(&data).unwrap();
                            self.state_update(path, &event, &mut reloaded, ctx);
                            if let Some(state) = ctx.meta_mut().states.get_mut(path) {
                                state.apply(event);
                            }
                        }
                        ClientResponse::Done => {
                            // TODO: What to do when the stream is finished completely?
                        }
                        other => {
                            log::error!("Unexpected message for the multi flow: {:?}", other);
                        }
                    }
                    self.state_changed(reloaded, ctx);
                }
            }
            (None, _) => {
                // Redraw on action
                ctx.redraw();
            }
        }
        Ok(())
    }
}