wasm_main_executor

Crates.iowasm_main_executor
lib.rswasm_main_executor
version0.1.0
sourcesrc
created_at2023-04-28 02:10:27.224733
updated_at2023-04-28 02:10:27.224733
descriptionRun futures from web workers on the main browser thread.
homepage
repositoryhttps://github.com/DouglasDwyer/wasm_main_executor
max_upload_size
id851113
size8,910
Douglas Dwyer (DouglasDwyer)

documentation

README

wasm_main_executor

Run futures on the main browser thread

Crates.io Docs.rs

Certain tasks, like creating an AudioContext or RtcPeerConnection, can only be performed on the main browser thread. wasm_main_executor provides an easy way to send futures to the main browser thread from any context. This allows web workers to spawn main-threaded tasks and await their completion, and facilitates the implementation of cross-thread polyfills/shims.

Usage

The following is a simple example of executor usage:

async fn test_future() -> i32 {
    // Do some async or main-threaded work...
    2
}

// Start the executor. This must be called from the main browser thread.
wasm_main_executor::initialize().unwrap();

// Futures may be spawned on background threads using the executor.
// The future runs on the main thread.
let fut = wasm_main_executor::spawn(test_future());

// A future is returned which may be awaited on the background thread.
assert_eq!(2, futures::executor::block_on(fut));
Commit count: 6

cargo fmt