init#
The default export of the browser build (pkg/plen_wasm.js). It fetches and instantiates the WebAssembly module. Await it once before the first new JsonProject or schedule call. The Node build (pkg-node/) has no init: require loads and instantiates the module synchronously.
Signature#
module_or_path?: : Promise<InitOutput>
;
Asynchronous. It's the only async step of the raw surface; every other export is synchronous.
Parameters#
| Name | Type | Description |
|---|---|---|
module_or_path |
{ module_or_path: InitInput | Promise<InitInput> } |
Optional. Where to load the module from. Omitted: plen_wasm_bg.wasm in the same folder as plen_wasm.js. |
Accepted values for module_or_path.module_or_path:
| Value | What init does |
|---|---|
URL string, URL, Request |
Fetches it, then instantiates the response. |
Response |
Instantiates the response body. Streams it when the server sends Content-Type: application/wasm; otherwise logs a warning and falls back to reading the whole body. |
BufferSource (bytes) |
Instantiates the bytes. |
WebAssembly.Module |
Instantiates the compiled module. |
| A promise of any of these | Awaits it, then handles the value as above. |
Passing the value directly, without the { module_or_path } wrapper, still works. wasm-bindgen marks that form as deprecated and logs a warning.
Behavior#
- Runs once per page or worker. When the module is already instantiated, a later
initorinitSynccall returns the existing instance and ignores its argument. - The exports read the module that
initinstalls. Callingnew JsonProject(...)orschedule(...)before the promise resolves throws aTypeError.
Returns#
A promise of the instantiated module's raw exports (InitOutput). These are binding-generator internals. Call the named exports (JsonProject, schedule) instead, and treat the resolved value only as a "ready" signal.
Errors#
| Error | When |
|---|---|
Rejects with Error (failed to fetch Wasm: ...) |
The fetched Response has a non-2xx status. |
| Rejects (fetch error) | The URL can't be fetched. |
Rejects (WebAssembly.CompileError / LinkError) |
The bytes aren't a valid module for this build. |
See Error handling for the other host-level failures.
initSync#
module: : InitOutput
;
Instantiates the module synchronously from bytes or a compiled WebAssembly.Module. Bytes are compiled on the calling thread. It fetches nothing, so it fits hosts that already hold the module, such as a worker that receives a WebAssembly.Module from the main thread. Like init, it returns the existing instance when the module is already instantiated. Passing the value without the { module } wrapper is deprecated and logs a warning. Invalid bytes throw synchronously.
Example#
await ;
const = ;
const = ;
Synchronous loading in a worker:
;
Build targets and the Node loading path are in Module and handles.
The package entry (plen-wasm) also has a default export init, used with the Project class. In the browser build, await init() before creating a Project; it loads the WebAssembly module and resolves to the module's exports. See JavaScript usage.
Related#
JsonProject- the project handle to create once the module is loadedschedule- one-shot scheduling without a handle- Module and Handles - build targets, loading and handle lifetime
- Run in a worker - loading the module off the main thread
- Error handling - host-level failures such as calling an export before
initresolves