Skip to content

Redesign the VTK.wasm JavaScript API + docs overhaul#45

Open
jspanchu wants to merge 6 commits into
mainfrom
feat/js-loader-session-api
Open

Redesign the VTK.wasm JavaScript API + docs overhaul#45
jspanchu wants to merge 6 commits into
mainfrom
feat/js-loader-session-api

Conversation

@jspanchu

@jspanchu jspanchu commented Jun 13, 2026

Copy link
Copy Markdown
Member

Summary

Replaces the scattered, confusingly-named loader/session API with a single, lean entry point and adds clean teardown. Also drops legacy (<9.5) support, regenerates the docs around the new API, and wires up an auto-generated API reference.

⚠️ Breaking change — see Migration below.

Motivation

The previous API had three pain points:

  1. Namespace creation was scattered across createNamespace() (free function), VtkWASMLoader.createNamespace(), and VtkWASMLoader.createStandaloneSession() — no canonical path.
  2. Confusing names — the JS RemoteSession class shadowed the C++ vtkRemoteSession it wrapped (exposed as .sceneManager), and load() meant three different things.
  3. No clean teardown — C++ session objects were never deleted, and the remote path leaked a DOM container + canvas listeners.

What changed

Core API

  • loadVtkWasm(options) → cached VtkWasmRuntime, the single factory for sessions.
  • runtime.createStandaloneSession() / runtime.createRemoteSession(); the C++ handle is private (#native), and session.vtk is the one way to the namespace.
  • Uniform dispose() / [Symbol.dispose] on runtime and both sessions for real teardown (using works).
  • RemoteSession now binds to user-provided canvas ids (bindCanvas) instead of a hard-coded internal selector, and never creates/removes canvas elements.
  • Legacy <9.5 support removed: sessionFactory, stateDecorators, and the dual-path branches in proxy/scriptLoader/remote are gone.
flowchart TD
    ST["HTML script tag (global vtkWASM)"]
    BD["Bundler import"]
    AN["Annotation script tag"]
    ST --> LF["loadVtkWasm(options)"]
    BD --> LF
    LF --> RT["VtkWasmRuntime — cached per url + config"]
    RT -->|createStandaloneSession| SS["StandaloneSession"]
    RT -->|createRemoteSession| RS["RemoteSession"]
    SS --> NS["session.vtk: create & render objects"]
    RS --> RW["bindNetwork + bindCanvas + update"]
    AN -.->|auto: loads + standalone session| NS
    NS --> DS["session.dispose()"]
    RW --> DS
    RT --> DR["runtime.dispose()"]
Loading

Packaging — exports collapse to . (index) + ./viewer; vite builds from src/index.js.

Docs

  • New conceptual guides: Loading VTK.wasm (with the path diagram above), Standalone Session, Remote Session — each links into the generated reference rather than restating signatures.
  • HTML Script Tag + Bundler Integration merged into Adding VTK.wasm to a Project; JS "Getting started" page removed; sidebar reordered.
  • Auto-generated API reference from source JSDoc (TypeDoc + markdown/vitepress theme), wired into the nav/sidebar (expanded by default); mermaid enabled.
  • README trimmed to onboarding essentials.

Migration

Before After
new VtkWASMLoader(); await l.load(url, cfg, name) await loadVtkWasm({ url, ...cfg, wasmBaseName: name })
createNamespace(url, cfg) (await loadVtkWasm({ url, ...cfg })).createStandaloneSession().vtk
new RemoteSession(); await r.load(url, cfg) (await loadVtkWasm({ url, ...cfg })).createRemoteSession()
remote.sceneManager.xxx() remote.vtk / remote methods (#native is private)
import … from "@kitware/vtk-wasm/vtk" / /remote import { loadVtkWasm } from "@kitware/vtk-wasm"
(none) session.dispose(), runtime.dispose(), using

Testing

  • npm run build — ESM + both UMD bundles build clean.
  • npm run lint — passes.
  • npm run docs:build — builds with no dead links (only the pre-existing mermaid chunk-size advisory).

jspanchu added 6 commits June 13, 2026 13:16
Replace the scattered VtkWASMLoader/createNamespace entry points with a
single loadVtkWasm() that returns a cached VtkWasmRuntime, the factory for
StandaloneSession and RemoteSession. Both sessions wrap their C++ handle as
a private #native and expose dispose()/[Symbol.dispose] for clean teardown.

- runtime.js: loadVtkWasm + VtkWasmRuntime (module cache, session factories)
- standaloneSession.js / remoteSession.js: session wrappers with lifecycle
- remoteSession: rely on user-provided canvas ids (bindCanvas) instead of a
  hard-coded internal selector; never create/remove canvas elements
- index.js: public exports + <script id="vtk-wasm"> bootstrap
- drop legacy (<9.5) support: sessionFactory, stateDecorators, dual-path
  branches in proxy/scriptLoader, and the old remote.js shims
- vite: build from src/index.js (esm "index" + umd "vtk")

BREAKING CHANGE: VtkWASMLoader, createNamespace, and the standalone/remote
entry modules are removed in favor of loadVtkWasm() and runtime.create*Session().
Update the simple-app example and the plain-javascript demos to use
loadVtkWasm({ url }).createStandaloneSession().vtk instead of the removed
VtkWASMLoader/createNamespace API.
- package.json: collapse exports to the new "." (index) + "./viewer" entry
  points; add docs:api script (run before docs:dev/docs:build)
- add TypeDoc (+ markdown/vitepress theme) and mermaid dev deps
- typedoc.json + tsconfig.json: generate the API reference from source JSDoc
  into docs/api (allowJs, no TS migration)
- gitignore the generated docs/api output
- new conceptual guides: Loading VTK.wasm (with a mermaid path diagram),
  Standalone Session, and Remote Session, each linking into the generated
  API reference rather than restating signatures
- merge HTML Script Tag + Bundler Integration into "Adding VTK.wasm to a
  Project"; remove the JS Getting started page
- reorder the "For JavaScript developers" sidebar and repoint inbound links
- wire the TypeDoc-generated API reference into the nav/sidebar (expanded by
  default) and enable mermaid via vitepress-plugin-mermaid
The C++ vtkRemoteSession has no create/destroy, so creating or deleting
objects through a remote session's vtk proxy would throw. Gate both on
typeof checks: warn and return undefined/false instead. Document that a
remote session can only control server-owned objects via getVtkObject.
@jspanchu jspanchu changed the title Feat/js loader session api Redesign the VTK.wasm JavaScript API + docs overhaul Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant