forked from fitzgen/dodrio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_api.rs
153 lines (136 loc) · 4.51 KB
/
js_api.rs
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
use super::{assert_rendered, create_element, RenderFn};
use dodrio::{builder::*, Node, Render, RenderContext, Vdom};
use dodrio_js_api::JsRender;
use futures::channel::oneshot;
use js_sys::Object;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_test::*;
pub struct WrapJs {
inner: JsRender,
}
impl WrapJs {
pub fn new() -> (WrapJs, oneshot::Receiver<()>, Closure<dyn FnMut()>) {
let (sender, receiver) = oneshot::channel();
let on_after_render = Closure::wrap(Box::new({
let mut sender = Some(sender);
move || {
sender
.take()
.expect_throw("on_after_render should only be called once")
.send(())
.expect_throw("receiver should not have been dropped");
}
}) as Box<dyn FnMut()>);
let component = WrapJs {
inner: JsRender::new(JsComponent::new(&on_after_render)),
};
(component, receiver, on_after_render)
}
}
impl<'a> Render<'a> for WrapJs {
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
div(&cx)
.attr("class", "wrap-js")
.children([self.inner.render(cx)])
.finish()
}
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
type JsComponent;
#[wasm_bindgen(constructor)]
fn new(on_after_render: &Closure<dyn FnMut()>) -> JsComponent;
}
fn eval_js_rendering_component() {
use std::sync::Once;
static EVAL: Once = Once::new();
EVAL.call_once(|| {
super::init_logging();
js_sys::eval(
r#"
window.JsComponent = class JsComponent {
constructor(onAfterRender) {
this.count = 0;
this.onAfterRender = onAfterRender;
}
render() {
return {
tagName: "span",
attributes: [{ name: "class", value: "js-component" }],
listeners: [{ on: "click", callback: this.onClick.bind(this) }],
children: [
"Here is some plain text",
{
tagName: "b",
children: ["...and here is some bold text"]
},
String(this.count),
]
};
}
async onClick(vdom, event) {
this.count++;
await vdom.render();
this.onAfterRender();
}
};
"#,
)
.expect_throw("should eval JS component OK");
});
}
#[wasm_bindgen_test]
async fn can_use_js_rendering_components() {
eval_js_rendering_component();
let container = create_element("div");
let (component, receiver, _closure) = WrapJs::new();
let _vdom = Rc::new(Vdom::new(&container, component));
assert_rendered(
&container,
&RenderFn(|cx| {
div(&cx)
.attr("class", "wrap-js")
.children([span(&cx)
.attr("class", "js-component")
.children([
text("Here is some plain text"),
b(&cx)
.children([text("...and here is some bold text")])
.finish(),
text("0"),
])
.finish()])
.finish()
}),
);
container
.query_selector(".js-component")
.expect_throw("should querySelector OK")
.expect_throw("should find `.js-component` element OK")
.dyn_into::<web_sys::HtmlElement>()
.expect_throw("should be an `HTMLElement` object")
.click();
receiver.await.unwrap();
assert_rendered(
&container,
&RenderFn(|cx| {
div(&cx)
.attr("class", "wrap-js")
.children([span(&cx)
.attr("class", "js-component")
.children([
text("Here is some plain text"),
b(&cx)
.children([text("...and here is some bold text")])
.finish(),
text("1"),
])
.finish()])
.finish()
}),
);
}