@k8ordo/state 1.4.4 → 2.0.0
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 +1 -1
- package/dist/register.d.mts +2 -3
- package/dist/store/core.d.mts +7 -11
- package/dist/store/core.mjs +5 -1
- package/dist/store/memory-store.mjs +2 -4
- package/dist/store/page-store.mjs +29 -27
- package/dist/url/codec.mjs +9 -10
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ pnpm add @k8ordo/state zod
|
|
|
32
32
|
| ---------------- | ------- | -------- | ------------------------------------------------------- |
|
|
33
33
|
| `react` | ≥19.3.0 | yes | `useAppState` |
|
|
34
34
|
| `zod` | ^4.6.6 | yes | the schemas (`zod/mini` works, and is the lighter pick) |
|
|
35
|
-
| `@k8ordo/router` | ^
|
|
35
|
+
| `@k8ordo/router` | ^2.0.0 | optional | typed `href` paths from the app's route table |
|
|
36
36
|
| `typescript` | ≥7.0.0 | optional | the shipped type declarations |
|
|
37
37
|
| `@types/react` | ≥19.3.0 | optional | the shipped type declarations |
|
|
38
38
|
|
package/dist/register.d.mts
CHANGED
|
@@ -12,9 +12,8 @@ import { NavigablePath, NavigablePatternOf, Routes, RoutesRecord } from "@k8ordo
|
|
|
12
12
|
*
|
|
13
13
|
* and every `href()` in the app is checked against the table's linkable
|
|
14
14
|
* patterns. A router that is not `@k8ordo/router` registers its path union
|
|
15
|
-
* directly — `interface Register { path: Route }` with `Route` from `next
|
|
16
|
-
*
|
|
17
|
-
* existed. Declared as an interface — the one exception to the repository's
|
|
15
|
+
* directly — `interface Register { path: Route }` with `Route` from `next`.
|
|
16
|
+
* Declared as an interface — the one exception to the repository's
|
|
18
17
|
* type-only rule — because declaration merging is the entire mechanism.
|
|
19
18
|
*/
|
|
20
19
|
export interface Register {}
|
package/dist/store/core.d.mts
CHANGED
|
@@ -14,17 +14,12 @@ export type UpdateHandle = {
|
|
|
14
14
|
finished: Promise<void>;
|
|
15
15
|
};
|
|
16
16
|
export type Patch = StateValues | ((current: StateValues) => StateValues);
|
|
17
|
-
/**
|
|
18
|
-
* `tree` is the pathname the tree a caller renders in was applied for, which
|
|
19
|
-
* an intercepting router provides (`tree-pathname.ts`) — only a page store
|
|
20
|
-
* reads it.
|
|
21
|
-
*/
|
|
22
17
|
export type Store = {
|
|
23
|
-
subscribe: (keys: readonly string[] | null, notify: () => void, tree
|
|
24
|
-
getSnapshot: (sig: string, keys: readonly string[] | null, tree
|
|
25
|
-
update: (patch: Patch, options
|
|
18
|
+
subscribe: (keys: readonly string[] | null, notify: () => void, tree: string | null) => () => void;
|
|
19
|
+
getSnapshot: (sig: string, keys: readonly string[] | null, tree: string | null) => StateValues;
|
|
20
|
+
update: (patch: Patch, options: {
|
|
26
21
|
history?: 'push' | 'replace';
|
|
27
|
-
}, tree
|
|
22
|
+
} | undefined, tree: string | null) => UpdateHandle;
|
|
28
23
|
dispose: () => void;
|
|
29
24
|
};
|
|
30
25
|
export type Handle = {
|
|
@@ -37,6 +32,7 @@ export type Handle = {
|
|
|
37
32
|
}) => void;
|
|
38
33
|
};
|
|
39
34
|
export declare const createHandle: () => Handle;
|
|
35
|
+
export declare const settledHandle: () => UpdateHandle;
|
|
40
36
|
/** A snapshot held still, with the picks already handed out for it. */
|
|
41
37
|
export type Frozen = {
|
|
42
38
|
readonly values: StateValues;
|
|
@@ -48,8 +44,8 @@ export type StoreCore = {
|
|
|
48
44
|
/** Notifies every listener, whatever moved. */
|
|
49
45
|
touch: () => void;
|
|
50
46
|
applyNext: (next: StateValues) => void;
|
|
51
|
-
subscribe:
|
|
52
|
-
getSnapshot:
|
|
47
|
+
subscribe: (keys: readonly string[] | null, notify: () => void) => () => void;
|
|
48
|
+
getSnapshot: (sig: string, keys: readonly string[] | null) => StateValues;
|
|
53
49
|
clear: () => void;
|
|
54
50
|
};
|
|
55
51
|
/**
|
package/dist/store/core.mjs
CHANGED
|
@@ -36,6 +36,10 @@ const createHandle = () => {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
};
|
|
39
|
+
const settledHandle = () => ({
|
|
40
|
+
committed: Promise.resolve(),
|
|
41
|
+
finished: Promise.resolve()
|
|
42
|
+
});
|
|
39
43
|
/**
|
|
40
44
|
* The shared half of every store: an immutable snapshot over a fixed key set,
|
|
41
45
|
* exact per-key change detection, and notification only to the listeners
|
|
@@ -113,4 +117,4 @@ const resolvePatch = (patch, current, keys, label) => {
|
|
|
113
117
|
for (const key of Object.keys(resolved)) if (!keys.includes(key)) throw new TypeError(`"${label}" has no field "${key}"`);
|
|
114
118
|
return resolved;
|
|
115
119
|
};
|
|
116
|
-
export { createHandle, createStoreCore, resolvePatch };
|
|
120
|
+
export { createHandle, createStoreCore, resolvePatch, settledHandle };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createStoreCore, resolvePatch, settledHandle } from "./core.mjs";
|
|
2
2
|
import { getOrCreateStore } from "./registry.mjs";
|
|
3
3
|
// src/store/memory-store.ts
|
|
4
4
|
const createMemoryStore = (def) => {
|
|
@@ -10,9 +10,7 @@ const createMemoryStore = (def) => {
|
|
|
10
10
|
...core.snapshot(),
|
|
11
11
|
...resolved
|
|
12
12
|
});
|
|
13
|
-
|
|
14
|
-
handle.settle();
|
|
15
|
-
return handle.external;
|
|
13
|
+
return settledHandle();
|
|
16
14
|
};
|
|
17
15
|
return {
|
|
18
16
|
subscribe: core.subscribe,
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { isRecord, sameValue } from "../schema/object.mjs";
|
|
2
|
-
import { createHandle, createStoreCore, resolvePatch } from "./core.mjs";
|
|
2
|
+
import { createHandle, createStoreCore, resolvePatch, settledHandle } from "./core.mjs";
|
|
3
3
|
import { getOrCreateStore } from "./registry.mjs";
|
|
4
4
|
// src/store/page-store.ts
|
|
5
5
|
const storedEntryState = () => navigation.currentEntry?.getState();
|
|
6
6
|
const readLive = (def, page) => {
|
|
7
|
-
const
|
|
8
|
-
if (page.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return
|
|
7
|
+
const url = page.url === null ? {} : page.url.parse(new URL(location.href).searchParams);
|
|
8
|
+
if (page.entry === null) return {
|
|
9
|
+
url,
|
|
10
|
+
entry: {}
|
|
11
|
+
};
|
|
12
|
+
const stored = storedEntryState();
|
|
13
|
+
return {
|
|
14
|
+
url,
|
|
15
|
+
entry: page.entry.parse(isRecord(stored) ? stored[def.key] : void 0)
|
|
16
|
+
};
|
|
14
17
|
};
|
|
15
18
|
/**
|
|
16
19
|
* The URL's pathname as the router compares a tree's: a trailing slash is
|
|
@@ -24,7 +27,11 @@ const livePathname = () => {
|
|
|
24
27
|
return pathname.slice(0, end);
|
|
25
28
|
};
|
|
26
29
|
const createPageStore = (def, page) => {
|
|
27
|
-
const
|
|
30
|
+
const initial = readLive(def, page);
|
|
31
|
+
const core = createStoreCore(page.keys, {
|
|
32
|
+
...initial.url,
|
|
33
|
+
...initial.entry
|
|
34
|
+
});
|
|
28
35
|
let at = livePathname();
|
|
29
36
|
const left = /* @__PURE__ */ new Map();
|
|
30
37
|
const bound = /* @__PURE__ */ new Map();
|
|
@@ -41,8 +48,9 @@ const createPageStore = (def, page) => {
|
|
|
41
48
|
for (const tree of left.keys()) if (!bound.has(tree)) left.delete(tree);
|
|
42
49
|
}
|
|
43
50
|
const live = readLive(def, page);
|
|
44
|
-
core.applyNext(
|
|
45
|
-
...live,
|
|
51
|
+
core.applyNext({
|
|
52
|
+
...live.url,
|
|
53
|
+
...live.entry,
|
|
46
54
|
...pending
|
|
47
55
|
});
|
|
48
56
|
if (returned) core.touch();
|
|
@@ -74,28 +82,26 @@ const createPageStore = (def, page) => {
|
|
|
74
82
|
pendingPush = false;
|
|
75
83
|
handle = null;
|
|
76
84
|
try {
|
|
77
|
-
const
|
|
78
|
-
const stored = storedEntryState();
|
|
79
|
-
const liveUrl = page.url === null ? {} : page.url.parse(url.searchParams);
|
|
80
|
-
const liveEntry = page.entry === null ? {} : page.entry.parse(isRecord(stored) ? stored[def.key] : void 0);
|
|
85
|
+
const live = readLive(def, page);
|
|
81
86
|
const target = canonical({
|
|
82
|
-
...
|
|
83
|
-
...
|
|
87
|
+
...live.url,
|
|
88
|
+
...live.entry,
|
|
84
89
|
...patch
|
|
85
90
|
});
|
|
86
|
-
const urlChanged = page.url?.keys.some((key) => !sameValue(
|
|
87
|
-
const entryChanged = page.entry?.keys.some((key) => !sameValue(
|
|
91
|
+
const urlChanged = page.url?.keys.some((key) => !sameValue(live.url[key], target[key])) ?? false;
|
|
92
|
+
const entryChanged = page.entry?.keys.some((key) => !sameValue(live.entry[key], target[key])) ?? false;
|
|
88
93
|
if (!urlChanged && !entryChanged) {
|
|
89
94
|
core.applyNext(target);
|
|
90
95
|
current.settle();
|
|
91
96
|
return;
|
|
92
97
|
}
|
|
93
|
-
const state = carryState(
|
|
98
|
+
const state = carryState(storedEntryState(), target);
|
|
94
99
|
if (!urlChanged) {
|
|
95
100
|
navigation.updateCurrentEntry({ state });
|
|
96
101
|
current.settle();
|
|
97
102
|
return;
|
|
98
103
|
}
|
|
104
|
+
const url = new URL(location.href);
|
|
99
105
|
if (page.url !== null) {
|
|
100
106
|
for (const key of page.url.keys) url.searchParams.delete(key);
|
|
101
107
|
for (const [key, value] of new URLSearchParams(page.url.search(target))) url.searchParams.append(key, value);
|
|
@@ -109,11 +115,7 @@ const createPageStore = (def, page) => {
|
|
|
109
115
|
}
|
|
110
116
|
};
|
|
111
117
|
const update = (patch, options, tree) => {
|
|
112
|
-
if (tree !==
|
|
113
|
-
const dropped = createHandle();
|
|
114
|
-
dropped.settle();
|
|
115
|
-
return dropped.external;
|
|
116
|
-
}
|
|
118
|
+
if (tree !== null && tree !== at) return settledHandle();
|
|
117
119
|
const resolved = resolvePatch(patch, core.snapshot(), page.keys, def.key);
|
|
118
120
|
const echoed = canonical({
|
|
119
121
|
...core.snapshot(),
|
|
@@ -133,7 +135,7 @@ const createPageStore = (def, page) => {
|
|
|
133
135
|
};
|
|
134
136
|
const subscribe = (keys, notify, tree) => {
|
|
135
137
|
const unsubscribe = core.subscribe(keys, notify);
|
|
136
|
-
if (tree ===
|
|
138
|
+
if (tree === null) return unsubscribe;
|
|
137
139
|
bound.set(tree, (bound.get(tree) ?? 0) + 1);
|
|
138
140
|
return () => {
|
|
139
141
|
unsubscribe();
|
|
@@ -143,7 +145,7 @@ const createPageStore = (def, page) => {
|
|
|
143
145
|
};
|
|
144
146
|
};
|
|
145
147
|
const getSnapshot = (sig, keys, tree) => {
|
|
146
|
-
const held = tree ===
|
|
148
|
+
const held = tree === null || tree === at ? void 0 : left.get(tree);
|
|
147
149
|
if (held === void 0) return core.getSnapshot(sig, keys);
|
|
148
150
|
if (keys === null) return held.values;
|
|
149
151
|
let pick = held.picks.get(sig);
|
package/dist/url/codec.mjs
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import { analyzeSchema, parseWithSalvage, sameValue } from "../schema/object.mjs";
|
|
2
|
-
import { safeEncode } from "zod/v4/core";
|
|
2
|
+
import { $ZodArray, $ZodCatch, $ZodDefault, $ZodNonOptional, $ZodNullable, $ZodOptional, $ZodPipe, $ZodPrefault, $ZodReadonly, safeEncode } from "zod/v4/core";
|
|
3
3
|
// src/url/codec.ts
|
|
4
|
-
const defOf = (schema) => schema._zod.def;
|
|
5
4
|
/**
|
|
6
5
|
* Peels wrappers (`default`, `optional`, `catch`, …) and pipes down to the
|
|
7
6
|
* type that decides how many values a param carries. Only the input side of a
|
|
8
7
|
* pipe matters here — that is the side the URL string enters through.
|
|
9
8
|
*/
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return
|
|
9
|
+
const baseOf = (schema) => {
|
|
10
|
+
if (schema instanceof $ZodOptional || schema instanceof $ZodNullable || schema instanceof $ZodDefault || schema instanceof $ZodPrefault || schema instanceof $ZodNonOptional || schema instanceof $ZodCatch || schema instanceof $ZodReadonly) return baseOf(schema._zod.def.innerType);
|
|
11
|
+
if (schema instanceof $ZodPipe) return baseOf(schema._zod.def.in);
|
|
12
|
+
return schema;
|
|
14
13
|
};
|
|
15
14
|
const readAll = (input, key) => {
|
|
16
15
|
if (input instanceof URLSearchParams) return input.getAll(key);
|
|
@@ -36,17 +35,17 @@ const serializeValue = (schema, key, value) => {
|
|
|
36
35
|
throw new TypeError(`url field "${key}" holds a value of type ${typeof value}, which has no URL serialization`);
|
|
37
36
|
};
|
|
38
37
|
const refuseBoolean = (schema, name) => {
|
|
39
|
-
if (
|
|
38
|
+
if (baseOf(schema)._zod.def.type === "boolean") throw new TypeError(`url boolean fields must use z.stringbool() — a URL carries strings, and "false" is not false to z.boolean() or z.coerce.boolean(): ${name}`);
|
|
40
39
|
};
|
|
41
40
|
const createUrlCodec = (schema) => {
|
|
42
41
|
const info = analyzeSchema(schema, "url");
|
|
43
42
|
const { keys, defaults } = info;
|
|
44
43
|
const items = /* @__PURE__ */ new Map();
|
|
45
44
|
for (const key of keys) {
|
|
46
|
-
const base =
|
|
47
|
-
if (base
|
|
45
|
+
const base = baseOf(info.shape[key]);
|
|
46
|
+
if (base instanceof $ZodArray) {
|
|
48
47
|
if (!sameValue(defaults[key], [])) throw new TypeError(`url array fields must default to [] — an absent param and an empty list are the same URL: ${key}`);
|
|
49
|
-
const item = base.element;
|
|
48
|
+
const item = base._zod.def.element;
|
|
50
49
|
refuseBoolean(item, `${key}[]`);
|
|
51
50
|
items.set(key, item);
|
|
52
51
|
} else refuseBoolean(info.shape[key], key);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k8ordo/state",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Declare state by where it lives — URL search params, history-entry state, localStorage, sessionStorage, a cookie, or memory. One zod schema per boundary place and a typed box for memory, over the Navigation API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cookie",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@arethetypeswrong/cli": "0.18.5",
|
|
46
|
-
"@k8ordo/router": "^
|
|
46
|
+
"@k8ordo/router": "^2.0.0",
|
|
47
47
|
"@types/react": "19.3.0",
|
|
48
48
|
"@types/react-dom": "19.3.0",
|
|
49
49
|
"@vitejs/plugin-basic-ssl": "2.3.0",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"zod": "4.6.7"
|
|
62
62
|
},
|
|
63
63
|
"peerDependencies": {
|
|
64
|
-
"@k8ordo/router": "^
|
|
64
|
+
"@k8ordo/router": "^2.0.0",
|
|
65
65
|
"@types/react": ">=19.3.0",
|
|
66
66
|
"react": ">=19.3.0",
|
|
67
67
|
"typescript": ">=7.0.0",
|