/ concept-collection / hitandrun-commonview
Sign in
concept-collection / hitandrun-commonview
hitandrun-commonview / README.md
3.5 KBPreviewCodeBlameHistoryRaw

hitandrun-commonview#

One interactive figure, one shared view. This app shows the hitandrun-interactive hit-and-run sampling figure — just the figure, no source code — and keeps it identical for everyone who has the page open, in the style of commonview: peers discover each other over nostr relays and form a WebRTC full mesh; the oldest peer is the central peer and owns the authoritative state.

The twist over commonview's counter: the shared state is a live MATLAB computation. The central peer — and only the central peer — runs numbl (a MATLAB-compatible runtime) in a web worker, executing the unmodified hitandrun_demo.m from hitandrun-interactive. The figure's uihtml bridge is intercepted host-side (no iframe): control changes from any viewer are forwarded to the central peer, which feeds them to the script and broadcasts the results — parameter selections, the region, and the samples — to every viewer.

What is shared#

Graceful failover#

Run#

npm install        # requires numbl >= 0.4.8 on npm (browser-embedding exports)
npm run dev

Open the printed URL in two tabs (identity is per page load, so every tab is its own peer). Drag the samples slider or press "New region" in either window and watch both update; close the central window and watch the other take over.

How the engine embedding works#

src/engine/numbl.worker.ts runs executeCode from the numbl npm package against an in-memory filesystem holding the .m files (verbatim copies from hitandrun-interactive, under matlab/). The script's uihtml(...) call surfaces as a plot instruction carrying the component id and initial Data; sendEventToHTMLSource calls surface via the onHtmlSourceEvent hook; and events from the app re-enter the still-live interpreter through the UihtmlSession returned by executeCode — firing the script's HTMLEventReceivedFcn exactly as if the figure page had sent them. The HTML the script loads for the figure is replaced by a one-line placeholder; nothing is ever rendered from the worker.

moveopenescclose