@20baht/store 0.3.1 → 0.3.2
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/README.md +20 -0
- package/dist/index.d.ts +34 -0
- package/dist/react.d.ts +3 -1
- package/dist/react.js +0 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,6 +37,26 @@ const off = store.subscribe((data) => console.log(data));
|
|
|
37
37
|
- Updates are immutable — untouched subtrees keep their identity, so snapshots
|
|
38
38
|
are cheap to diff/memoize.
|
|
39
39
|
|
|
40
|
+
## Typed
|
|
41
|
+
|
|
42
|
+
Name the shape of the data and the refs are typed: a top-level key, or `form.key` for a workflow.
|
|
43
|
+
The shape comes from the schema (`defineForm` / `defineWorkflow` in `@20baht/engine`):
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import type { FormValues } from '@20baht/engine';
|
|
47
|
+
|
|
48
|
+
const store = createBahtStore<FormValues<typeof contactForm>>();
|
|
49
|
+
store.get('age'); // number | null
|
|
50
|
+
store.set('age', 'old'); // compile error
|
|
51
|
+
store.patch({ nmae: 'Ada' }); // compile error
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`WorkflowValues<typeof workflow>` does the same for a `<baht-workflow>` (`store.get('Request.days')`).
|
|
55
|
+
A ref the type cannot follow (a repeater row, `ports[0].port`) reads as `unknown` and still works.
|
|
56
|
+
The type is always named, never inferred from the initial data; without it the store is the
|
|
57
|
+
untyped `BahtStore` it always was, and a typed store is still a `BahtStore` to code that takes any.
|
|
58
|
+
`useBahtStore` / `useBahtValue` follow the store's type.
|
|
59
|
+
|
|
40
60
|
## React
|
|
41
61
|
|
|
42
62
|
```jsx
|
package/dist/index.d.ts
CHANGED
|
@@ -24,4 +24,38 @@ export interface BahtStore {
|
|
|
24
24
|
*/
|
|
25
25
|
bind(el: HTMLElement): () => void;
|
|
26
26
|
}
|
|
27
|
+
/** One level down: `D[A][B]`, through a form that may be absent. */
|
|
28
|
+
type Nested<O, B extends string> = O extends undefined ? undefined : B extends keyof O ? O[B] : unknown;
|
|
29
|
+
/**
|
|
30
|
+
* The value at `ref` in data shaped `D`: a top-level key, or `form.key`. Anything else (a
|
|
31
|
+
* repeater row, a key the type does not list) is `unknown`.
|
|
32
|
+
*/
|
|
33
|
+
export type ValueAt<D, R extends string> = R extends keyof D ? D[R] : R extends `${infer A}.${infer B}` ? A extends keyof D ? Nested<D[A], B> : unknown : unknown;
|
|
34
|
+
/**
|
|
35
|
+
* A `BahtStore` told the shape of its data: `createBahtStore<FormValues<typeof form>>()` for a
|
|
36
|
+
* bound `<baht-form>`, `WorkflowValues<typeof workflow>` for a `<baht-workflow>` (types of
|
|
37
|
+
* `@20baht/engine`). The same object, and still a `BahtStore` to code that takes any.
|
|
38
|
+
*/
|
|
39
|
+
export interface TypedBahtStore<D extends Record<string, unknown>> {
|
|
40
|
+
/** Whole tree (stable identity between changes) or one ref's value. */
|
|
41
|
+
get(): D;
|
|
42
|
+
get<R extends string>(ref: R): ValueAt<D, R>;
|
|
43
|
+
/** Write one ref — subscribers fire, bound elements update in place. */
|
|
44
|
+
set<R extends string>(ref: R, value: ValueAt<D, R>): void;
|
|
45
|
+
/** Shallow-merge at the root. */
|
|
46
|
+
patch(partial: Partial<D>): void;
|
|
47
|
+
/** Swap the whole tree. */
|
|
48
|
+
replace(data: D): void;
|
|
49
|
+
/** Back to the initial data the store was created with. */
|
|
50
|
+
reset(): void;
|
|
51
|
+
subscribe: (listener: (data: D) => void) => () => void;
|
|
52
|
+
/** Two-way sync with a <baht-form> or <baht-workflow>: see `BahtStore.bind`. */
|
|
53
|
+
bind(el: HTMLElement): () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* `createBahtStore<FormValues<typeof form>>()` types the refs (`TypedBahtStore`); the type is
|
|
57
|
+
* named, never inferred from `initial`, which may hold only some of the keys.
|
|
58
|
+
*/
|
|
27
59
|
export declare function createBahtStore(initial?: Record<string, unknown>): BahtStore;
|
|
60
|
+
export declare function createBahtStore<D extends Record<string, unknown>>(initial?: NoInfer<Partial<D>>): TypedBahtStore<D>;
|
|
61
|
+
export {};
|
package/dist/react.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { BahtStore } from './index.js';
|
|
1
|
+
import type { BahtStore, TypedBahtStore, ValueAt } from './index.js';
|
|
2
2
|
/** The whole tree, re-rendering on every store change. */
|
|
3
|
+
export declare function useBahtStore<D extends Record<string, unknown>>(store: TypedBahtStore<D>): D;
|
|
3
4
|
export declare function useBahtStore(store: BahtStore): Record<string, unknown>;
|
|
4
5
|
/**
|
|
5
6
|
* One ref as [value, setValue] — re-renders only when THAT subtree changes
|
|
@@ -7,4 +8,5 @@ export declare function useBahtStore(store: BahtStore): Record<string, unknown>;
|
|
|
7
8
|
*
|
|
8
9
|
* const [age, setAge] = useBahtValue(store, 'Form_1.age');
|
|
9
10
|
*/
|
|
11
|
+
export declare function useBahtValue<D extends Record<string, unknown>, R extends string>(store: TypedBahtStore<D>, ref: R): [ValueAt<D, R>, (value: ValueAt<D, R>) => void];
|
|
10
12
|
export declare function useBahtValue(store: BahtStore, ref: string): [unknown, (value: unknown) => void];
|
package/dist/react.js
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
// @20baht/store/react — hooks over the framework-agnostic store.
|
|
2
2
|
// React is an optional peer dep; import this entry only from React apps.
|
|
3
3
|
import { useCallback, useSyncExternalStore } from 'react';
|
|
4
|
-
/** The whole tree, re-rendering on every store change. */
|
|
5
4
|
export function useBahtStore(store) {
|
|
6
5
|
return useSyncExternalStore(store.subscribe, () => store.get(), () => store.get());
|
|
7
6
|
}
|
|
8
|
-
/**
|
|
9
|
-
* One ref as [value, setValue] — re-renders only when THAT subtree changes
|
|
10
|
-
* (immutable updates keep untouched subtrees identity-stable).
|
|
11
|
-
*
|
|
12
|
-
* const [age, setAge] = useBahtValue(store, 'Form_1.age');
|
|
13
|
-
*/
|
|
14
7
|
export function useBahtValue(store, ref) {
|
|
15
8
|
const value = useSyncExternalStore(store.subscribe, () => store.get(ref), () => store.get(ref));
|
|
16
9
|
const set = useCallback((v) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20baht/store",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Tiny key-value state adapter for bahtui forms — get/set by ref (Form_1.age), subscribe, and two-way bind to <baht-form>/<baht-workflow>.",
|
|
6
6
|
"license": "MIT",
|