async_wasm_task

Crates.ioasync_wasm_task
lib.rsasync_wasm_task
version0.2.3
sourcesrc
created_at2023-10-06 16:32:45.054632
updated_at2023-10-08 06:14:22.482156
descriptionManage the concurrency of async tasks in webassembly Rust
homepage
repositoryhttps://github.com/cunarist/async-wasm-task
max_upload_size
id995223
size29,236
Donghyun Kim (temeddix)

documentation

README

async_wasm_task

Crates.io Documentation License

bandicam 2023-10-07 17-34-36-577

Tested with Rust-In-Flutter

async_wasm_task is a Rust library that provides an API for managing asynchronous tasks and Rust Futures in a JavaScript environment, closely resembling the familiar patterns of tokio::task. It is designed to allow async Rust code to work seamlessly with JavaScript in web applications, leveraging web workers for parallel task execution.

Focusing on wasm32-unknown-unknown target, this library assumes that you're compilng your Rust project with wasm-pack. It spawns parallel web workers(threads) with the the same JavaScript file that the original web worker has been created with, so you might not need to deal with JavaScript code yourself.

The number of web workers are automatically adjusted adapting to the parallel tasks that has been queued by spawn_blocking. Refer to the docs for additional details.

Features

  • Familiar API: If you're familiar with tokio::task, you'll feel right at home with async_wasm_task. It provides similar functionality and follows the same patterns for spawning and managing asynchronous tasks.

  • Web Worker Integration: async_wasm_task adapts to the JavaScript environment by utilizing web workers under the hood. This means you can write Rust code that runs concurrently and efficiently in web applications.

  • Spawn Async and Blocking Tasks: You can spawn both asynchronous and blocking tasks. Asynchronous tasks allow you to perform non-blocking operations, while blocking tasks are suitable for compute-heavy or synchronous tasks.

Usage

Add this library to your Cargo.toml:

[dependencies]
async_wasm_task = "[latest-version]"

Here's a simple example of how to use async_wasm_task:

use async_wasm_task::{spawn, spawn_blocking, yield_now};

async fn start() {
    let async_join_handle = spawn(async {
        // Your asynchronous code here.
        // This will run concurrently
        // in the same web worker(thread).
    });
    let blocking_join_handle = spawn_blocking(|| {
        // Your blocking code here.
        // This will run parallelly
        // in the external pool of web workers.
    });
    let async_result = async_join_handle.await;
    let blocking_result = blocking_join_handle.await;
    for i in 1..1000 {
        // Some repeating task here
        // that shouldn't block the JavaScript runtime.
        yield_now().await;
    }
}

Documentation

Detailed documentation can be found on docs.rs.

Contributing

Contributions are welcome! If you have any suggestions, bug reports, or want to contribute to the development of async_wasm_task, please open an issue or submit a pull request.

Commit count: 31

cargo fmt