@runic-artifex/react 0.6.0-preview.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Runic Artifex contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # `@runic-artifex/react`
2
+
3
+ React hook for clients generated by Runic Views. The generated modules import
4
+ `@runic-artifex/views`, so install both:
5
+
6
+ ```sh
7
+ npm install @runic-artifex/react@preview @runic-artifex/views@preview
8
+ pnpm add @runic-artifex/react@preview @runic-artifex/views@preview
9
+ bun add @runic-artifex/react@preview @runic-artifex/views@preview
10
+ ```
11
+
12
+ Previews are published under the `preview` dist-tag. Install the versions that
13
+ match your Runic SDK packages. React 18.3 or later is supported.
14
+
15
+ ```tsx
16
+ import { useView } from "@runic-artifex/react";
17
+ import { connectWorkspace } from "./generated/workspace.js";
18
+ import type { CounterPageReference } from "./generated/counter.js";
19
+
20
+ const workspace = { connect: connectWorkspace };
21
+
22
+ export function App() {
23
+ const { state, client, error } = useView(workspace);
24
+ if (error) return <p role="alert">{String(error)}</p>;
25
+ if (!state || !client) return <p>Connecting…</p>;
26
+ return <button onClick={() => void client.showCounter()}>Counter</button>;
27
+ }
28
+
29
+ export function CounterPage({ page }: { page: CounterPageReference }) {
30
+ const { state, client } = useView(page);
31
+ return <button disabled={!client} onClick={() => void client?.increment()}>{state?.count ?? "…"}</button>;
32
+ }
33
+ ```
34
+
35
+ `useView(source)` returns `{ state, client, error, retry }`:
36
+
37
+ - A generated page reference, or `{ connect: connect<Name> }` for a root
38
+ ViewModel, is connected while the component is mounted and disposed when it
39
+ unmounts or the reference changes. A reference is identified by its `connect`
40
+ function, so an inline `{ connect: connectWorkspace }` does not reconnect.
41
+ - An already connected client is observed and left to its owner.
42
+ - `state` renders through `useSyncExternalStore`, so concurrent rendering sees
43
+ one consistent snapshot. StrictMode's extra mount disposes its connection.
44
+ - `error` holds a failed connection; `retry()` connects again.
@@ -0,0 +1,36 @@
1
+ /** The framework-neutral surface of a generated `<Name>Client`. */
2
+ export interface ViewClient<TState = unknown> {
3
+ readonly snapshot: TState;
4
+ subscribe(listener: (state: TState) => void): () => void;
5
+ dispose(): void;
6
+ }
7
+ /** A generated content reference, or `{ connect: connect<Name> }` for a root ViewModel. */
8
+ export interface ViewReference<TClient extends ViewClient = ViewClient> {
9
+ connect(): Promise<TClient>;
10
+ }
11
+ /**
12
+ * A reference is connected and disposed by the hook. An already connected
13
+ * client is only observed; its owner disposes it.
14
+ */
15
+ export type ViewSource<TClient extends ViewClient> = ViewReference<TClient> | TClient | null | undefined;
16
+ export interface ViewHandle<TClient extends ViewClient> {
17
+ /** The latest state, or undefined until the client connects. */
18
+ readonly state: TClient["snapshot"] | undefined;
19
+ /** The connected client for commands, or undefined until it connects. */
20
+ readonly client: TClient | undefined;
21
+ /** The connection failure, if any. */
22
+ readonly error: unknown;
23
+ /** Connects a reference again after a failure. */
24
+ retry(): void;
25
+ }
26
+ /**
27
+ * Connects a generated View reference for the lifetime of the component and
28
+ * renders its state through `useSyncExternalStore`. A changed reference
29
+ * disposes the previous client; StrictMode's extra mount disposes its
30
+ * connection when it resolves.
31
+ *
32
+ * A reference is identified by its `connect` function, so an inline
33
+ * `{ connect: connectWorkspace }` does not reconnect on every render.
34
+ */
35
+ export declare function useView<TClient extends ViewClient>(source: ViewSource<TClient>): ViewHandle<TClient>;
36
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,mEAAmE;AACnE,MAAM,WAAW,UAAU,CAAC,MAAM,GAAG,OAAO;IAC1C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IACzD,OAAO,IAAI,IAAI,CAAC;CACjB;AAED,2FAA2F;AAC3F,MAAM,WAAW,aAAa,CAAC,OAAO,SAAS,UAAU,GAAG,UAAU;IACpE,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC7B;AAED;;;GAGG;AACH,MAAM,MAAM,UAAU,CAAC,OAAO,SAAS,UAAU,IAAI,aAAa,CAAC,OAAO,CAAC,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAEzG,MAAM,WAAW,UAAU,CAAC,OAAO,SAAS,UAAU;IACpD,gEAAgE;IAChE,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,SAAS,CAAC;IAChD,yEAAyE;IACzE,QAAQ,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,CAAC;IACrC,sCAAsC;IACtC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,kDAAkD;IAClD,KAAK,IAAI,IAAI,CAAC;CACf;AAMD;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,OAAO,SAAS,UAAU,EAAE,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,CAoCpG"}
package/dist/index.js ADDED
@@ -0,0 +1,50 @@
1
+ import { useCallback, useEffect, useState, useSyncExternalStore } from "react";
2
+ function isClient(source) {
3
+ return typeof source.subscribe === "function";
4
+ }
5
+ /**
6
+ * Connects a generated View reference for the lifetime of the component and
7
+ * renders its state through `useSyncExternalStore`. A changed reference
8
+ * disposes the previous client; StrictMode's extra mount disposes its
9
+ * connection when it resolves.
10
+ *
11
+ * A reference is identified by its `connect` function, so an inline
12
+ * `{ connect: connectWorkspace }` does not reconnect on every render.
13
+ */
14
+ export function useView(source) {
15
+ const observed = source && isClient(source) ? source : undefined;
16
+ const reference = source && !isClient(source) ? source : undefined;
17
+ const connect = reference?.connect;
18
+ const [attempt, setAttempt] = useState(0);
19
+ const [connection, setConnection] = useState();
20
+ useEffect(() => {
21
+ if (!reference)
22
+ return;
23
+ let active = true;
24
+ let connected;
25
+ reference.connect().then(client => {
26
+ if (!active) {
27
+ client.dispose();
28
+ return;
29
+ }
30
+ connected = client;
31
+ setConnection({ connect, attempt, client });
32
+ }, error => {
33
+ if (active)
34
+ setConnection({ connect, attempt, error });
35
+ });
36
+ return () => {
37
+ active = false;
38
+ connected?.dispose();
39
+ };
40
+ // An inline reference object is recreated on each render; its connect function identifies it.
41
+ }, [connect, attempt]);
42
+ const current = connection && connection.connect === connect && connection.attempt === attempt ? connection : undefined;
43
+ const client = observed ?? current?.client;
44
+ const subscribe = useCallback((onChange) => client ? client.subscribe(() => onChange()) : () => { }, [client]);
45
+ const snapshot = useCallback(() => client?.snapshot, [client]);
46
+ const state = useSyncExternalStore(subscribe, snapshot, snapshot);
47
+ const retry = useCallback(() => setAttempt(value => value + 1), []);
48
+ return { state, client, error: observed ? undefined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ChF,CAAC"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@runic-artifex/react",
3
+ "version": "0.6.0-preview.1",
4
+ "description": "React hooks for generated Runic Views clients",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "packageManager": "bun@1.4.2",
8
+ "sideEffects": false,
9
+ "files": [
10
+ "dist",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "scripts": {
24
+ "clean": "bun -e \"import('node:fs').then(({rmSync}) => rmSync('dist', { recursive: true, force: true }))\"",
25
+ "build": "bun run --bun clean && tsc -p tsconfig.build.json",
26
+ "check": "tsc -p tsconfig.json --noEmit",
27
+ "test": "bun run --bun build && vitest run",
28
+ "pack:check": "bun pm pack --dry-run"
29
+ },
30
+ "peerDependencies": {
31
+ "react": ">=18.3.0 <20"
32
+ },
33
+ "devDependencies": {
34
+ "@types/react": "19.3.0",
35
+ "@types/react-dom": "19.3.0",
36
+ "happy-dom": "20.14.5",
37
+ "react": "19.3.0",
38
+ "react-dom": "19.3.0",
39
+ "typescript": "6.0.3",
40
+ "vitest": "5.0.3"
41
+ },
42
+ "engines": {
43
+ "node": ">=24.18.0 <25"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public",
47
+ "tag": "preview"
48
+ },
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "git+https://github.com/Runic-Artifex/runic-sdk.git",
52
+ "directory": "packages/web/react"
53
+ },
54
+ "homepage": "https://github.com/Runic-Artifex/runic-sdk/tree/main/packages/web/react#readme",
55
+ "bugs": {
56
+ "url": "https://github.com/Runic-Artifex/runic-sdk/issues"
57
+ },
58
+ "keywords": [
59
+ "runic-artifex",
60
+ "runic",
61
+ "views",
62
+ "react"
63
+ ],
64
+ "gitHead": "3026ada4e531cf58ba5081d8ee53b32b60f1ab6a"
65
+ }