Skip to content

Commit d7f4c4e

Browse files
committed
docs: document the initializer
1 parent a49b89c commit d7f4c4e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

site/content/advanced.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,46 @@ And then, define the entrypoint in your `lib.rs` like (does not need to be `asyn
2929
#[wasm_bindgen(start)]
3030
pub async fn run() {}
3131
```
32+
33+
## Initializer
34+
35+
Since: `0.19.0-alpha.1`.
36+
37+
Trunk supports tapping into the initialization process of the WASM application. By
38+
default, this is not active and works the same way as with previous versions.
39+
40+
The default process is that trunk injects a small JavaScript snippet, which imports the JavaScript loader generated
41+
by `wasm_bindgen` and calls the `init` method. That will fetch the WASM blob and run it.
42+
43+
The downside with is, that during this process, there's no feedback to the user. Neither when it takes a bit longer to
44+
load the WASM file, nor when something goes wrong.
45+
46+
Now it is possible to tap into this process by setting `data-initializer` to a JavaScript module file. This module file
47+
is required to (default) export a function, which returns the "initializer" instance. Here is an example:
48+
49+
```javascript
50+
export default function myInitializer () {
51+
return {
52+
onStart: () => {
53+
// called when the loading starts
54+
},
55+
onProgress: ({current, total}) => {
56+
// the progress while loading, will be called periodically.
57+
// "current" will contain the number of bytes of the WASM already loaded
58+
// "total" will either contain the total number of bytes expected for the WASM, or if the server did not provide
59+
// the content-length header it will contain 0.
60+
},
61+
onComplete: () => {
62+
// called when the initialization is complete (successfully or failed)
63+
},
64+
onSuccess: (wasm) => {
65+
// called when the initialization is completed successfully, receives the `wasm` instance
66+
},
67+
onFailure: (error) => {
68+
// called when the initialization is completed with an error, receives the `error`
69+
}
70+
}
71+
};
72+
```
73+
74+
For a full example see: <https://github.com/trunk-rs/trunk/examples/initializer>.

0 commit comments

Comments
 (0)