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
290
291
292
293
294
295
use quote::quote;
#[proc_macro_attribute]
pub fn custom_element(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let custom_tag: proc_macro2::Literal =
syn::parse(attr).expect("must be a literal");
let impl_item: syn::ItemImpl = syn::parse(item)
.expect("Expecting custom_element macro to be used in impl trait");
let (_, path, _) = &impl_item
.trait_
.as_ref()
.expect("must have a trait implementation");
let component: &syn::PathSegment =
&path.segments.last().expect("must have a last segment");
let component_ident = component.ident.to_string();
match &*component_ident {
"Component" => impl_component(&impl_item, &custom_tag, component),
"Application" => impl_application(&impl_item, &custom_tag, component),
_ => panic!("unsupported trait implementation: {}", component_ident),
}
}
fn impl_component(
impl_item: &syn::ItemImpl,
custom_tag: &proc_macro2::Literal,
component: &syn::PathSegment,
) -> proc_macro::TokenStream {
let mut tokens = proc_macro2::TokenStream::new();
let component_msg = get_msg_generic_ident(&component);
let self_type = &impl_item.self_ty;
if let syn::Type::Path(type_path) = self_type.as_ref() {
let path_segment = &type_path.path.segments[0];
let component = &path_segment.ident;
let derive_component = proc_macro2::Ident::new(
&format!("_{}__CustomElement", component),
proc_macro2::Span::call_site(),
);
let derive_msg = proc_macro2::Ident::new(
&format!("_{}__CustomMsg", component),
proc_macro2::Span::call_site(),
);
let derive_component_str = derive_component.to_string();
tokens.extend(quote! {
#impl_item
#[allow(non_camel_case_types)]
pub struct #derive_msg(#component_msg);
#[allow(non_camel_case_types)]
#[wasm_bindgen]
pub struct #derive_component{
program: Program<#component<#derive_msg>, #derive_msg>,
}
#[wasm_bindgen]
impl #derive_component {
#[wasm_bindgen(constructor)]
pub fn new(node: JsValue) -> Self {
use sauron::wasm_bindgen::JsCast;
log::info!("constructor..");
let mount_node: &web_sys::Node = node.unchecked_ref();
Self {
program: Program::new(
#component::default(),
mount_node,
false,
true,
),
}
}
#[wasm_bindgen(method)]
pub fn observed_attributes() -> JsValue {
JsValue::from_serde(&<#component::<#derive_msg> as Component<#component_msg, #derive_msg>>::observed_attributes())
.expect("must parse from serde")
}
#[wasm_bindgen(method)]
pub fn attribute_changed_callback(&self) {
use std::ops::DerefMut;
use sauron::wasm_bindgen::JsCast;
log::info!("attribute changed...");
let mount_node = self.program.mount_node();
let mount_element: &web_sys::Element = mount_node.unchecked_ref();
let attribute_names = mount_element.get_attribute_names();
let len = attribute_names.length();
let mut attribute_values: std::collections::BTreeMap<String, String> = std::collections::BTreeMap::new();
for i in 0..len {
let name = attribute_names.get(i);
let attr_name =
name.as_string().expect("must be a string attribute");
if let Some(attr_value) = mount_element.get_attribute(&attr_name) {
attribute_values.insert(attr_name, attr_value);
}
}
<#component<#derive_msg> as Component<#component_msg, #derive_msg>>::attributes_changed(self.program.app.borrow_mut().deref_mut(), attribute_values);
}
#[wasm_bindgen(method)]
pub fn connected_callback(&mut self) {
use std::ops::Deref;
self.program.mount();
log::info!("Component is connected..");
let component_style = <#component<#derive_msg> as Component<#component_msg, #derive_msg>>::style(self.program.app.borrow().deref());
self.program.inject_style_to_mount(&component_style);
self.program.update_dom();
}
#[wasm_bindgen(method)]
pub fn disconnected_callback(&mut self) {
log::info!("Component is disconnected..");
}
#[wasm_bindgen(method)]
pub fn adopted_callback(&mut self) {
log::info!("Component is adopted..");
}
}
impl Application<#derive_msg> for #component<#derive_msg> {
fn update(&mut self, msg: #derive_msg) -> Cmd<Self, #derive_msg> {
let mount_attributes = <Self as Component<#component_msg, #derive_msg>>::attributes_for_mount(self);
Cmd::batch([
Cmd::from(
<Self as Component<#component_msg, #derive_msg>>::update(
self, msg.0,
)
.localize(#derive_msg),
),
Cmd::new(|program| {
program.update_mount_attributes(mount_attributes);
}),
])
}
fn view(&self) -> Node<#derive_msg> {
<Self as Component<#component_msg, #derive_msg>>::view(self)
.map_msg(#derive_msg)
}
}
#[wasm_bindgen]
pub fn register(){
sauron::register_custom_element(#custom_tag, #derive_component_str, "HTMLElement");
}
});
} else {
panic!("Expecting a Path");
}
tokens.into()
}
fn impl_application(
impl_item: &syn::ItemImpl,
custom_tag: &proc_macro2::Literal,
component: &syn::PathSegment,
) -> proc_macro::TokenStream {
let mut tokens = proc_macro2::TokenStream::new();
let app_msg = get_msg_generic_ident(&component);
let self_type = &impl_item.self_ty;
if let syn::Type::Path(type_path) = self_type.as_ref() {
let path_segment = &type_path.path.segments[0];
let app = &path_segment.ident;
let derive_component = proc_macro2::Ident::new(
&format!("_{}__CustomElement", app),
proc_macro2::Span::call_site(),
);
let derive_component_str = derive_component.to_string();
tokens.extend(quote! {
#impl_item
#[allow(non_camel_case_types)]
#[wasm_bindgen]
pub struct #derive_component{
program: Program<#app, #app_msg>,
}
#[wasm_bindgen]
impl #derive_component {
#[wasm_bindgen(constructor)]
pub fn new(node: JsValue) -> Self {
use sauron::wasm_bindgen::JsCast;
log::info!("constructor..");
let mount_node: &web_sys::Node = node.unchecked_ref();
Self {
program: Program::new(
#app::default(),
mount_node,
false,
true,
),
}
}
#[wasm_bindgen(method)]
pub fn observed_attributes() -> JsValue {
JsValue::from_serde(&<#app as Application<#app_msg>>::observed_attributes())
.expect("must parse from serde")
}
#[wasm_bindgen(method)]
pub fn attribute_changed_callback(&self) {
use sauron::wasm_bindgen::JsCast;
log::info!("attribute changed...");
let mount_node = self.program.mount_node();
let mount_element: &web_sys::Element = mount_node.unchecked_ref();
let attribute_names = mount_element.get_attribute_names();
let len = attribute_names.length();
let mut attribute_values: std::collections::BTreeMap<String, String> = std::collections::BTreeMap::new();
for i in 0..len {
let name = attribute_names.get(i);
let attr_name =
name.as_string().expect("must be a string attribute");
if let Some(attr_value) = mount_element.get_attribute(&attr_name) {
attribute_values.insert(attr_name, attr_value);
}
}
self.program
.app
.borrow_mut()
.attributes_changed(attribute_values);
}
#[wasm_bindgen(method)]
pub fn connected_callback(&mut self) {
use std::ops::Deref;
self.program.mount();
log::info!("Application is connected..");
let app_style = self.program.app.borrow().style();
self.program.inject_style_to_mount(&app_style);
self.program.update_dom();
}
#[wasm_bindgen(method)]
pub fn disconnected_callback(&mut self) {
log::info!("Application is disconnected..");
}
#[wasm_bindgen(method)]
pub fn adopted_callback(&mut self) {
log::info!("Application is adopted..");
}
}
#[wasm_bindgen]
pub fn register_application(){
sauron::register_custom_element(#custom_tag, #derive_component_str, "HTMLElement");
}
});
} else {
panic!("Expecting a Path");
}
tokens.into()
}
fn get_msg_generic_ident(component: &syn::PathSegment) -> proc_macro2::Ident {
let component_msg =
if let syn::PathArguments::AngleBracketed(component_msg) =
&component.arguments
{
let first_arg_generics = &component_msg.args[0];
if let syn::GenericArgument::Type(type_) = first_arg_generics {
if let syn::Type::Path(type_path) = type_ {
let generic = type_path
.path
.segments
.last()
.expect("must have a generic path segment");
generic.ident.clone()
} else {
panic!("expecting a type path");
}
} else {
panic!("expecting a generic argument type");
}
} else {
panic!("expecting a generic argument");
};
component_msg
}