Migration Guide for 0.0.7
Notable changes
- Removing the
asyncfeature aswasm-bindgen-futurescrate has now been folded intojs-sysand thus removed as an (optional) dependency ofwasm-bindgen-spawn. All items that were previously available behind theasyncfeature are now always available. - Support for
panic=unwind:join()now returnsResult<T, Box<dyn Any + Send + 'static>>. When a thread panics, the panic payload can be observed by theJoinHandlejust like the standard library. ThreadCreatorstruct is removed and the thread dispatcher instance is not available as a global singleton automatically managed within this crate. The crate exposes APIs to manage the dispatcher singleton.spawnnow panics if the thread creation fails as it is unexpected and unlikely - just like the std library. Introducedtry_spawnfor a recoverable version.- Expanded target support to other package formats supported by
wasm-pack - Expanded support to native runtimes: NodeJS/Deno/Bun
Other notable changes that are transparent to users:
- Proper TypeScript in the build for the JS dispatcher implementation, so there is type safety across the Rust and JS code.
- End-to-end test coverage
Migration Checklist
-
Refer to Cargo config Source of truch for the updated
.cargo/config.toml. This is also the new location for the source-of-truth setup steps.- It’s recommended you also update the configs to use
panic=unwind. Please first read the wasm-bindgen book chapters:- https://wasm-bindgen.github.io/wasm-bindgen/reference/catch-unwind.html
- https://wasm-bindgen.github.io/wasm-bindgen/reference/handling-aborts.html
- It’s recommended you also update the configs to use
-
Remove the
asyncfeature fromwasm-bindgen-spawn.- optionally, if you already depend on
js-sys, removewasm-bindgen-futuresdependency and set--cfg=wasm_bindgen_use_js_systo let wasm_bindgen usejs-sysin the code generated by macros
- optionally, if you already depend on
-
Remove
ThreadCreatoreverywhere, replace the init site with the new APIs. See Creating the thread dispatcher -
Remove
?or.unwrap(), or any other error handling at each.spawncall site.- Or change
spawntotry_spawn
- Or change
-
Update the error handling at each
joincall site. You can handle the error as you would usingstd::thread::JoinHandle.join(). Please refer to:- https://doc.rust-lang.org/std/thread/fn.spawn.html
- https://doc.rust-lang.org/std/thread/struct.JoinHandle.html#method.join
- if previously you were printing the error you can try this helper function:
#![allow(unused)] fn main() { pub fn best_effort_panic_message<'a>(payload: &'a Box<dyn Any + Send + 'static>) -> &'a str { if let Some(s) = payload.downcast_ref::<&str>() { s } else if let Some(s) = payload.downcast_ref::<String>() { s.as_str() } else { "unknown panic info" } } }