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
use crate::action::{Action, ActionSignal, Props, StatefulAction, DEFAULT};
use crate::comm::{QWriter, Signal};
use crate::resource::{ResourceAddr, ResourceMap};
use crate::server::{AsyncSignal, Config, State, SyncSignal, IO};
use eframe::egui;
use eyre::{eyre, Result};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

#[derive(Debug, Deserialize, Serialize)]
pub struct Seq(Vec<Box<dyn Action>>);

stateful!(Seq {
    children: VecDeque<Box<dyn StatefulAction>>,
});

impl Seq {
    pub fn new(children: Vec<Box<dyn Action>>) -> Self {
        Self(children)
    }
}

impl Action for Seq {
    #[inline]
    fn resources(&self, config: &Config) -> Vec<ResourceAddr> {
        self.0
            .iter()
            .flat_map(|c| c.resources(config))
            .unique()
            .collect()
    }

    fn stateful(
        &self,
        io: &IO,
        res: &ResourceMap,
        config: &Config,
        sync_writer: &QWriter<SyncSignal>,
        async_writer: &QWriter<AsyncSignal>,
    ) -> Result<Box<dyn StatefulAction>> {
        let children: VecDeque<_> = self
            .0
            .iter()
            .map(|a| {
                a.stateful(io, res, config, sync_writer, async_writer)
                    .unwrap()
            })
            .collect();

        for c in children.iter().take(children.len() - 1) {
            if c.props().infinite() {
                return Err(eyre!("Only the final action in a `Seq` can be infinite."));
            }
        }

        Ok(Box::new(StatefulSeq {
            done: false,
            children,
        }))
    }
}

impl StatefulSeq {
    pub fn push(&mut self, child: impl Into<Box<dyn StatefulAction>>) {
        self.children.push_back(child.into());
    }
}

impl StatefulAction for StatefulSeq {
    impl_stateful!();

    #[inline(always)]
    fn props(&self) -> Props {
        if let Some(c) = self.children.get(0) {
            c.props()
        } else {
            DEFAULT.into()
        }
    }

    #[inline]
    fn start(
        &mut self,
        sync_writer: &mut QWriter<SyncSignal>,
        async_writer: &mut QWriter<AsyncSignal>,
        state: &State,
    ) -> Result<Signal> {
        if let Some(c) = self.children.get_mut(0) {
            c.start(sync_writer, async_writer, state)
        } else {
            self.done = true;
            sync_writer.push(SyncSignal::UpdateGraph);
            Ok(Signal::none())
        }
    }

    #[inline]
    fn update(
        &mut self,
        signal: &ActionSignal,
        sync_writer: &mut QWriter<SyncSignal>,
        async_writer: &mut QWriter<AsyncSignal>,
        state: &State,
    ) -> Result<Signal> {
        if let Some(c) = self.children.get_mut(0) {
            let mut news = vec![];
            news.extend(c.update(signal, sync_writer, async_writer, state)?);

            if c.is_over()? {
                news.extend(self.children.pop_front().unwrap().stop(
                    sync_writer,
                    async_writer,
                    state,
                )?);

                if let Some(c) = self.children.get_mut(0) {
                    news.extend(c.start(sync_writer, async_writer, state)?);
                } else {
                    self.done = true;
                }
            }

            Ok(news.into())
        } else {
            Ok(Signal::none())
        }
    }

    fn show(
        &mut self,
        ui: &mut egui::Ui,
        sync_writer: &mut QWriter<SyncSignal>,
        async_writer: &mut QWriter<AsyncSignal>,
        state: &State,
    ) -> Result<()> {
        if let Some(c) = self.children.get_mut(0) {
            c.show(ui, sync_writer, async_writer, state)
        } else {
            Ok(())
        }
    }

    #[inline]
    fn stop(
        &mut self,
        sync_writer: &mut QWriter<SyncSignal>,
        async_writer: &mut QWriter<AsyncSignal>,
        state: &State,
    ) -> Result<Signal> {
        self.done = true;
        if let Some(c) = self.children.get_mut(0) {
            c.stop(sync_writer, async_writer, state)
        } else {
            Ok(Signal::none())
        }
    }
}