A wrapper library around hotdrink-rs
for compilation to WebAssembly.
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
.
Add the following to your Cargo.toml
:
toml
hotdrink-wasm = "0.1.1"
```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);
pub fn makecs() -> Result
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();
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 hotdrinkrs::thread::TerminationStrategy; use hotdrinkwasm::{componenttypewrapper};
use hotdrinkwasm::{constraintsystemwrapperthreaded, thread::StaticPool};
componenttypewrapper! { pub struct ValueWrapper { #[derive(Clone, Debug)] pub enum Value { i32, String } } }
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.
Licensed under either of
at your option.
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.