Switch from the WASM package#
Move code written for the plen-wasm glue API to plen-js and drop the WebAssembly module and its async startup.
Both packages expose the same Project API: the same methods, options, data and result types, and the same schedule, InputError and ProjectError exports. Results match the WASM package's, and result keys come back in the same sorted order. Most code moves over with an import change.
Prerequisites#
- Code that uses the glue entry of
plen-wasm(Project,schedule,InputError,ProjectError, defaultinit). Code built on the rawJsonProjectexports and JSON strings has no counterpart here. - Node 18 or later, or a browser.
Steps#
-
Replace the dependency:
-
Change the import specifier. The export names stay the same:
// before // afterFor CommonJS, use
require. The named exports are direct, andinitis.default:const = ; -
Remove the
await init()call. AProjectworks right after the import:const = new; const = ;You don't need to call
init(); it exists only so code written for the WASM package works unchanged. If you keep the call, it resolvesundefinedimmediately. It doesn't return the module exports the WASMinit()returns, so remove any code that reads its result. Drop the.wasmfile path or bytes you passed to it too:initignores its argument. -
Keep or remove the
dispose()calls.dispose()is optional in JavaScript; after it, every call on that project throwsError("Project has been disposed"). Garbage collection frees the memory either way. Keep the calls if the same code must also run on the WASM package. -
Check code that depends on WASM-specific failures. Everything else, including
InputErrorkind,messageandpathandProjectErrorcode, stays the same:Case plen-wasmplen-jsA required argument is undefined, e.g.preview(undefined)wasm-bindgen argument error InputErrorA string contains a lone surrogate rejected accepted An input object has a getter read twice read once Internal engine failure WebAssembly trap ErrornamedPanicSee Error handling for the full list of errors.
Result#
The project schedules with no WebAssembly download or init() wait, and result has the same shape and values the WASM package returned:
baselineId is an opaque token, so its value can differ from the one the WASM package returned for the same data.
Every method still runs synchronously on the calling thread, as it did on the WASM package. To keep long leveling or evaluate runs off the UI thread, see Run in a worker.