Modernizing counter example

This commit is contained in:
Pauan 2021-05-28 13:37:14 +02:00
parent 85cddfabdb
commit 1d444bffad
4 changed files with 34 additions and 33 deletions

View File

@ -14,9 +14,11 @@ lto = true
[lib]
crate-type = ["cdylib"]
[workspace]
[dependencies]
console_error_panic_hook = "0.1.5"
dominator = "0.5.0"
wasm-bindgen = "0.2.48"
futures-signals = "0.3.0"
lazy_static = "1.0.0"
console_error_panic_hook = "0.1.6"
dominator = "0.5.18"
wasm-bindgen = "0.2.74"
futures-signals = "0.3.20"
once_cell = "1.7.2"

View File

@ -8,10 +8,11 @@
"start": "rimraf dist/js && rollup --config --watch"
},
"devDependencies": {
"@wasm-tool/rollup-plugin-rust": "^1.0.0",
"@wasm-tool/rollup-plugin-rust": "^1.0.6",
"rimraf": "^3.0.2",
"rollup": "^1.31.0",
"rollup-plugin-livereload": "^1.2.0",
"rollup-plugin-serve": "^1.0.1"
"rollup": "^2.50.2",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-serve": "^1.1.0",
"rollup-plugin-terser": "^7.0.2"
}
}

View File

@ -1,6 +1,7 @@
import rust from "@wasm-tool/rollup-plugin-rust";
import serve from "rollup-plugin-serve";
import livereload from "rollup-plugin-livereload";
import { terser } from "rollup-plugin-terser";
const is_watch = !!process.env.ROLLUP_WATCH;
@ -16,7 +17,6 @@ export default {
plugins: [
rust({
serverPath: "js/",
debug: false,
}),
is_watch && serve({
@ -25,5 +25,7 @@ export default {
}),
is_watch && livereload("dist"),
!is_watch && terser(),
],
};

View File

@ -1,15 +1,15 @@
use wasm_bindgen::prelude::*;
use std::sync::Arc;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use futures_signals::signal::{Mutable, SignalExt};
use dominator::{Dom, class, html, clone, events};
struct State {
struct App {
counter: Mutable<i32>,
}
impl State {
impl App {
fn new() -> Arc<Self> {
Arc::new(Self {
counter: Mutable::new(0),
@ -18,24 +18,22 @@ impl State {
fn render(state: Arc<Self>) -> Dom {
// Define CSS styles
lazy_static! {
static ref ROOT_CLASS: String = class! {
.style("display", "inline-block")
.style("background-color", "black")
.style("padding", "10px")
};
static ROOT_CLASS: Lazy<String> = Lazy::new(|| class! {
.style("display", "inline-block")
.style("background-color", "black")
.style("padding", "10px")
});
static ref TEXT_CLASS: String = class! {
.style("color", "white")
.style("font-weight", "bold")
};
static TEXT_CLASS: Lazy<String> = Lazy::new(|| class! {
.style("color", "white")
.style("font-weight", "bold")
});
static ref BUTTON_CLASS: String = class! {
.style("display", "block")
.style("width", "100px")
.style("margin", "5px")
};
}
static BUTTON_CLASS: Lazy<String> = Lazy::new(|| class! {
.style("display", "block")
.style("width", "100px")
.style("margin", "5px")
});
// Create the DOM nodes
html!("div", {
@ -84,10 +82,8 @@ pub fn main_js() -> Result<(), JsValue> {
#[cfg(debug_assertions)]
console_error_panic_hook::set_once();
let state = State::new();
dominator::append_dom(&dominator::body(), State::render(state));
let app = App::new();
dominator::append_dom(&dominator::body(), App::render(app));
Ok(())
}