Skip to content

Commit

Permalink
docs: document the initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Feb 2, 2024
1 parent a49b89c commit d7f4c4e
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions site/content/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,46 @@ And then, define the entrypoint in your `lib.rs` like (does not need to be `asyn
#[wasm_bindgen(start)]
pub async fn run() {}
```

## Initializer

Since: `0.19.0-alpha.1`.

Trunk supports tapping into the initialization process of the WASM application. By
default, this is not active and works the same way as with previous versions.

The default process is that trunk injects a small JavaScript snippet, which imports the JavaScript loader generated
by `wasm_bindgen` and calls the `init` method. That will fetch the WASM blob and run it.

The downside with is, that during this process, there's no feedback to the user. Neither when it takes a bit longer to
load the WASM file, nor when something goes wrong.

Now it is possible to tap into this process by setting `data-initializer` to a JavaScript module file. This module file
is required to (default) export a function, which returns the "initializer" instance. Here is an example:

```javascript
export default function myInitializer () {
return {
onStart: () => {
// called when the loading starts
},
onProgress: ({current, total}) => {
// the progress while loading, will be called periodically.
// "current" will contain the number of bytes of the WASM already loaded
// "total" will either contain the total number of bytes expected for the WASM, or if the server did not provide
// the content-length header it will contain 0.
},
onComplete: () => {
// called when the initialization is complete (successfully or failed)
},
onSuccess: (wasm) => {
// called when the initialization is completed successfully, receives the `wasm` instance
},
onFailure: (error) => {
// called when the initialization is completed with an error, receives the `error`
}
}
};
```

For a full example see: <https://github.com/trunk-rs/trunk/examples/initializer>.

0 comments on commit d7f4c4e

Please sign in to comment.