Skip to content

Latest commit

 

History

History
33 lines (21 loc) · 1.47 KB

synchronous-instantiation.md

File metadata and controls

33 lines (21 loc) · 1.47 KB

Synchronous Instantiation

View full source code

This example shows how to synchronously initialize a WebAssembly module as opposed to asynchronously. In most cases, the default way of asynchronously initializing a module will suffice. However, there might be use cases where you'd like to lazy load a module on demand and synchronously compile and instantiate it. Note that this only works off the main thread and since compilation and instantiation of large modules can be expensive you should only use this method if it's absolutely required in your use case. Otherwise you should use the default method.

For this deployment strategy bundlers like Webpack are not required. For more information on deployment see the dedicated documentation.

First let's take a look at our tiny lib:

{{#include ../../../examples/synchronous-instantiation/src/lib.rs}}

Next, let's have a look at the index.html:

{{#include ../../../examples/synchronous-instantiation/index.html}}

Otherwise the rest of the magic happens in worker.js:

{{#include ../../../examples/synchronous-instantiation/worker.js}}

And that's it! Be sure to read up on the deployment options to see what it means to deploy without a bundler.