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
use super::event::*;
use super::update::*;
use super::htmlcontrol::*;
use super::canvas_update::*;
use ui::*;
use ui::session::*;
use canvas::*;
use binding::*;
use futures::*;
use futures::stream;
use itertools::join;
use percent_encoding::*;
use std::sync::*;
pub struct HttpUserInterface<CoreUi> {
core_ui: Arc<CoreUi>,
ui_tree: BindRef<Control>,
base_path: String
}
impl<CoreUi: CoreUserInterface> HttpUserInterface<CoreUi> {
pub fn new(ui: Arc<CoreUi>, base_path: String) -> HttpUserInterface<CoreUi> {
let ui_tree = ui.ui_tree();
HttpUserInterface {
core_ui: ui,
ui_tree: ui_tree,
base_path: base_path
}
}
pub fn core(&self) -> Arc<CoreUi> {
Arc::clone(&self.core_ui)
}
fn http_event_to_core_event(http_event: Event) -> UiEvent {
use Event::*;
match http_event {
NewSession => UiEvent::Tick,
UiRefresh => UiEvent::Tick,
Tick => UiEvent::Tick,
Action(controller_path, action_name, action_parameter) => UiEvent::Action(controller_path, action_name, action_parameter)
}
}
fn new_ui_update(old_updates: Vec<Update>) -> Vec<Update> {
let mut new_updates = vec![];
let viewmodel_updates: Vec<_> = old_updates.iter()
.flat_map(|update| match update {
&Update::UpdateViewModel(ref view_model) => view_model.clone(),
_ => vec![]
}.into_iter())
.collect();
for update in old_updates {
match update {
Update::UpdateHtml(html_diff) => {
if html_diff.len() == 1 && html_diff[0].address.len() == 0 {
new_updates.push(Update::NewUserInterfaceHtml(html_diff[0].new_html.clone(), html_diff[0].ui_tree.clone(), viewmodel_updates.clone()));
} else {
new_updates.push(Update::UpdateHtml(html_diff));
}
},
Update::UpdateViewModel(_) => (),
update => new_updates.push(update)
}
}
new_updates
}
fn map_canvas_diff(canvas_diff: CanvasDiff) -> CanvasUpdate {
let mut encoded_updates = String::new();
canvas_diff.updates.encode_canvas(&mut encoded_updates);
let controller_path = join(canvas_diff.controller.iter()
.map(|component| utf8_percent_encode(&*component, DEFAULT_ENCODE_SET)),
"/");
let canvas_name = utf8_percent_encode(&canvas_diff.canvas_name, DEFAULT_ENCODE_SET).to_string();
CanvasUpdate::new(controller_path, canvas_name, encoded_updates)
}
fn map_core_ui_diff(ui_diff: UiDiff, ui_tree: &Control, base_path: &str) -> HtmlDiff {
let address = ui_diff.address;
let new_ui = ui_diff.new_ui;
let controller_path = html_controller_path_for_address(ui_tree, &address);
let html = new_ui.to_html_subcomponent(base_path, &controller_path);
HtmlDiff::new(address, &new_ui, html.to_string())
}
fn map_core_update(core_update: UiUpdate, base_path: &str, ui_tree: &Control) -> Vec<Update> {
use self::UiUpdate::*;
match core_update {
Start => vec![],
UpdateUi(core_diffs) => {
vec![Update::UpdateHtml(core_diffs.into_iter()
.map(|core_ui_diff| Self::map_core_ui_diff(core_ui_diff, &ui_tree, &base_path))
.collect()
)]
},
UpdateCanvas(canvas_diffs) => vec![Update::UpdateCanvas(canvas_diffs.into_iter().map(|diff| Self::map_canvas_diff(diff)).collect())],
UpdateViewModel(view_model_diffs) => vec![Update::UpdateViewModel(view_model_diffs)]
}
}
fn core_updates_to_http_updates(core_update: Vec<UiUpdate>, base_path: &str, ui_tree: &Control) -> Vec<Update> {
use self::UiUpdate::*;
let is_start = core_update.len() > 0 && core_update[0] == Start;
let base_update = core_update.into_iter()
.flat_map(|core_update| Self::map_core_update(core_update, base_path, ui_tree).into_iter())
.collect();
if is_start {
Self::new_ui_update(base_update)
} else {
base_update
}
}
}
pub type HttpEventSink = Box<Sink<SinkItem=Vec<Event>, SinkError=()>+Send>;
pub type HttpUpdateStream = Box<Stream<Item=Vec<Update>, Error=()>+Send>;
impl<CoreUi: CoreUserInterface> UserInterface<Vec<Event>, Vec<Update>, ()> for HttpUserInterface<CoreUi> {
type EventSink = HttpEventSink;
type UpdateStream = HttpUpdateStream;
fn get_input_sink(&self) -> Self::EventSink {
let core_sink = self.core_ui.get_input_sink();
let mapped_sink = core_sink.with_flat_map(|http_events: Vec<_>| {
let core_events = http_events.into_iter()
.map(|evt| Self::http_event_to_core_event(evt))
.collect();
stream::once(Ok(core_events))
});
Box::new(mapped_sink)
}
fn get_updates(&self) -> Self::UpdateStream {
let core_updates = self.core_ui.get_updates();
let ui_tree = BindRef::clone(&self.ui_tree);
let base_path = self.base_path.clone();
let mapped_updates = core_updates.map(move |core_updates| {
let ui_tree = ui_tree.get();
Self::core_updates_to_http_updates(core_updates, &base_path, &ui_tree)
});
Box::new(mapped_updates)
}
}
#[cfg(test)]
mod test {
use super::*;
use serde_json::*;
use futures::sync::oneshot;
use std::time::Duration;
use std::thread::*;
struct TestController {
ui: Binding<Control>,
}
impl Controller for TestController {
fn ui(&self) -> BindRef<Control> {
BindRef::new(&self.ui)
}
}
#[derive(Clone, PartialEq, Debug)]
enum TestItem {
Updates(Vec<Update>),
Timeout
}
fn timeout(ms: u64) -> oneshot::Receiver<()> {
let (timeout_send, timeout_recv) = oneshot::channel::<()>();
spawn(move || {
sleep(Duration::from_millis(ms));
timeout_send.send(()).ok();
});
timeout_recv
}
#[test]
fn generates_initial_update() {
let controller = TestController { ui: bind(Control::empty()) };
let core_session = Arc::new(UiSession::new(controller));
let http_session = HttpUserInterface::new(core_session, "test/session".to_string());
let http_stream = http_session.get_updates();
let next_or_timeout = http_stream.map(|updates| TestItem::Updates(updates)).select(timeout(2000).into_stream().map(|_| TestItem::Timeout).map_err(|_| ()));
let mut next_or_timeout = executor::spawn(next_or_timeout);
let first_update = next_or_timeout.wait_stream().unwrap();
assert!(first_update != Ok(TestItem::Timeout));
assert!(first_update == Ok(TestItem::Updates(vec![
Update::NewUserInterfaceHtml("<flo-empty></flo-empty>".to_string(), json![{ "attributes": Vec::<String>::new(), "control_type": "Empty" }], vec![])
])));
}
}