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:

toml hotdrink-wasm = "0.1.1"

Single threaded

```rust use hotdrinkrs::{component, model::ConstraintSystem}; use hotdrinkwasm::{componenttypewrapper, constraintsystemwrapper}; use wasmbindgen::{JsValue, prelude::wasmbindgen};

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

constraintsystemwrapper!(MyCs, ValueWrapper, Value);

[wasm_bindgen]

pub fn makecs() -> Result { let mut cs = ConstraintSystem::new(); cs.addcomponent(component! { component MyComponent { let a: i32 = 0, b: String = ""; // } }); MyCs::wrap(cs) } ```

After producing a JavaScript module in www/pkg with bash wasm-pack build --out-dir www/pkg --release you can use the wrapper like this:

javascript 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.

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

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

```rust use hotdrinkwasm::{componenttype_wrapper};

[cfg(feature = "thread")]

use hotdrinkwasm::{constraintsystemwrapperthreaded, thread::{StaticPool, TerminationStrategy}};

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

[cfg(feature = "thread")]

constraintsystemwrapper_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.

bash 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.