mirror of
https://github.com/vercel-labs/json-render.git
synced 2026-10-03 04:18:15 +08:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57b2fa7add | ||
|
|
6bcaaad57d |
@@ -117,6 +117,7 @@ function Dashboard({ spec }) {
|
||||
| `@json-render/redux` | Redux / Redux Toolkit adapter for `StateStore` |
|
||||
| `@json-render/zustand` | Zustand adapter for `StateStore` |
|
||||
| `@json-render/jotai` | Jotai adapter for `StateStore` |
|
||||
| `@json-render/xstate-store` | XState Store (atom) adapter for `StateStore` |
|
||||
|
||||
## Renderers
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# @json-render/xstate-store
|
||||
|
||||
[XState Store](https://stately.ai/docs/xstate-store) adapter for json-render's `StateStore` interface. Wire an `@xstate/store` atom as the state backend for json-render.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install @json-render/xstate-store @json-render/core @json-render/react @xstate/store
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> This adapter requires `@xstate/store` v3+.
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { createAtom } from "@xstate/store";
|
||||
import { xstateStoreStateStore } from "@json-render/xstate-store";
|
||||
import { StateProvider } from "@json-render/react";
|
||||
|
||||
// 1. Create an atom
|
||||
const uiAtom = createAtom({ count: 0 });
|
||||
|
||||
// 2. Create the json-render StateStore adapter
|
||||
const store = xstateStoreStateStore({ atom: uiAtom });
|
||||
|
||||
// 3. Use it
|
||||
<StateProvider store={store}>
|
||||
{/* json-render reads/writes go through @xstate/store */}
|
||||
</StateProvider>
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `xstateStoreStateStore(options)`
|
||||
|
||||
Creates a `StateStore` backed by an `@xstate/store` atom.
|
||||
|
||||
#### Options
|
||||
|
||||
| Option | Type | Required | Description |
|
||||
|--------|------|----------|-------------|
|
||||
| `atom` | `Atom<StateModel>` | Yes | An `@xstate/store` atom (from `createAtom`) holding the json-render state model |
|
||||
@@ -0,0 +1,55 @@
|
||||
{
|
||||
"name": "@json-render/xstate-store",
|
||||
"version": "0.0.0",
|
||||
"license": "Apache-2.0",
|
||||
"description": "XState Store adapter for json-render StateStore",
|
||||
"keywords": [
|
||||
"json-render",
|
||||
"xstate",
|
||||
"xstate-store",
|
||||
"state-management",
|
||||
"adapter"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/vercel-labs/json-render.git",
|
||||
"directory": "packages/xstate-store"
|
||||
},
|
||||
"homepage": "https://github.com/vercel-labs/json-render#readme",
|
||||
"bugs": {
|
||||
"url": "https://github.com/vercel-labs/json-render/issues"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsup",
|
||||
"dev": "tsup --watch",
|
||||
"check-types": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@json-render/core": "workspace:*"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@xstate/store": ">=3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@internal/typescript-config": "workspace:*",
|
||||
"tsup": "^8.0.2",
|
||||
"typescript": "^5.4.5",
|
||||
"@xstate/store": "^3.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { createAtom } from "@xstate/store";
|
||||
import { xstateStoreStateStore } from "./index";
|
||||
|
||||
function createTestStore(initial: Record<string, unknown> = {}) {
|
||||
const atom = createAtom<Record<string, unknown>>(initial);
|
||||
const store = xstateStoreStateStore({ atom });
|
||||
return { atom, store };
|
||||
}
|
||||
|
||||
describe("xstateStoreStateStore", () => {
|
||||
it("get/set round-trip", () => {
|
||||
const { store } = createTestStore({ count: 0 });
|
||||
|
||||
expect(store.get("/count")).toBe(0);
|
||||
|
||||
store.set("/count", 42);
|
||||
|
||||
expect(store.get("/count")).toBe(42);
|
||||
expect(store.getSnapshot().count).toBe(42);
|
||||
});
|
||||
|
||||
it("update round-trip with multiple values", () => {
|
||||
const { store } = createTestStore({});
|
||||
|
||||
store.update({ "/a": 1, "/b": "hello" });
|
||||
|
||||
expect(store.get("/a")).toBe(1);
|
||||
expect(store.get("/b")).toBe("hello");
|
||||
expect(store.getSnapshot()).toEqual({ a: 1, b: "hello" });
|
||||
});
|
||||
|
||||
it("subscribe fires on set", () => {
|
||||
const { store } = createTestStore({});
|
||||
const listener = vi.fn();
|
||||
store.subscribe(listener);
|
||||
|
||||
store.set("/x", 1);
|
||||
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("subscribe fires on update", () => {
|
||||
const { store } = createTestStore({});
|
||||
const listener = vi.fn();
|
||||
store.subscribe(listener);
|
||||
|
||||
store.update({ "/a": 1, "/b": 2 });
|
||||
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("unsubscribe stops notifications", () => {
|
||||
const { store } = createTestStore({});
|
||||
const listener = vi.fn();
|
||||
const unsub = store.subscribe(listener);
|
||||
|
||||
store.set("/x", 1);
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
|
||||
unsub();
|
||||
store.set("/x", 2);
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("getSnapshot immutability -- previous snapshot is not mutated", () => {
|
||||
const { store } = createTestStore({
|
||||
user: { name: "Alice", age: 30 },
|
||||
});
|
||||
const snap1 = store.getSnapshot();
|
||||
|
||||
store.set("/user/name", "Bob");
|
||||
const snap2 = store.getSnapshot();
|
||||
|
||||
expect(snap1.user).toEqual({ name: "Alice", age: 30 });
|
||||
expect((snap2.user as Record<string, unknown>).name).toBe("Bob");
|
||||
expect(snap1.user).not.toBe(snap2.user);
|
||||
});
|
||||
|
||||
it("structural sharing -- untouched branches keep references", () => {
|
||||
const { store } = createTestStore({
|
||||
a: { x: 1 },
|
||||
b: { y: 2 },
|
||||
});
|
||||
const snap1 = store.getSnapshot();
|
||||
|
||||
store.set("/a/x", 99);
|
||||
const snap2 = store.getSnapshot();
|
||||
|
||||
expect(snap2.b).toBe(snap1.b);
|
||||
expect(snap2.a).not.toBe(snap1.a);
|
||||
});
|
||||
|
||||
it("getServerSnapshot returns same as getSnapshot", () => {
|
||||
const { store } = createTestStore({ x: 1 });
|
||||
|
||||
expect(store.getServerSnapshot!()).toBe(store.getSnapshot());
|
||||
|
||||
store.set("/x", 2);
|
||||
expect(store.getServerSnapshot!()).toBe(store.getSnapshot());
|
||||
});
|
||||
|
||||
it("set skips update when value is unchanged", () => {
|
||||
const { store } = createTestStore({ x: 1 });
|
||||
const snap1 = store.getSnapshot();
|
||||
const listener = vi.fn();
|
||||
store.subscribe(listener);
|
||||
|
||||
store.set("/x", 1);
|
||||
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
expect(store.getSnapshot()).toBe(snap1);
|
||||
});
|
||||
|
||||
it("update skips update when no values changed", () => {
|
||||
const { store } = createTestStore({ a: 1, b: 2 });
|
||||
const snap1 = store.getSnapshot();
|
||||
const listener = vi.fn();
|
||||
store.subscribe(listener);
|
||||
|
||||
store.update({ "/a": 1, "/b": 2 });
|
||||
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
expect(store.getSnapshot()).toBe(snap1);
|
||||
});
|
||||
|
||||
it("reads from the shared atom", () => {
|
||||
const atom = createAtom<Record<string, unknown>>({ count: 0 });
|
||||
const store = xstateStoreStateStore({ atom });
|
||||
|
||||
atom.set({ count: 99 });
|
||||
|
||||
expect(store.get("/count")).toBe(99);
|
||||
expect(store.getSnapshot().count).toBe(99);
|
||||
});
|
||||
|
||||
it("subscribe fires on external atom.set", () => {
|
||||
const atom = createAtom<Record<string, unknown>>({ count: 0 });
|
||||
const store = xstateStoreStateStore({ atom });
|
||||
const listener = vi.fn();
|
||||
store.subscribe(listener);
|
||||
|
||||
atom.set({ count: 99 });
|
||||
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
expect(store.get("/count")).toBe(99);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { StateModel, StateStore } from "@json-render/core";
|
||||
import { createStoreAdapter } from "@json-render/core/store-utils";
|
||||
import type { Atom } from "@xstate/store";
|
||||
|
||||
export type { StateStore } from "@json-render/core";
|
||||
|
||||
/**
|
||||
* Options for {@link xstateStoreStateStore}.
|
||||
*/
|
||||
export interface XstateStoreStateStoreOptions {
|
||||
/** An `@xstate/store` atom (created with `createAtom`). */
|
||||
atom: Atom<StateModel>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link StateStore} backed by an `@xstate/store` atom.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { createAtom } from "@xstate/store";
|
||||
* import { xstateStoreStateStore } from "@json-render/xstate-store";
|
||||
*
|
||||
* const uiAtom = createAtom<Record<string, unknown>>({ count: 0 });
|
||||
*
|
||||
* const store = xstateStoreStateStore({ atom: uiAtom });
|
||||
*
|
||||
* <StateProvider store={store}>...</StateProvider>
|
||||
* ```
|
||||
*/
|
||||
export function xstateStoreStateStore(
|
||||
options: XstateStoreStateStoreOptions,
|
||||
): StateStore {
|
||||
const { atom } = options;
|
||||
|
||||
return createStoreAdapter({
|
||||
getSnapshot: () => atom.get(),
|
||||
setSnapshot: (next) => atom.set(next),
|
||||
subscribe(listener) {
|
||||
const sub = atom.subscribe(() => {
|
||||
listener();
|
||||
});
|
||||
return () => sub.unsubscribe();
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "@internal/typescript-config/base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"rootDir": "src"
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { defineConfig } from "tsup";
|
||||
|
||||
export default defineConfig({
|
||||
entry: ["src/index.ts"],
|
||||
format: ["cjs", "esm"],
|
||||
dts: true,
|
||||
sourcemap: true,
|
||||
clean: true,
|
||||
external: [
|
||||
"@json-render/core",
|
||||
"@json-render/core/store-utils",
|
||||
"@xstate/store",
|
||||
],
|
||||
});
|
||||
Generated
+38
-35
@@ -448,7 +448,7 @@ importers:
|
||||
version: 0.563.0(react@19.2.4)
|
||||
next:
|
||||
specifier: 16.1.6
|
||||
version: 16.1.6(@opentelemetry/api@1.9.0)(babel-plugin-react-compiler@1.0.0)(react-dom@19.2.4(react@19.2.4))(react@19.2.4)
|
||||
version: 16.1.6(@babel/core@7.29.0)(@opentelemetry/api@1.9.0)(babel-plugin-react-compiler@1.0.0)(react-dom@19.2.4(react@19.2.4))(react@19.2.4)
|
||||
radix-ui:
|
||||
specifier: ^1.4.3
|
||||
version: 1.4.3(@types/react-dom@19.2.3(@types/react@19.2.3))(@types/react@19.2.3)(react-dom@19.2.4(react@19.2.4))(react@19.2.4)
|
||||
@@ -722,7 +722,7 @@ importers:
|
||||
version: 6.0.91(zod@4.3.5)
|
||||
next:
|
||||
specifier: ^16.1.6
|
||||
version: 16.1.6(@opentelemetry/api@1.9.0)(babel-plugin-react-compiler@1.0.0)(react-dom@19.2.4(react@19.2.4))(react@19.2.4)
|
||||
version: 16.1.6(@babel/core@7.29.0)(@opentelemetry/api@1.9.0)(babel-plugin-react-compiler@1.0.0)(react-dom@19.2.4(react@19.2.4))(react@19.2.4)
|
||||
react:
|
||||
specifier: ^19.1.0
|
||||
version: 19.2.4
|
||||
@@ -1136,6 +1136,25 @@ importers:
|
||||
specifier: 5.9.2
|
||||
version: 5.9.2
|
||||
|
||||
packages/xstate-store:
|
||||
dependencies:
|
||||
'@json-render/core':
|
||||
specifier: workspace:*
|
||||
version: link:../core
|
||||
devDependencies:
|
||||
'@internal/typescript-config':
|
||||
specifier: workspace:*
|
||||
version: link:../typescript-config
|
||||
'@xstate/store':
|
||||
specifier: ^3.0.0
|
||||
version: 3.15.0(react@19.2.4)
|
||||
tsup:
|
||||
specifier: ^8.0.2
|
||||
version: 8.5.1(jiti@2.6.1)(postcss@8.5.6)(tsx@4.21.0)(typescript@5.9.2)(yaml@2.8.2)
|
||||
typescript:
|
||||
specifier: ^5.4.5
|
||||
version: 5.9.2
|
||||
|
||||
packages/zustand:
|
||||
dependencies:
|
||||
'@json-render/core':
|
||||
@@ -5278,6 +5297,17 @@ packages:
|
||||
resolution: {integrity: sha512-cQzWCtO6C8TQiYl1ruKNn2U6Ao4o4WBBcbL61yJl84x+j5sOWWFU9X7DpND8XZG3daDppSsigMdfAIl2upQBRw==}
|
||||
engines: {node: '>=10.0.0'}
|
||||
|
||||
'@xstate/store@3.15.0':
|
||||
resolution: {integrity: sha512-1V4/5D55zvJz3inEbAH+pqrAy2xhb2d8OKC1fZcBwWmr51VpxTOCNgf9ma4fa/seUfpQPjRf9wxBSyfcTKtUbQ==}
|
||||
peerDependencies:
|
||||
react: ^18.2.0 || ^19.0.0
|
||||
solid-js: ^1.7.6
|
||||
peerDependenciesMeta:
|
||||
react:
|
||||
optional: true
|
||||
solid-js:
|
||||
optional: true
|
||||
|
||||
'@xtuc/ieee754@1.2.0':
|
||||
resolution: {integrity: sha512-DX8nKgqcGwsc0eJSqYt5lwP4DH5FlHnmuWWBRy7X0NcaGR0ZtuyeESgMwTYVEtxmsNGY+qit4QYT/MIYTOTPeA==}
|
||||
|
||||
@@ -16231,7 +16261,7 @@ snapshots:
|
||||
'@stripe/ui-extension-tools@0.0.1(@babel/core@7.29.0)(babel-jest@27.5.1(@babel/core@7.29.0))':
|
||||
dependencies:
|
||||
'@types/jest': 28.1.8
|
||||
'@typescript-eslint/eslint-plugin': 5.62.0(@typescript-eslint/parser@5.62.0(eslint@9.39.2(jiti@2.6.1))(typescript@4.9.5))(eslint@8.57.1)(typescript@4.9.5)
|
||||
'@typescript-eslint/eslint-plugin': 5.62.0(@typescript-eslint/parser@5.62.0(eslint@8.57.1)(typescript@4.9.5))(eslint@8.57.1)(typescript@4.9.5)
|
||||
'@typescript-eslint/parser': 5.62.0(eslint@8.57.1)(typescript@4.9.5)
|
||||
eslint: 8.57.1
|
||||
eslint-plugin-react: 7.37.5(eslint@8.57.1)
|
||||
@@ -16578,7 +16608,7 @@ snapshots:
|
||||
'@types/node': 22.19.6
|
||||
optional: true
|
||||
|
||||
'@typescript-eslint/eslint-plugin@5.62.0(@typescript-eslint/parser@5.62.0(eslint@9.39.2(jiti@2.6.1))(typescript@4.9.5))(eslint@8.57.1)(typescript@4.9.5)':
|
||||
'@typescript-eslint/eslint-plugin@5.62.0(@typescript-eslint/parser@5.62.0(eslint@8.57.1)(typescript@4.9.5))(eslint@8.57.1)(typescript@4.9.5)':
|
||||
dependencies:
|
||||
'@eslint-community/regexpp': 4.12.2
|
||||
'@typescript-eslint/parser': 5.62.0(eslint@8.57.1)(typescript@4.9.5)
|
||||
@@ -16926,6 +16956,10 @@ snapshots:
|
||||
|
||||
'@xmldom/xmldom@0.8.11': {}
|
||||
|
||||
'@xstate/store@3.15.0(react@19.2.4)':
|
||||
optionalDependencies:
|
||||
react: 19.2.4
|
||||
|
||||
'@xtuc/ieee754@1.2.0': {}
|
||||
|
||||
'@xtuc/long@4.2.2': {}
|
||||
@@ -21547,32 +21581,6 @@ snapshots:
|
||||
- '@babel/core'
|
||||
- babel-plugin-macros
|
||||
|
||||
next@16.1.6(@opentelemetry/api@1.9.0)(babel-plugin-react-compiler@1.0.0)(react-dom@19.2.4(react@19.2.4))(react@19.2.4):
|
||||
dependencies:
|
||||
'@next/env': 16.1.6
|
||||
'@swc/helpers': 0.5.15
|
||||
baseline-browser-mapping: 2.9.14
|
||||
caniuse-lite: 1.0.30001764
|
||||
postcss: 8.4.31
|
||||
react: 19.2.4
|
||||
react-dom: 19.2.4(react@19.2.4)
|
||||
styled-jsx: 5.1.6(react@19.2.4)
|
||||
optionalDependencies:
|
||||
'@next/swc-darwin-arm64': 16.1.6
|
||||
'@next/swc-darwin-x64': 16.1.6
|
||||
'@next/swc-linux-arm64-gnu': 16.1.6
|
||||
'@next/swc-linux-arm64-musl': 16.1.6
|
||||
'@next/swc-linux-x64-gnu': 16.1.6
|
||||
'@next/swc-linux-x64-musl': 16.1.6
|
||||
'@next/swc-win32-arm64-msvc': 16.1.6
|
||||
'@next/swc-win32-x64-msvc': 16.1.6
|
||||
'@opentelemetry/api': 1.9.0
|
||||
babel-plugin-react-compiler: 1.0.0
|
||||
sharp: 0.34.5
|
||||
transitivePeerDependencies:
|
||||
- '@babel/core'
|
||||
- babel-plugin-macros
|
||||
|
||||
node-abi@3.87.0:
|
||||
dependencies:
|
||||
semver: 7.7.3
|
||||
@@ -23572,11 +23580,6 @@ snapshots:
|
||||
client-only: 0.0.1
|
||||
react: 19.2.3
|
||||
|
||||
styled-jsx@5.1.6(react@19.2.4):
|
||||
dependencies:
|
||||
client-only: 0.0.1
|
||||
react: 19.2.4
|
||||
|
||||
sucrase@3.35.1:
|
||||
dependencies:
|
||||
'@jridgewell/gen-mapping': 0.3.13
|
||||
|
||||
Reference in New Issue
Block a user