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
use std::{
cell::{Ref, RefCell},
rc::Rc,
};
pub use wasm_bindgen::{JsCast, JsValue, UnwrapThrowExt};
use web_sys::Node;
pub use web_sys::{Element, Event, EventTarget};
use crate::{
prelude::{txrx, Component, IsDomNode, Receiver, Subscriber, Transmitter, ViewBuilder},
utils,
};
pub struct Gizmo<T: Component> {
pub trns: Transmitter<T::ModelMsg>,
pub recv: Receiver<T::ViewMsg>,
pub state: Rc<RefCell<T>>,
}
impl<T> Gizmo<T>
where
T: Component + 'static,
T::ViewMsg: Clone,
T::DomNode: JsCast + AsRef<Node> + Clone,
{
pub fn from_parts(
init: T,
tx_in: Transmitter<T::ModelMsg>,
rx_out: Receiver<T::ViewMsg>,
) -> Self {
let subscriber = Subscriber::new(&tx_in);
init.bind(&subscriber);
let state = Rc::new(RefCell::new(init));
let tx_out = rx_out.new_trns();
let rx_in = tx_in.spawn_recv();
let (tx_view, rx_view) = txrx();
rx_in.respond_shared(state.clone(), move |t: &mut T, msg: &T::ModelMsg| {
t.update(msg, &tx_view, &subscriber);
});
rx_view.respond(move |msg: &T::ViewMsg| {
let tx_out = tx_out.clone();
let msg = msg.clone();
utils::set_immediate(move || tx_out.send(&msg));
});
Gizmo {
trns: tx_in,
recv: rx_out,
state,
}
}
pub fn new(init: T) -> Gizmo<T> {
let tx_in = Transmitter::new();
let rx_out = Receiver::new();
Gizmo::from_parts(init, tx_in, rx_out)
}
pub fn view_builder(&self) -> ViewBuilder<T::DomNode> {
self.state.as_ref().borrow().view(&self.trns, &self.recv)
}
pub fn send(&self, msg: &T::ModelMsg) {
self.trns.send(msg);
}
pub fn with_state<F, N>(&self, f: F) -> N
where
F: Fn(&T) -> N,
{
let t = self.state.borrow();
f(&t)
}
pub fn set_state(&mut self, t: T) {
*self.state.borrow_mut() = t;
}
pub fn state_ref(&self) -> Ref<T> {
self.state.borrow()
}
}
impl<T: Component> From<T> for Gizmo<T> {
fn from(component: T) -> Gizmo<T> {
Gizmo::new(component)
}
}
pub type BuilderFn<T, D> = dyn Fn(&Transmitter<T>, &Receiver<T>) -> ViewBuilder<D>;
pub struct SimpleComponent<T, D: IsDomNode>(Box<BuilderFn<T, D>>);
impl<T, D: IsDomNode> SimpleComponent<T, D> {
pub fn new<F>(f: F) -> Self
where
F: Fn(&Transmitter<T>, &Receiver<T>) -> ViewBuilder<D> + 'static,
{
SimpleComponent(Box::new(f))
}
}
impl<T, D> Component for SimpleComponent<T, D>
where
T: Clone + 'static,
D: JsCast + AsRef<Node> + Clone + 'static,
{
type ModelMsg = T;
type ViewMsg = T;
type DomNode = D;
fn update(&mut self, msg: &T, tx_view: &Transmitter<T>, _sub: &Subscriber<T>) {
tx_view.send(msg);
}
fn view(&self, tx: &Transmitter<T>, rx: &Receiver<T>) -> ViewBuilder<D> {
self.0(tx, rx)
}
}