Skip to content

Commit 81ee3e0

Browse files
committed
examples: rust_from_js
1 parent 1ca2fad commit 81ee3e0

File tree

7 files changed

+143
-2
lines changed

7 files changed

+143
-2
lines changed

Diff for: Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ members = [
136136
"examples/pages_hash_routing",
137137
"examples/pages_keep_state",
138138
"examples/resize_observer",
139-
# "examples/server_integration", # has own workspace
139+
"examples/rust_from_js",
140140
"examples/service_worker",
141141
"examples/subscribe",
142142
"examples/tests",
@@ -150,8 +150,8 @@ members = [
150150
"examples/window_events",
151151
]
152152

153+
# they have own workspaces
153154
exclude = [
154-
# it has own workspace
155155
"examples/e2e_encryption",
156156
"examples/server_integration",
157157
]

Diff for: examples/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Pages keep their state.
6666
### [ResizeObserver](resize_observer)
6767
How to use [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver).
6868

69+
## [Rust from JS](rust_from_js)
70+
How to call Rust functions from Javascript.
71+
6972
### [Subscribe](subscribe)
7073
How to create and use subscriptions, streams, notifications and commands.
7174

Diff for: examples/rust_from_js/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "rust_from_js"
3+
version = "0.1.0"
4+
authors = ["Martin Kavík <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
seed = {path = "../../"}

Diff for: examples/rust_from_js/Makefile.toml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extend = "../../Makefile.toml"
2+
3+
# ---- BUILD ----
4+
5+
[tasks.build]
6+
alias = "default_build"
7+
8+
[tasks.build_release]
9+
alias = "default_build_release"
10+
11+
# ---- START ----
12+
13+
[tasks.start]
14+
alias = "default_start"
15+
16+
[tasks.start_release]
17+
alias = "default_start_release"
18+
19+
# ---- TEST ----
20+
21+
[tasks.test_firefox]
22+
alias = "default_test_firefox"
23+
24+
# ---- LINT ----
25+
26+
[tasks.clippy]
27+
alias = "default_clippy"

Diff for: examples/rust_from_js/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Rust from JS example
2+
3+
How to call Rust functions from Javascript.
4+
5+
_Note:_ See example `update_from_js` for more advanced example without the mutable global variable as a state.
6+
7+
---
8+
9+
```bash
10+
cargo make start
11+
```
12+
13+
Open [127.0.0.1:8000](http://127.0.0.1:8000) in your browser.
14+
15+
See the code in `index.html`.
16+
17+
Run in the browser dev console `rust.set_title("New title")` and click the `Rerender` button.

Diff for: examples/rust_from_js/index.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
7+
<title>Rust from JS example</title>
8+
</head>
9+
10+
<body>
11+
<section id="app"></section>
12+
<script type="module">
13+
import { set_title } from '/pkg/package.js';
14+
window.rust = { set_title };
15+
16+
import init from '/pkg/package.js';
17+
init('/pkg/package_bg.wasm');
18+
</script>
19+
</body>
20+
21+
</html>

Diff for: examples/rust_from_js/src/lib.rs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use seed::{prelude::*, *};
2+
use std::cell::RefCell;
3+
4+
thread_local!(static TITLE: RefCell<String> = RefCell::new("I'm TITLE!".to_owned()));
5+
6+
#[wasm_bindgen]
7+
pub fn set_title(title: String) {
8+
TITLE.with(|title_cell| title_cell.replace(title));
9+
}
10+
11+
fn title() -> String {
12+
TITLE.with(|title_cell| title_cell.borrow().clone())
13+
}
14+
15+
// ------ ------
16+
// Init
17+
// ------ ------
18+
19+
fn init(_: Url, _: &mut impl Orders<Msg>) -> Model {
20+
Model
21+
}
22+
23+
// ------ ------
24+
// Model
25+
// ------ ------
26+
27+
struct Model;
28+
29+
// ------ ------
30+
// Update
31+
// ------ ------
32+
33+
#[derive(Clone, Copy)]
34+
enum Msg {
35+
Rerender,
36+
}
37+
38+
fn update(msg: Msg, _: &mut Model, _: &mut impl Orders<Msg>) {
39+
match msg {
40+
Msg::Rerender => log!("Rerendered"),
41+
}
42+
}
43+
44+
// ------ ------
45+
// View
46+
// ------ ------
47+
48+
fn view(_: &Model) -> Vec<Node<Msg>> {
49+
vec![
50+
h1![title()],
51+
button!["Rerender", ev(Ev::Click, |_| Msg::Rerender)],
52+
]
53+
}
54+
55+
// ------ ------
56+
// Start
57+
// ------ ------
58+
59+
#[wasm_bindgen(start)]
60+
pub fn start() {
61+
App::start("app", init, update, view);
62+
}

0 commit comments

Comments
 (0)