Skip to content

Added Vite bundler support, Channels & Async functions #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Roba1993
Copy link

To support the Vite bundler (and possible others), I figured out, that providing the wasm.js file as a path is not enough. Additionally also the direct path to the was.wasm file need to be provided. If both are delivered correctly it's working. This solves my issue #4.

The implementation simply adds a new path for the actual wasm file. Please let me know, when the naming is still a bit off.

Following my example code from my index.js file loaded with Vite:

import wasmUrl from "../pkg/webapp.js?url";
import wasmBgUrl from "../pkg/webapp_bg.wasm?url";
import init, {
  main,
  initWorkerPool,
  WorkerPoolOptions,
} from "../pkg/webapp.js";
async function run() {
  // main app init and run
  await init();
  main();

  // web worker setup
  let options = new WorkerPoolOptions();
  options.path = window.location.origin + wasmUrl;
  options.path_bg = window.location.origin + wasmBgUrl;
  options.num_workers = 4;
  initWorkerPool(options);
}
run();

This leaves one problem for now. Using the worker pool is only possible after it has been fully loaded. With this configuration the main rust code is running before the WebWorkerPool is fully initialized. Using it inside rust before will create a new default one which will fail. This is due to the actual implementation here.

.get_or_init(|| async {

Could this be changed to something which is waiting until the pool is ready?

My local workaround for this is the following ugly code:

pub async fn ww_pool() -> &'static wasmworker::pool::WebWorkerPool {
    while !wasmworker::has_worker_pool() {
        sleep(Duration::from_millis(100)).await;
    }
    wasmworker::worker_pool().await
}

@Roba1993 Roba1993 changed the title Support of Vite bundler Added Vite bundler support, Channels & Async functions May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant