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
use super::{FactoryComponent, FactoryHandle};
use crate::factory::{DataGuard, DynamicIndex, FactorySender, FactoryView};
use crate::shutdown::ShutdownSender;
use crate::{shutdown, GuardedReceiver, Receiver, Sender};
use std::any;
use tracing::info_span;
#[derive(Debug)]
pub(super) struct FactoryBuilder<C: FactoryComponent> {
pub(super) data: Box<C>,
pub(super) root_widget: C::Root,
pub(super) component_sender: FactorySender<C>,
pub(super) input_receiver: Receiver<C::Input>,
pub(super) output_receiver: Receiver<C::Output>,
pub(super) cmd_receiver: Receiver<C::CommandOutput>,
pub(super) shutdown_notifier: ShutdownSender,
}
impl<C: FactoryComponent> FactoryBuilder<C> {
pub(super) fn new(index: &DynamicIndex, init: C::Init) -> Self {
let (input_sender, input_receiver) = crate::channel::<C::Input>();
let (output_sender, output_receiver) = crate::channel::<C::Output>();
let (cmd_sender, cmd_receiver) = crate::channel::<C::CommandOutput>();
let (shutdown_notifier, shutdown_receiver) = shutdown::channel();
let component_sender =
FactorySender::new(input_sender, output_sender, cmd_sender, shutdown_receiver);
let data = Box::new(C::init_model(init, index, component_sender.clone()));
let root_widget = data.init_root();
Self {
data,
root_widget,
component_sender,
input_receiver,
output_receiver,
cmd_receiver,
shutdown_notifier,
}
}
pub(super) fn launch<Transform>(
self,
index: &DynamicIndex,
returned_widget: <C::ParentWidget as FactoryView>::ReturnedWidget,
parent_sender: &Sender<C::ParentInput>,
transform: Transform,
) -> FactoryHandle<C>
where
Transform: Fn(C::Output) -> Option<C::ParentInput> + 'static,
{
let Self {
mut data,
root_widget,
component_sender,
input_receiver,
output_receiver,
cmd_receiver,
shutdown_notifier,
} = self;
let forward_sender = parent_sender.0.clone();
crate::spawn_local(async move {
while let Some(msg) = output_receiver.recv().await {
if let Some(new_msg) = transform(msg) {
if forward_sender.send(new_msg).is_err() {
break;
}
}
}
});
let (notifier, notifier_receiver) = crate::channel();
let widgets = Box::new(data.init_widgets(
index,
&root_widget,
&returned_widget,
component_sender.clone(),
));
let input_sender = component_sender.input_sender().clone();
let output_sender = component_sender.output_sender().clone();
let data = DataGuard::new(
data,
widgets,
shutdown_notifier,
output_sender,
|mut model, mut widgets| {
async move {
let mut notifier = GuardedReceiver::new(notifier_receiver);
let mut cmd = GuardedReceiver::new(cmd_receiver);
let mut input = GuardedReceiver::new(input_receiver);
loop {
futures::select!(
message = input => {
let span = info_span!(
"update_with_view",
input=?message,
component=any::type_name::<C>(),
id=model.id(),
);
let _enter = span.enter();
model.update_with_view(&mut widgets, message, component_sender.clone());
}
message = cmd => {
let span = info_span!(
"update_cmd_with_view",
cmd_output=?message,
component=any::type_name::<C>(),
id=model.id(),
);
let _enter = span.enter();
model.update_cmd_with_view(&mut widgets, message, component_sender.clone());
}
_ = notifier => {
model.update_view(&mut widgets, component_sender.clone());
}
);
}
}
},
C::shutdown,
);
FactoryHandle {
data,
root_widget,
returned_widget,
input: input_sender,
notifier,
}
}
}