Modernizing async example

This commit is contained in:
Pauan 2021-05-28 13:29:17 +02:00
parent ca4e9651ad
commit 85cddfabdb
4 changed files with 31 additions and 28 deletions

View File

@ -14,22 +14,24 @@ lto = true
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]
[workspace]
[dependencies] [dependencies]
console_error_panic_hook = "0.1.5" console_error_panic_hook = "0.1.6"
dominator = "0.5.0" dominator = "0.5.18"
futures = "0.3.4" wasm-bindgen = "0.2.74"
futures-signals = "0.3.0" wasm-bindgen-futures = "0.4.24"
wasm-bindgen-futures = "0.4.9" futures = "0.3.15"
wasm-bindgen = "0.2.48" futures-signals = "0.3.20"
gloo-timers = { version = "0.2.1", features = ["futures"] } gloo-timers = { version = "0.2.1", features = ["futures"] }
js-sys = "0.3.36" js-sys = "0.3.51"
serde_json = "1.0.10" once_cell = "1.7.2"
serde_derive = "1.0.27" serde_json = "1.0.64"
serde = "1.0.27" serde_derive = "1.0.126"
lazy_static = "1.0.0" serde = "1.0.126"
[dependencies.web-sys] [dependencies.web-sys]
version = "0.3.22" version = "0.3.51"
features = [ features = [
"console", "console",
"AbortController", "AbortController",

View File

@ -8,10 +8,11 @@
"start": "rimraf dist/js && rollup --config --watch" "start": "rimraf dist/js && rollup --config --watch"
}, },
"devDependencies": { "devDependencies": {
"@wasm-tool/rollup-plugin-rust": "^1.0.0", "@wasm-tool/rollup-plugin-rust": "^1.0.6",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"rollup": "^1.31.0", "rollup": "^2.50.2",
"rollup-plugin-livereload": "^1.2.0", "rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-serve": "^1.0.1" "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 rust from "@wasm-tool/rollup-plugin-rust";
import serve from "rollup-plugin-serve"; import serve from "rollup-plugin-serve";
import livereload from "rollup-plugin-livereload"; import livereload from "rollup-plugin-livereload";
import { terser } from "rollup-plugin-terser";
const is_watch = !!process.env.ROLLUP_WATCH; const is_watch = !!process.env.ROLLUP_WATCH;
@ -16,7 +17,6 @@ export default {
plugins: [ plugins: [
rust({ rust({
serverPath: "js/", serverPath: "js/",
debug: false,
}), }),
is_watch && serve({ is_watch && serve({
@ -25,5 +25,7 @@ export default {
}), }),
is_watch && livereload("dist"), is_watch && livereload("dist"),
!is_watch && terser(),
], ],
}; };

View File

@ -1,11 +1,11 @@
use std::rc::Rc; use std::sync::Arc;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use gloo_timers::future::TimeoutFuture; use gloo_timers::future::TimeoutFuture;
use serde_derive::{Serialize, Deserialize}; use serde_derive::{Serialize, Deserialize};
use futures_signals::signal::{Mutable, not}; use futures_signals::signal::{Mutable, not};
use dominator::{html, class, events, clone, with_node, Dom}; use dominator::{html, class, events, clone, with_node, Dom};
use web_sys::HtmlInputElement; use web_sys::HtmlInputElement;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use util::*; use util::*;
mod util; mod util;
@ -61,27 +61,25 @@ struct App {
} }
impl App { impl App {
fn new(name: &str, user: Option<User>) -> Rc<Self> { fn new(name: &str, user: Option<User>) -> Arc<Self> {
Rc::new(Self { Arc::new(Self {
user: Mutable::new(user), user: Mutable::new(user),
input: Mutable::new(name.to_string()), input: Mutable::new(name.to_string()),
loader: AsyncLoader::new(), loader: AsyncLoader::new(),
}) })
} }
fn render(app: Rc<Self>) -> Dom { fn render(app: Arc<Self>) -> Dom {
lazy_static! { static APP: Lazy<String> = Lazy::new(|| class! {
static ref APP: String = class! { .style("white-space", "pre")
.style("white-space", "pre") });
};
}
html!("div", { html!("div", {
.class(&*APP) .class(&*APP)
.children(&mut [ .children(&mut [
html!("input" => HtmlInputElement, { html!("input" => HtmlInputElement, {
.property_signal("value", app.input.signal_cloned()) .prop_signal("value", app.input.signal_cloned())
.with_node!(element => { .with_node!(element => {
.event(clone!(app => move |_: events::Input| { .event(clone!(app => move |_: events::Input| {