Skip to content

Latest commit

 

History

History
124 lines (104 loc) · 6.94 KB

REACT.md

File metadata and controls

124 lines (104 loc) · 6.94 KB

React stores and C++ islands

@webfront/react is an optional package, installed by wf from an npm-compatible registry. Its sources and declarations live in js/react; it is not an embedded core resource. React remains a peer dependency, shared with the application. @types/react is an optional peer for TypeScript projects; install it explicitly alongside TypeScript. No react-reconciler is used. The peer ranges are React ^19.3.1 and optional @types/react ^19.3.0; CI tests React 19.3.1 and @types/react 19.3.0. useSyncExternalStore reads cached snapshots; development React 19.3.1 is exercised under StrictMode. This lower bound is a support/qualification policy based on that CI baseline, not a technical requirement of the hooks: the hooks used by the adapter exist in React 18, but React 18 and 19.0–19.2 have not been qualified here.

Store

createStore is that of @webfront/store, which @webfront/react brings with it and re-exports; useStore reads one through useSyncExternalStore:

import {createStore, useStore} from '@webfront/react';
const statistics = createStore<[number, string]>('statistics', [0, 'Waiting']);
function Statistics() {
    const [count, label] = useStore(statistics);
    return <p>{count}: {label}</p>;
}

Delivery, coalescing, lifetime and errors are those of the store. webfront::react::Store, from <webfront/components/react/Store.hpp>, is the former name of webfront::store::Store. useStore subscribes again if a disposed store's consumer renders: unmount all consumers before dispose().

Island

Load the optional server interpreter and pass it to an empty component:

import {Island} from '@webfront/react';
async function prepareDom() {
    const interpreterUrl = '/webfront-dom.mjs';
    const {installDomInterpreter} = await import(interpreterUrl);
    return installDomInterpreter(webFront);
}
// After prepareDom() resolves, render inside your existing React tree:
// <Island dom={dom} name="panel" className="panel" />

Island renders a div without children. Its layout effect lends that element; after the React commit, await dom.connect() and ask a C++ callback to build ui.document().island("panel"). Never send mutations before the corresponding island is committed. children and dangerouslySetInnerHTML are rejected at runtime and excluded from its TypeScript props. Keep name and dom stable for the mounted component. Each island name is unique within its interpreter.

StrictMode's immediate effect cleanup/setup replay keeps the same loan and C++ children. Release is deferred to a microtask; a real unmount revokes the loan. Unexpected synchronous release errors are caught and logged, and the adapter drops its local lease. Such an error still requires application-level recovery/reload; logging it does not acknowledge a C++ revocation. A bridge already closed rejects the interpreter's revocation Promise, which the interpreter handles and logs. Parent renders preserve the C++ subtree. A later real mount gets fresh node IDs. Delayed effect disconnect/reconnect mechanisms such as React Activity or Offscreen are unsupported for a live Island: hiding revokes its loan, and revealing preserved DOM does not restore it. Coordinate a full unmount, fresh empty mount and C++ rebuild instead. Unmount still requires the document handoff: pause all document writers, flush, await a browser round trip, then unmount and await an application-level acknowledgement that C++ observed the revocation before resuming. The release function itself returns no acknowledgement or Promise. The component cannot pause application writers itself. An uncoordinated unmount can close the whole document.

Game of Life variant

examples/life/react/ keeps the simulation in C++, renders the grid and statistics with React, and builds a heading, status and “Next generation” button in C++ inside life-panel. The C++ event computes a generation, updates the island and sets a tuple snapshot containing the packed grid, generation and population. Each browser owns a separate simulation, released on disconnect. The original Life starter remains available.

Build the WebFrontLifeReact CMake target. The application only creates an empty project directory when it is missing (without argument ./life-project); it does not copy the example's sources. Use a new directory, so that no existing project is touched: mkdir fails if it exists and cp -n never overwrites a file. Copy main.tsx, Life.tsx and webfront.project.json from examples/life/react/ into that directory first, then start the application:

mkdir life-react-project
cp -n examples/life/react/main.tsx examples/life/react/Life.tsx examples/life/react/webfront.project.json life-react-project/
./build/examples/WebFrontLifeReact life-react-project

In its terminal, run wf install, then wf preview (or wf dev: editing the rendering in Life.tsx then keeps the grid, the generation and the connection, see hot replacement; main.tsx only starts the page, so a change of it reloads). The configured registry must carry @webfront/react@0.2.0 and its dependency @webfront/store@0.1.0; this change prepares the archive but does not publish to npm. The repository's test registry already carries the package. For a local evaluation, configure the driver with --registry-dir <build>/test/web/self-build-project/build-registry; the browser suite uses that registry for this adapter and React. To use the same registry in WebFrontLifeReact, copy its contents to <project>/registry/ and add "registry": "/registry/" to the project manifest before wf install. This is a development mirror served from the project document root.

To prepare the archive, use the repository wf project after restoring its packages:

wf run react-package
wf run store-package

This writes build-react-package/webfront-react-0.2.0.tgz, build-store-package/webfront-store-0.1.0.tgz and the integrity.txt of each. Publishing that archive to a registry is a separate maintainer action.

The test registry uses the same deterministic packagers. ReactAdapterPackageContents verifies byte-for-byte archive equality, the exact file list, every maintained source file and the SHA-512 in integrity.txt. The Life canvas ignores clicks until life.react.start acknowledges startup. If the React island does not mount within five seconds, startup fails visibly. Editing a cell starts a new simulation at generation zero.

The generated .tgz uses valid gzip stored blocks, without size compression, so bootstrap packaging needs no preinstalled compressor. Registry publication is a separate step and may retain this format or recompress with a newly recorded digest. The canvas cell editor is pointer-only; the C++ “Next generation” button supports normal keyboard activation. A complete keyboard cell editor is not demonstrated.