Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
  • Loading branch information
MingweiSamuel committed Sep 4, 2024
1 parent 82cacab commit 3153caf
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 43 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions wasm_test_site/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
hydroflow = { path = "../hydroflow" }
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = "0.4.43"
web-sys = { version = "0.3.51", features = [ "console" ] }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }
console_error_panic_hook = "0.1.7"

[dev-dependencies]
wasm-bindgen-test = "0.3.34"
Expand Down
4 changes: 4 additions & 0 deletions wasm_test_site/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Go to `www/README.md`

---

<div align="center">

<h1><code>wasm-pack-template</code></h1>
Expand Down
32 changes: 24 additions & 8 deletions wasm_test_site/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
mod utils;

use utils::set_panic_hook;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
fn log(string: impl AsRef<str>) {
web_sys::console::log_1(&JsValue::from_str(string.as_ref()));
}

#[wasm_bindgen]
pub fn greet() {
set_panic_hook();
alert("Hello, wasm_test_site!");
console_error_panic_hook::set_once();

log("Hello, wasm_test_site!");
}

#[wasm_bindgen]
pub fn test_hydroflow() -> web_sys::js_sys::Promise {
console_error_panic_hook::set_once();

let mut df = hydroflow::hydroflow_syntax! {
// https://hydro.run/docs/hydroflow/quickstart/example_1_simplest
source_iter(0..10) -> for_each(|n| log(format!("Hello {}", n)));
};

wasm_bindgen_futures::future_to_promise(async move {
let work_done = df.run_available_async().await;
Ok(if work_done {
JsValue::TRUE
} else {
JsValue::FALSE
})
})
}
10 changes: 0 additions & 10 deletions wasm_test_site/src/utils.rs

This file was deleted.

12 changes: 12 additions & 0 deletions wasm_test_site/www/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
Run once to init:
```
npm ci
```
Run to rebuild:
```
npm run wasm
npm run dev
```

---

<div align="center">

<h1><code>create-wasm-app</code></h1>
Expand Down
3 changes: 2 additions & 1 deletion wasm_test_site/www/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import init, { greet } from "wasm_test_site";
import init, { greet, test_hydroflow } from "wasm_test_site";
let { memory } = await init();
greet();
test_hydroflow();
2 changes: 1 addition & 1 deletion wasm_test_site/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"build": "vite build",
"serve": "vite serve",
"dev": "vite serve",
"preview": "vite preview",
"wasm": "wasm-pack build ../ --target web"
},
Expand Down
1 change: 0 additions & 1 deletion website_playground/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ serde-wasm-bindgen = "0.4.5"
syn = { version = "2.0.46", features = [ "parsing", "extra-traits" ] }
tokio = { version = "1.29.0", features = [ "sync" ] }
wasm-bindgen = "0.2.83"
wasm-bindgen-futures = "0.4.43"
web-sys = { version = "0.3.51", features = [ "console" ] }

# The `console_error_panic_hook` crate provides better debugging of panics by
Expand Down
17 changes: 0 additions & 17 deletions website_playground/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,3 @@ pub fn send_datalog_boolean_demo(instance_name: &str, input: i32) -> Option<i32>
}
})
}

#[wasm_bindgen]
pub fn test_hydroflow() -> web_sys::js_sys::Promise {
let mut df = hydroflow::hydroflow_syntax! {
// https://hydro.run/docs/hydroflow/quickstart/example_1_simplest
source_iter(0..10) -> for_each(|n| log(&format!("Hello {}", n)));
};

wasm_bindgen_futures::future_to_promise(async move {
let work_done = df.run_available_async().await;
Ok(if work_done {
JsValue::TRUE
} else {
JsValue::FALSE
})
})
}

0 comments on commit 3153caf

Please sign in to comment.