Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Migration Guide for 0.0.7

Notable changes

  1. Removing the async feature as wasm-bindgen-futures crate has now been folded into js-sys and thus removed as an (optional) dependency of wasm-bindgen-spawn. All items that were previously available behind the async feature are now always available.
  2. Support for panic=unwind: join() now returns Result<T, Box<dyn Any + Send + 'static>>. When a thread panics, the panic payload can be observed by the JoinHandle just like the standard library.
  3. ThreadCreator struct 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.
  4. spawn now panics if the thread creation fails as it is unexpected and unlikely - just like the std library. Introduced try_spawn for a recoverable version.
  5. Expanded target support to other package formats supported by wasm-pack
  6. 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

  1. 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
  2. Remove the async feature from wasm-bindgen-spawn.

    • optionally, if you already depend on js-sys, remove wasm-bindgen-futures dependency and set --cfg=wasm_bindgen_use_js_sys to let wasm_bindgen use js-sys in the code generated by macros
  3. Remove ThreadCreator everywhere, replace the init site with the new APIs. See Creating the thread dispatcher

  4. Remove ? or .unwrap(), or any other error handling at each .spawn call site.

    • Or change spawn to try_spawn
  5. Update the error handling at each join call site. You can handle the error as you would using std::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"
          }
      }
      }