logic.rs
3.35 KB
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
use crate::api::markdown::Markdown;
use mogwai::prelude::*;
use super::state::MarkdownState;
#[derive(Clone)]
pub(super) enum MarkdownLogic {
Choose(Option<i32>),
Discard,
Select,
Store,
Toggle,
Update,
}
pub(super) async fn markdown_logic( mut rx_logic: broadcast::Receiver<MarkdownLogic>
, tx_logic: broadcast::Sender<MarkdownLogic>
, tx_toggle: broadcast::Sender<bool>
, tx_patches: mpmc::Sender<ListPatch<ViewBuilder<Dom>>>
, mut rx_dom: broadcast::Receiver<Dom> )
{
let dom = rx_dom.next().await.unwrap();
let mut md = Markdown::new("md-example").await.unwrap();
let mut state = MarkdownState::new(dom).unwrap();
while let Some(msg) = rx_logic.next().await {
match msg {
MarkdownLogic::Store => {
let new_md = state.get_md();
if md.json.content != new_md {
md.json.content = new_md;
md.save().await.unwrap();
}
},
MarkdownLogic::Update => state.update(),
MarkdownLogic::Toggle => {
state.toggle_show_edit();
tx_toggle.broadcast(state.show_edit).await.unwrap();
},
MarkdownLogic::Discard => {
state.set_md(Some(md.json.content.as_str()));
state.update();
},
MarkdownLogic::Select => {
state.toggle_show_patches();
match state.show_patches {
true => {
let patches = md
. patches().await.unwrap()
. into_iter()
. map(|diff| {
let id = Some(diff.id);
let choose_filter = tx_logic
. sink()
. contra_map(move |_| MarkdownLogic::Choose(id));
builder! {
<li><button on:click=choose_filter
value=format!("{}", diff.id.to_owned())>
{diff.date_created.to_owned()}
</button></li>
}});
let all = vec![builder! {
<li><button on:click=tx_logic.sink().contra_map(|_| MarkdownLogic::Choose(None))>
"Current"
</button></li>
}].into_iter().chain(patches);
let list_replace = ListPatch::splice(.., all);
tx_patches.send(list_replace).await.unwrap();
},
false => tx_patches.send(ListPatch::drain()).await.unwrap(),
}
},
MarkdownLogic::Choose(id) => {
md.read(id).await.unwrap();
state.set_md(Some(md.json.content.as_str()));
state.update();
tx_patches.send(ListPatch::drain()).await.unwrap();
state.reset_show_patches();
},
}
}
}