react-state-custom 1.0.24 → 1.0.25
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 +21 -0
- package/README.md +17 -1
- package/dist/index.es.js +282 -294
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/state-utils/createAutoCtx.d.ts +1 -1
- package/dist/state-utils/createRootCtx.d.ts +1 -0
- package/dist/state-utils/ctx.d.ts +1 -0
- package/package.json +18 -4
- package/.github/copilot-instructions.md +0 -57
- package/.github/workflows/deploy.yml +0 -56
- package/.vscode/extensions.json +0 -5
- package/.vscode/settings.json +0 -8
- package/API_DOCUMENTATION.md +0 -1012
- package/dist/dev.d.ts +0 -0
- package/dist/examples/Playground.d.ts +0 -1
- package/dist/examples/cart/app.d.ts +0 -1
- package/dist/examples/cart/index.d.ts +0 -3
- package/dist/examples/cart/state.d.ts +0 -23
- package/dist/examples/cart/view.d.ts +0 -4
- package/dist/examples/counter/app.d.ts +0 -1
- package/dist/examples/counter/index.d.ts +0 -2
- package/dist/examples/counter/state.d.ts +0 -6
- package/dist/examples/counter/view.d.ts +0 -2
- package/dist/examples/form/app.d.ts +0 -1
- package/dist/examples/form/index.d.ts +0 -3
- package/dist/examples/form/state.d.ts +0 -16
- package/dist/examples/form/view.d.ts +0 -4
- package/dist/examples/timer/app.d.ts +0 -1
- package/dist/examples/timer/index.d.ts +0 -2
- package/dist/examples/timer/state.d.ts +0 -11
- package/dist/examples/timer/view.d.ts +0 -4
- package/dist/examples/todo/app.d.ts +0 -1
- package/dist/examples/todo/index.d.ts +0 -3
- package/dist/examples/todo/state.d.ts +0 -17
- package/dist/examples/todo/view.d.ts +0 -4
- package/fix-vscode-yarn-pnp.sh +0 -26
- package/index.html +0 -14
- package/src/dev-tool/DataViewComponent.tsx +0 -17
- package/src/dev-tool/DevTool.css +0 -134
- package/src/dev-tool/DevTool.tsx +0 -20
- package/src/dev-tool/DevToolState.tsx +0 -78
- package/src/dev-tool/StateLabelRender.tsx +0 -38
- package/src/dev-tool/useHighlight.tsx +0 -56
- package/src/dev.tsx +0 -7
- package/src/examples/Playground.tsx +0 -180
- package/src/examples/cart/app.tsx +0 -16
- package/src/examples/cart/index.ts +0 -3
- package/src/examples/cart/state.ts +0 -67
- package/src/examples/cart/view.tsx +0 -62
- package/src/examples/counter/app.tsx +0 -14
- package/src/examples/counter/index.ts +0 -2
- package/src/examples/counter/state.ts +0 -22
- package/src/examples/counter/state.tsx?raw +0 -0
- package/src/examples/counter/view.tsx +0 -20
- package/src/examples/form/app.tsx +0 -16
- package/src/examples/form/index.ts +0 -3
- package/src/examples/form/state.ts +0 -58
- package/src/examples/form/view.tsx +0 -53
- package/src/examples/timer/app.tsx +0 -16
- package/src/examples/timer/index.ts +0 -2
- package/src/examples/timer/state.ts +0 -43
- package/src/examples/timer/view.tsx +0 -26
- package/src/examples/todo/app.tsx +0 -16
- package/src/examples/todo/index.ts +0 -3
- package/src/examples/todo/state.ts +0 -54
- package/src/examples/todo/view.tsx +0 -47
- package/src/index.ts +0 -22
- package/src/state-utils/createAutoCtx.tsx +0 -191
- package/src/state-utils/createRootCtx.tsx +0 -117
- package/src/state-utils/ctx.ts +0 -346
- package/src/state-utils/useArrayHash.ts +0 -53
- package/src/state-utils/useObjectHash.ts +0 -53
- package/src/state-utils/useQuickSubscribe.ts +0 -110
- package/src/state-utils/useRefValue.ts +0 -8
- package/src/state-utils/utils.ts +0 -43
- package/src/vite-env.d.ts +0 -6
- package/tsconfig.json +0 -27
- package/vite.config.dev.ts +0 -16
- package/vite.config.ts +0 -39
|
@@ -50,6 +50,6 @@ export declare const AutoRootCtx: ({ Wrapper }: {
|
|
|
50
50
|
* ```
|
|
51
51
|
* AutoRootCtx will subscribe/unsubscribe instances per unique params and render the appropriate Root under the hood.
|
|
52
52
|
*/
|
|
53
|
-
export declare const createAutoCtx: <U extends object, V extends object>({ Root,
|
|
53
|
+
export declare const createAutoCtx: <U extends object, V extends object>({ Root, resolveCtxName, name }: ReturnType<typeof createRootCtx<U, V>>, timeToClean?: number) => {
|
|
54
54
|
useCtxState: (e: U) => Context<V>;
|
|
55
55
|
};
|
|
@@ -29,6 +29,7 @@ import { Context } from './ctx';
|
|
|
29
29
|
* mount a single <AutoRootCtx Wrapper={ErrorBoundary} /> at the app root so you don't manually mount `Root`.
|
|
30
30
|
*/
|
|
31
31
|
export declare const createRootCtx: <U extends object, V extends object>(name: string, useFn: (e: U) => V) => {
|
|
32
|
+
name: string;
|
|
32
33
|
resolveCtxName: (e: U) => string;
|
|
33
34
|
Root: import('react').FC<U>;
|
|
34
35
|
/**
|
|
@@ -17,6 +17,7 @@ export declare class Context<D> extends EventTarget {
|
|
|
17
17
|
* Registry for tracking active keys (for duplicate detection).
|
|
18
18
|
*/
|
|
19
19
|
registry: Set<string>;
|
|
20
|
+
useCounter: number;
|
|
20
21
|
/**
|
|
21
22
|
* Publish a value to the context and notify subscribers if it changed.
|
|
22
23
|
* @param key - The key to update.
|
package/package.json
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-state-custom",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.25",
|
|
4
4
|
"description": "The `react-state-custom` library provides a powerful set of tools for managing shared state in React applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.umd.js",
|
|
7
7
|
"module": "dist/index.es.js",
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/index.d.ts",
|
|
11
|
+
"dist/index.es.js",
|
|
12
|
+
"dist/index.es.js.map",
|
|
13
|
+
"dist/index.umd.js",
|
|
14
|
+
"dist/index.umd.js.map",
|
|
15
|
+
"dist/react-state-custom.css",
|
|
16
|
+
"dist/state-utils",
|
|
17
|
+
"dist/dev-tool",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
9
21
|
"bugs": {
|
|
10
22
|
"url": "https://github.com/vothanhdat/react-state-custom/issues"
|
|
11
23
|
},
|
|
@@ -16,9 +28,11 @@
|
|
|
16
28
|
"scripts": {
|
|
17
29
|
"build": "vite build",
|
|
18
30
|
"build:demo": "vite build --config vite.config.dev.ts",
|
|
19
|
-
"dev": "vite
|
|
31
|
+
"dev": "vite",
|
|
32
|
+
"dev:playground": "vite --config vite.config.dev.ts",
|
|
20
33
|
"test": "echo \"No tests specified\" && exit 0",
|
|
21
|
-
"preview": "vite preview --config vite.config.dev.ts"
|
|
34
|
+
"preview": "vite preview --config vite.config.dev.ts",
|
|
35
|
+
"prepublishOnly": "yarn build"
|
|
22
36
|
},
|
|
23
37
|
"keywords": [
|
|
24
38
|
"frontend",
|
|
@@ -42,7 +56,7 @@
|
|
|
42
56
|
"react": "^19.2.0",
|
|
43
57
|
"react-dom": "^19.2.0",
|
|
44
58
|
"react-obj-view": "^1.0.4",
|
|
45
|
-
"react-state-custom": "^1.0.
|
|
59
|
+
"react-state-custom": "^1.0.24",
|
|
46
60
|
"typescript": "^5.8.3",
|
|
47
61
|
"vite": "7",
|
|
48
62
|
"vite-bundle-analyzer": "^1.1.0",
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
## Project Snapshot
|
|
2
|
-
- `react-state-custom` is a hook-first state management library; entrypoint `src/index.ts` re-exports context factories and subscription hooks.
|
|
3
|
-
- TypeScript only; consumers are expected to be React 19 apps (see `package.json` peerDependencies).
|
|
4
|
-
- Library build artifacts live in `dist/`; all source utilities reside under `src/state-utils/`.
|
|
5
|
-
- Live demo at https://vothanhdat.github.io/react-state-custom/ showcases interactive examples.
|
|
6
|
-
|
|
7
|
-
## Context Core
|
|
8
|
-
- `Context` (`src/state-utils/ctx.ts`) extends `EventTarget`; `data` stores the latest values and `publish` fires per-key DOM events when a loose `!=` comparison detects change.
|
|
9
|
-
- `getContext` memoizes by context name; `useDataContext(name)` memoizes the lookup via React `useMemo`.
|
|
10
|
-
- `useDataSource`/`useDataSourceMultiple` publish new values and register keys in `ctx.registry`; duplicate sources log via `useRegistryChecker`.
|
|
11
|
-
|
|
12
|
-
## Root Contexts
|
|
13
|
-
- `createRootCtx(name, useFn)` (`src/state-utils/createRootCtx.tsx`) wraps your hook (`useFn`) in a hidden `Root` component that pushes its result into a derived context namespace.
|
|
14
|
-
- Context names come from `name` plus sorted prop key/value pairs; keep props serializable and stable to avoid collisions.
|
|
15
|
-
- `ctxMountedCheck` blocks multiple `Root` instances for the same name; duplicates throw with the original call stack.
|
|
16
|
-
- Use `useCtxStateStrict` to hard-fail when the `Root` is missing, or `useCtxState` to surface a delayed console error instead.
|
|
17
|
-
|
|
18
|
-
## Auto Context Lifecycle
|
|
19
|
-
- `AutoRootCtx` (`src/state-utils/createAutoCtx.tsx`) maintains a global `'auto-ctx'` context exposing `subscribe(Root, params)` and reference counts for each instance.
|
|
20
|
-
- Mount `<AutoRootCtx Wrapper={YourErrorBoundary}>` once near the app root; it renders requested `Root` components inside the optional wrapper.
|
|
21
|
-
- `createAutoCtx(rootCtx, unmountTime?)` returns `useCtxState(params)` that subscribes through `'auto-ctx'` and hands back `useDataContext` for the resolved name.
|
|
22
|
-
- Multiple callers with identical params share a single `Root`; `unmountTime` delays teardown to absorb rapid mount/unmount cycles.
|
|
23
|
-
|
|
24
|
-
## Developer Tools
|
|
25
|
-
- `DevToolContainer` (`src/dev-tool/DevTool.tsx`) provides a debugging UI for inspecting all context data; mount it alongside `<AutoRootCtx />`.
|
|
26
|
-
- Accepts optional `Component` prop (type `DataViewComponent`) for custom data rendering; defaults to JSON stringify.
|
|
27
|
-
- Import styles with `import 'react-state-custom/dist/react-state-custom.css'` when using DevTool.
|
|
28
|
-
- Used in demo (`src/dev.tsx`, `src/examples/`) with `react-obj-view` for rich object inspection.
|
|
29
|
-
|
|
30
|
-
## Subscription Patterns
|
|
31
|
-
- `useDataSourceMultiple(ctx, ...entries)` expects stable `[key, value]` tuples; internal `useArrayHash` hashes length and reference equality only.
|
|
32
|
-
- Use `useDataSubscribe(ctx, key, debounceMs)` or `useDataSubscribeMultiple(ctx, ...keys)` for keyed reads; debounce variants batch updates when values bounce.
|
|
33
|
-
- `useDataSubscribeWithTransform` recomputes the transformed shape only on change; memoize the `transform` fn to avoid churn.
|
|
34
|
-
- `useQuickSubscribe(ctx)` returns a proxy over `ctx.data`; destructure needed fields immediately during render and avoid storing the proxy for later use.
|
|
35
|
-
|
|
36
|
-
## Utilities and Gotchas
|
|
37
|
-
- Value comparisons are shallow; mutate objects before republishing or provide new references so `publish` sees changes.
|
|
38
|
-
- `useArrayHash`/`useObjectHash` generate random hashes when array/object shape changes; they do not deep-compare nested content.
|
|
39
|
-
- The reserved `'auto-ctx'` namespace powers `AutoRootCtx`; do not manually reuse this context name.
|
|
40
|
-
|
|
41
|
-
## Examples
|
|
42
|
-
- Five complete examples in `src/examples/`: counter, todo, form, timer, cart.
|
|
43
|
-
- `Playground.tsx` uses `@codesandbox/sandpack-react` to render interactive examples with live editing.
|
|
44
|
-
- Each example has `state.ts` (hook), `view.tsx` (components), `app.tsx` (root), and `index.ts` (exports).
|
|
45
|
-
- Demo uses `?raw` imports to show source code in Sandpack editor.
|
|
46
|
-
|
|
47
|
-
## Build and Tooling
|
|
48
|
-
- `yarn build` runs Vite with `@vitejs/plugin-react`, `vite-plugin-dts`, and `vite-bundle-analyzer`; the analyzer spins up a server after builds—stop it in CI if unused.
|
|
49
|
-
- `yarn dev` starts the Vite dev server on port 3000 with demo examples; `yarn build:demo` creates GitHub Pages build.
|
|
50
|
-
- `yarn preview` previews demo build locally with correct base path (`/react-state-custom/`).
|
|
51
|
-
- Tests are stubbed (`yarn test` exits 0 after printing "No tests specified"); add coverage before depending on test gates.
|
|
52
|
-
- Repo is Yarn 4 (PnP); if editors struggle with module resolution, run `./fix-vscode-yarn-pnp.sh`.
|
|
53
|
-
- GitHub Actions workflow (`.github/workflows/deploy.yml`) auto-deploys demo to GitHub Pages on push to master.
|
|
54
|
-
|
|
55
|
-
## Reference Docs
|
|
56
|
-
- README (`README.md`) offers narrative examples and positioning; includes live demo link.
|
|
57
|
-
- API reference (`API_DOCUMENTATION.md`) documents every export; update alongside code changes.
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
name: Deploy Demo to GitHub Pages
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- master
|
|
7
|
-
workflow_dispatch:
|
|
8
|
-
|
|
9
|
-
permissions:
|
|
10
|
-
contents: read
|
|
11
|
-
pages: write
|
|
12
|
-
id-token: write
|
|
13
|
-
|
|
14
|
-
concurrency:
|
|
15
|
-
group: "pages"
|
|
16
|
-
cancel-in-progress: false
|
|
17
|
-
|
|
18
|
-
jobs:
|
|
19
|
-
build:
|
|
20
|
-
runs-on: ubuntu-latest
|
|
21
|
-
steps:
|
|
22
|
-
- name: Checkout
|
|
23
|
-
uses: actions/checkout@v4
|
|
24
|
-
|
|
25
|
-
- name: Setup Node
|
|
26
|
-
uses: actions/setup-node@v4
|
|
27
|
-
with:
|
|
28
|
-
node-version: '20'
|
|
29
|
-
|
|
30
|
-
- name: Enable Corepack
|
|
31
|
-
run: corepack enable
|
|
32
|
-
|
|
33
|
-
- name: Install dependencies
|
|
34
|
-
run: yarn install --immutable
|
|
35
|
-
|
|
36
|
-
- name: Build demo
|
|
37
|
-
run: yarn build:demo
|
|
38
|
-
|
|
39
|
-
- name: Setup Pages
|
|
40
|
-
uses: actions/configure-pages@v4
|
|
41
|
-
|
|
42
|
-
- name: Upload artifact
|
|
43
|
-
uses: actions/upload-pages-artifact@v3
|
|
44
|
-
with:
|
|
45
|
-
path: './demo-dist'
|
|
46
|
-
|
|
47
|
-
deploy:
|
|
48
|
-
environment:
|
|
49
|
-
name: github-pages
|
|
50
|
-
url: ${{ steps.deployment.outputs.page_url }}
|
|
51
|
-
runs-on: ubuntu-latest
|
|
52
|
-
needs: build
|
|
53
|
-
steps:
|
|
54
|
-
- name: Deploy to GitHub Pages
|
|
55
|
-
id: deployment
|
|
56
|
-
uses: actions/deploy-pages@v4
|
package/.vscode/extensions.json
DELETED