Crates.io | hotdrink-wasm |
lib.rs | hotdrink-wasm |
version | 0.1.3 |
source | src |
created_at | 2021-04-14 11:27:06.950766 |
updated_at | 2021-05-04 12:42:20.447088 |
description | WebAssembly bindings for hotdrink-rs. |
homepage | |
repository | https://github.com/HotDrink/hotdrink-rs |
max_upload_size | |
id | 383946 |
size | 91,071 |
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
:
hotdrink-wasm = "0.1.1"
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();
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.
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.