hotdrink-wasm

Crates.iohotdrink-wasm
lib.rshotdrink-wasm
version0.1.3
sourcesrc
created_at2021-04-14 11:27:06.950766
updated_at2021-05-04 12:42:20.447088
descriptionWebAssembly bindings for hotdrink-rs.
homepage
repositoryhttps://github.com/HotDrink/hotdrink-rs
max_upload_size
id383946
size91,071
(rudsvar)

documentation

README

hotdrink-wasm

A wrapper library around hotdrink-rs for compilation to WebAssembly.

Crates.io docs.rs

Prerequisites

The project uses multiple nightly features, and must be built using nightly Rust. I recommend using rustup, which can be downloaded here,

You also need wasm-pack to compile your project to WebAssembly, which can be downloaded here.

The standard library must be recompiled with atomics enabled to use Web Workers as threads, which means that we need the standard library source code. This can be downloaded with rustup component add rust-src.

Usage

Add the following to your Cargo.toml:

hotdrink-wasm = "0.1.1"

Single threaded

use hotdrink_rs::{component, model::ConstraintSystem};
use hotdrink_wasm::{component_type_wrapper, constraint_system_wrapper};
use wasm_bindgen::{JsValue, prelude::wasm_bindgen};

component_type_wrapper! {
    pub struct ValueWrapper {
        #[derive(Clone, Debug)]
        pub enum Value {
            i32,
            String
        }
    }
}

constraint_system_wrapper!(MyCs, ValueWrapper, Value);

#[wasm_bindgen]
pub fn make_cs() -> Result<MyCs, JsValue> {
    let mut cs = ConstraintSystem::new();
    cs.add_component(component! {
        component MyComponent {
            let a: i32 = 0, b: String = "";
            // <contraints>
        }
    });
    MyCs::wrap(cs)
}

After producing a JavaScript module in www/pkg with

wasm-pack build --out-dir www/pkg --release

you can use the wrapper like this:

let cs = wasm.make_cs();
cs.subscribe("MyComponent", "a",
    new_value => console.log("a =", new_value),
    () => console.log("a is pending"),
    err => console.log("a failed:", err)
);
cs.set_variable("MyComponent", "a", wasm.ValueWrapper.i32(5));
cs.set_variable("MyComponent", "b", wasm.ValueWrapper.String("Hello"));
cs.update();

Multithreaded

Remember to add the thread feature flag in your Cargo.toml.

hotdrink-wasm = { version = "0.1.1", features = ["thread"] }

To use a multithreaded constraint system, you would create it like this instead:

use hotdrink_wasm::{component_type_wrapper};
#[cfg(feature = "thread")]
use hotdrink_wasm::{constraint_system_wrapper_threaded, thread::{StaticPool, TerminationStrategy}};

component_type_wrapper! {
    pub struct ValueWrapper {
        #[derive(Clone, Debug)]
        pub enum Value {
            i32,
            String
        }
    }
}

#[cfg(feature = "thread")]
constraint_system_wrapper_threaded!(
    MyCs,
    ValueWrapper,
    Value,
    StaticPool, // Or DynamicPool
    4,          // Number of threads
    TerminationStrategy::UnusedResultAndNotDone
);

To use Web Workers from Rust, the we must compile with --target no-modules.

wasm-pack build --out-dir www/pkg --target no-modules --release

This will produce WebAssembly code and JS wrappers in www/pkg, which can then be imported there. See wasm-pack's documentation for more information.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 239

cargo fmt