@@ -29,3 +29,46 @@ And then, define the entrypoint in your `lib.rs` like (does not need to be `asyn
29
29
#[wasm_bindgen(start)]
30
30
pub async fn run () {}
31
31
```
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