Skip to content

Commit

Permalink
Merge pull request #365 from cunarist/tokio-with-wasm
Browse files Browse the repository at this point in the history
Upgrade `tokio_with_wasm` to 0.5.3
  • Loading branch information
temeddix authored Jun 11, 2024
2 parents 5d63209 + 91d410d commit c1b7a16
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 22 deletions.
24 changes: 12 additions & 12 deletions automate/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ def replace_text_in_file(filepath: str, change_from: str, change_to: str):
)
replace_text_in_file(
"native/hub/src/lib.rs",
"// use tokio_with_wasm",
"use tokio_with_wasm",
"// use tokio_with_wasm::alias as tokio;",
"use tokio_with_wasm::alias as tokio;",
)
replace_text_in_file(
"native/hub/Cargo.toml",
"# wasm-bindgen",
"wasm-bindgen",
"# tokio_with_wasm",
"tokio_with_wasm",
)
replace_text_in_file(
"native/hub/Cargo.toml",
"# tokio_with_wasm",
"tokio_with_wasm",
"# wasm-bindgen",
"wasm-bindgen",
)

os.chdir("../")
Expand Down Expand Up @@ -109,18 +109,18 @@ def replace_text_in_file(filepath: str, change_from: str, change_to: str):
)
replace_text_in_file(
"native/hub/src/lib.rs",
"// use tokio_with_wasm",
"use tokio_with_wasm",
"// use tokio_with_wasm::alias as tokio;",
"use tokio_with_wasm::alias as tokio;",
)
replace_text_in_file(
"native/hub/Cargo.toml",
"# wasm-bindgen",
"wasm-bindgen",
"# tokio_with_wasm",
"tokio_with_wasm",
)
replace_text_in_file(
"native/hub/Cargo.toml",
"# tokio_with_wasm",
"tokio_with_wasm",
"# wasm-bindgen",
"wasm-bindgen",
)

os.chdir("../")
Expand Down
2 changes: 1 addition & 1 deletion documentation/docs/frequently-asked-questions.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ Here are the current constraints of the `wasm32-unknown-unknown` target:

- Numerous functionalities within `std::fs` remain unimplemented.
- Various features of `std::net` are not available. Consider using `reqwest` crate instead. `reqwest` supports `wasm32-unknown-unknown` and relies on JavaScript to perform network communications.
- `std::thread::spawn` doesn't work. Consider using `tokio_with_wasm::tokio::task::spawn_blocking` instead.
- `std::thread::spawn` doesn't work. Consider using `tokio_with_wasm::task::spawn_blocking` instead.
- Several features of `std::time::Instant` are unimplemented. Consider using `chrono` as an alternative. `chrono` supports `wasm32-unknown-unknown` and relies on JavaScript to obtain system time.
- In case of a panic in an asynchronous Rust task, it aborts and throws a JavaScript `RuntimeError` [which Rust cannot catch](https://stackoverflow.com/questions/59426545/rust-paniccatch-unwind-no-use-in-webassembly). A recommended practice is to replace `.unwrap` with `.expect` or handle errors with `Err` instances.

Expand Down
11 changes: 8 additions & 3 deletions flutter_ffi_plugin/example/native/hub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ crate-type = ["lib", "cdylib", "staticlib"]
[dependencies]
rinf = "6.12.1"
prost = "0.12.6"
# tokio = { version = "1", features = ["sync"] }
wasm-bindgen = "0.2.92" # Uncomment this line to target the web
tokio_with_wasm = "0.4.4" # Uncomment this line to target the web
tokio = { version = "1", features = ["rt", "sync", "macros", "time"] }
tokio_with_wasm = { version = "0.5.3", features = [
"rt",
"sync",
"macros",
"time",
] }
wasm-bindgen = "0.2.92"
sample_crate = { path = "../sample_crate" }
2 changes: 1 addition & 1 deletion flutter_ffi_plugin/example/native/hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod messages;
mod sample_functions;

// use tokio;
use tokio_with_wasm::tokio; // Uncomment this line to target the web
use tokio_with_wasm::alias as tokio;

rinf::write_interface!();

Expand Down
10 changes: 9 additions & 1 deletion flutter_ffi_plugin/example/native/hub/src/sample_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,13 @@ pub async fn run_debug_tests() {
}

debug_print!("Debug tests completed!");
panic!("INTENTIONAL DEBUG PANIC");

tokio::spawn(async {
// Panic in a separate task
// to avoid memory leak on the web.
// On the web (`wasm32-unknown-unknown`),
// catching and unwinding panics is not possible.
// It is better to avoid panicking code at all costs on the web.
panic!("INTENTIONAL DEBUG PANIC");
});
}
6 changes: 4 additions & 2 deletions flutter_ffi_plugin/template/native/hub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ crate-type = ["lib", "cdylib", "staticlib"]
rinf = "6.12.1"
prost = "0.12.6"
tokio = { version = "1", features = ["sync"] }
# wasm-bindgen = "0.2.92" # Uncomment this line to target the web
# tokio_with_wasm = "0.4.4" # Uncomment this line to target the web

# Uncomment below to target the web.
# tokio_with_wasm = { version = "0.5.3", features = ["sync"] }
# wasm-bindgen = "0.2.92"
4 changes: 2 additions & 2 deletions flutter_ffi_plugin/template/native/hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
mod messages;

use tokio;
// use tokio_with_wasm::tokio; // Uncomment this line to target the web
use tokio; // Comment this line to target the web.
// use tokio_with_wasm::alias as tokio; // Uncomment this line to target the web.

rinf::write_interface!();

Expand Down

0 comments on commit c1b7a16

Please sign in to comment.