Skip to content
This repository was archived by the owner on Mar 2, 2021. It is now read-only.

Fill out examples #16

Merged
merged 17 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ edition = "2018"
default = []

[dependencies]
bumpalo = "2.0.0"
futures = "0.1"
bumpalo = "2.1.0"
futures = "0.1.25"
wasm-bindgen = "0.2.34"
wasm-bindgen-futures = "0.3.10"
js-sys = "0.3.10"
wasm-bindgen-futures = "0.3.11"
js-sys = "0.3.11"
log = "0.4.6"

[dependencies.web-sys]
version = "0.3.10"
version = "0.3.11"
features = [
"console",
"Document",
Expand All @@ -27,11 +27,11 @@ features = [
]

[dev-dependencies]
wasm-bindgen-test = "0.2"
wasm-bindgen-test = "0.2.34"
console_log = "0.1.2"

[dev-dependencies.web-sys]
version = "0.3.10"
version = "0.3.11"
features = [
"Attr",
"EventTarget",
Expand All @@ -41,12 +41,14 @@ features = [
]

[profile.release]
# Tell `rustc` to optimize for small code size.
incremental = false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lto = true
opt-level = "s"

[workspace]
members = [
"./examples/counter",
"./examples/hello-world",
"./examples/input-form",
"./examples/todomvc",
]
10 changes: 5 additions & 5 deletions examples/counter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ authors = ["Nick Fitzgerald <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib"]

[features]

[dependencies]
console_error_panic_hook = "0.1.1"
console_error_panic_hook = "0.1.5"
console_log = "0.1.2"
dodrio = { path = "../.." }
log = "0.4.6"
wasm-bindgen = "0.2"
wasm-bindgen = "0.2.34"

[dependencies.web-sys]
version = "0.3.8"
version = "0.3.11"
features = [
"console",
"Document",
Expand All @@ -30,4 +30,4 @@ features = [
]

[dev-dependencies]
wasm-bindgen-test = "0.2"
wasm-bindgen-test = "0.2.34"
32 changes: 12 additions & 20 deletions examples/counter/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
# 🦀🕸 `rust-webpack-template`
# Counter

> **Kickstart your Rust, WebAssembly, and Webpack project!**
A counter that can be incremented and decremented.

This template is designed for creating monorepo-style Web applications with
Rust-generated WebAssembly and Webpack without publishing your wasm to NPM.
## Source

* Want to create and publish NPM packages with Rust and WebAssembly? [Check out
`wasm-pack-template`.](https://github.com/rustwasm/wasm-pack-template)
See `src/lib.rs`.

## 🔋 Batteries Included
## Build

This template comes pre-configured with all the boilerplate for compiling Rust
to WebAssembly and hooking into a Webpack build pipeline.

* `npm run start` -- Serve the project locally for development at
`http://localhost:8080`.

* `npm run build` -- Bundle the project (in production mode).

## 🚴 Using This Template
```
wasm-pack build --target no-modules
```

First, [install `wasm-pack`!](https://rustwasm.github.io/wasm-pack/installer/)
## Serve

Then, use `npm init` to clone this template:
Use any HTTP server, for example:

```sh
npm init rust-webpack my-app
```
python -m SimpleHTTPServer
```
32 changes: 25 additions & 7 deletions examples/counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,37 @@ use dodrio::{on, Node, Render};
use log::*;
use wasm_bindgen::prelude::*;

/// A counter that can be incremented and decrmented!
struct Counter {
val: isize,
count: isize,
}

impl Counter {
/// Construct a new, zeroed counter.
fn new() -> Counter {
Counter { val: 0 }
Counter { count: 0 }
}

/// Increment this counter's count.
fn increment(&mut self) {
self.val += 1;
self.count += 1;
}

/// Decrement this counter's count.
fn decrement(&mut self) {
self.val -= 1;
self.count -= 1;
}
}

// The `Render` implementation for `Counter`s displays the current count and has
// buttons to increment and decrement the count.
impl Render for Counter {
fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> dodrio::Node<'bump>
where
'a: 'bump,
{
let val = bumpalo::format!(in bump, "{}", self.val);
// Stringify the count as a bump-allocated string.
let count = bumpalo::format!(in bump, "{}", self.count);

Node::element(
bump,
Expand All @@ -38,17 +45,26 @@ impl Render for Counter {
bump,
"button",
[on(bump, "click", |root, vdom, _event| {
root.unwrap_mut::<Counter>().increment();
// Cast the root render component to a `Counter`, since
// we know that's what it is.
let counter = root.unwrap_mut::<Counter>();

// Increment the counter.
counter.increment();

// Since the count has updated, we should re-render the
// counter on the next animation frame.
vdom.schedule_render();
})],
[],
[Node::text("+")],
),
Node::text(val.into_bump_str()),
Node::text(count.into_bump_str()),
Node::element(
bump,
"button",
[on(bump, "click", |root, vdom, _event| {
// Same as above, but decrementing instead of incrementing.
root.unwrap_mut::<Counter>().decrement();
vdom.schedule_render();
})],
Expand All @@ -62,9 +78,11 @@ impl Render for Counter {

#[wasm_bindgen(start)]
pub fn run() {
// Initialize debug logging for if/when things go wrong.
console_error_panic_hook::set_once();
console_log::init_with_level(Level::Trace).expect("should initialize logging OK");

// Get the document's `<body>`.
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let body = document.body().unwrap();
Expand Down
10 changes: 5 additions & 5 deletions examples/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ authors = ["Nick Fitzgerald <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib"]

[features]

[dependencies]
dodrio = { path = "../.." }
wasm-bindgen = "0.2"
console_error_panic_hook = "0.1.1"
wasm-bindgen = "0.2.34"
console_error_panic_hook = "0.1.5"

[dependencies.web-sys]
version = "0.3.8"
version = "0.3.11"
features = [
"Document",
"HtmlElement",
Expand All @@ -24,4 +24,4 @@ features = [
]

[dev-dependencies]
wasm-bindgen-test = "0.2"
wasm-bindgen-test = "0.2.34"
Loading