@kdeveloper/kvark 0.6.3 → 0.6.6
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 +32 -13
- package/dist/{atom-DEAcunGv.d.mts → atom-BC8W-H22.d.ts} +2 -2
- package/dist/atom-BC8W-H22.d.ts.map +1 -0
- package/dist/{family.d.mts → family.d.ts} +3 -3
- package/dist/family.d.ts.map +1 -0
- package/dist/{family.mjs → family.js} +26 -18
- package/dist/family.js.map +1 -0
- package/dist/{index.d.mts → index.d.ts} +2 -2
- package/dist/index.js +2 -0
- package/dist/react/{index.d.mts → index.d.ts} +2 -2
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/{index.mjs → index.js} +2 -2
- package/dist/react/index.js.map +1 -0
- package/dist/{store-DQL8VJhU.d.mts → store-Cng0OzIQ.d.ts} +1 -1
- package/dist/store-Cng0OzIQ.d.ts.map +1 -0
- package/dist/{store-j2laS7Q2.mjs → store-DuKg4_ng.js} +122 -48
- package/dist/store-DuKg4_ng.js.map +1 -0
- package/dist/types-B2mzc3A5.js +8 -0
- package/dist/types-B2mzc3A5.js.map +1 -0
- package/dist/vue/{index.d.mts → index.d.ts} +8 -7
- package/dist/vue/index.d.ts.map +1 -0
- package/dist/vue/{index.mjs → index.js} +1 -1
- package/dist/vue/index.js.map +1 -0
- package/package.json +15 -13
- package/dist/atom-DEAcunGv.d.mts.map +0 -1
- package/dist/family.d.mts.map +0 -1
- package/dist/family.mjs.map +0 -1
- package/dist/index.mjs +0 -2
- package/dist/react/index.d.mts.map +0 -1
- package/dist/react/index.mjs.map +0 -1
- package/dist/store-DQL8VJhU.d.mts.map +0 -1
- package/dist/store-j2laS7Q2.mjs.map +0 -1
- package/dist/vue/index.d.mts.map +0 -1
- package/dist/vue/index.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
# @kdeveloper/kvark
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@kdeveloper/kvark)
|
|
4
|
+
|
|
3
5
|
Atomic state management for **React** and **Vue 3** with **explicit dependency graphs**, stale-while-revalidate, and first-class external invalidation.
|
|
4
6
|
|
|
5
7
|
Inspired by Jotai, but built around a key difference: dependencies are declared upfront rather than inferred at runtime. This makes the data graph statically analysable, enables parallel loading, and allows invalidation from anywhere — WebSocket handlers, SSE streams, timers, Service Workers — without framework hooks.
|
|
6
8
|
|
|
7
|
-
```bash
|
|
8
|
-
pnpm add @kdeveloper/kvark
|
|
9
|
-
# optional peers: react >=18 and/or vue >=3.4 (install the one you use)
|
|
10
|
-
```
|
|
11
|
-
|
|
12
9
|
## Why Kvark?
|
|
13
10
|
|
|
14
11
|
| | Jotai | Kvark |
|
|
@@ -75,7 +72,7 @@ function UserCard() {
|
|
|
75
72
|
|
|
76
73
|
### Vue 3
|
|
77
74
|
|
|
78
|
-
The same atoms and store work with `@kdeveloper/kvark/vue`. Composables mirror the React hooks; `useAtomValue` returns an **awaitable** [`shallowRef`](https://vuejs.org/api/reactivity-advanced.html#shallowref) — use `.value` in `<script setup>` (templates unwrap refs automatically).
|
|
75
|
+
The same atoms and store work with `@kdeveloper/kvark/vue`. Composables mirror the React hooks; `useAtomValue` returns an **awaitable** [`shallowRef`](https://vuejs.org/api/reactivity-advanced.html#shallowref) — use `.value` in `<script setup>` (templates unwrap refs automatically). Until the first `get` resolves, that ref’s value is `undefined`; TypeScript types it as `V | undefined` (`ThenableShallowRef<V>`). After `await useAtomValue(atom)` you get `Readonly<ShallowRef<V>>` with a defined `.value`. The ref also implements `PromiseLike`, so awaiting it in an async `setup` suspends until the first `get` resolves — see [Suspense (Vue 3)](#suspense-vue-3).
|
|
79
76
|
|
|
80
77
|
Composables must run **inside** a `Provider` subtree. Put `useAtomValue` / `useAtom` in a child component (or a nested route), not in the same component that renders `Provider` without passing the store through a wrapper.
|
|
81
78
|
|
|
@@ -112,10 +109,14 @@ const user = useAtomValue(userAtom);
|
|
|
112
109
|
</script>
|
|
113
110
|
|
|
114
111
|
<template>
|
|
115
|
-
<div>{{ user
|
|
112
|
+
<div>{{ user?.name }}</div>
|
|
116
113
|
</template>
|
|
117
114
|
```
|
|
118
115
|
|
|
116
|
+
While the atom is **pending**, the unwrapped ref is `undefined`, so `{{ user.name }}` would throw at runtime and TypeScript flags `user.name` in script. Use optional chaining (as above), `v-if="user"`, or `await useAtomValue(userAtom)` with `<Suspense>`.
|
|
117
|
+
|
|
118
|
+
With `useAtomValue(atom, { observe: true })`, the ref always holds an `ObservedValue` object (`value`, `isStale`, `error`); `value` is `V | undefined` while pending (`ThenableObservedShallowRef<V>`).
|
|
119
|
+
|
|
119
120
|
`App.vue`
|
|
120
121
|
|
|
121
122
|
```vue
|
|
@@ -417,6 +418,8 @@ const balance = await readBalance();
|
|
|
417
418
|
|
|
418
419
|
Same composable names and behaviour as React: `Provider`, `useStore`, `useAtomValue`, `useSetAtom`, `useAtom`, `useAtomContext`. Wrap your app (or subtree) with `Provider` and pass `:store="store"`.
|
|
419
420
|
|
|
421
|
+
Exported types: **`ThenableShallowRef<V>`** (default `useAtomValue`), **`ThenableObservedShallowRef<V>`** and **`ObservedValue<V>`** (with `{ observe: true }`). They encode pending vs resolved ref shapes for TypeScript.
|
|
422
|
+
|
|
420
423
|
`useAtomValue` / `useAtom` expose values as **awaitable shallow refs** (`PromiseLike`) — in script code use `.value`; in templates Vue unwraps refs for you. `await useAtomValue(atom)` in an async setup suspends until the atom's first `get` resolves, integrating with `<Suspense>`.
|
|
421
424
|
|
|
422
425
|
### Suspense (Vue 3)
|
|
@@ -606,15 +609,31 @@ type Writable = IsWritable<typeof countAtom>; // → true | false
|
|
|
606
609
|
|
|
607
610
|
## Package Structure
|
|
608
611
|
|
|
609
|
-
| Import | Contents
|
|
610
|
-
| -------------------------- |
|
|
611
|
-
| `@kdeveloper/kvark` | `atom`, `createStore`, all types
|
|
612
|
-
| `@kdeveloper/kvark/react` | `Provider`, `useStore`, `useAtomValue`, `useSetAtom`, `useAtom`, `useAtomContext`
|
|
613
|
-
| `@kdeveloper/kvark/vue` | `Provider`, `useStore`, `useAtomValue`, `useSetAtom`, `useAtom`, `useAtomContext` |
|
|
614
|
-
| `@kdeveloper/kvark/family` | `atomFamily`, `stableFamilyKey`, re-exports `atom` / `createStore` and core types
|
|
612
|
+
| Import | Contents |
|
|
613
|
+
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
614
|
+
| `@kdeveloper/kvark` | `atom`, `createStore`, all types |
|
|
615
|
+
| `@kdeveloper/kvark/react` | `Provider`, `useStore`, `useAtomValue`, `useSetAtom`, `useAtom`, `useAtomContext` |
|
|
616
|
+
| `@kdeveloper/kvark/vue` | `Provider`, `useStore`, `useAtomValue`, `useSetAtom`, `useAtom`, `useAtomContext`; types `ThenableShallowRef`, `ThenableObservedShallowRef`, `ObservedValue` |
|
|
617
|
+
| `@kdeveloper/kvark/family` | `atomFamily`, `stableFamilyKey`, re-exports `atom` / `createStore` and core types |
|
|
615
618
|
|
|
616
619
|
The core (`@kdeveloper/kvark`) has **zero runtime dependencies**. **React** and **Vue** are optional peer dependencies — install the framework you use and import from `/react` or `/vue`.
|
|
617
620
|
|
|
621
|
+
The published package is **ESM-only** (`import`); there is no CommonJS `require` entry.
|
|
622
|
+
|
|
623
|
+
## Development
|
|
624
|
+
|
|
625
|
+
This repo uses [pnpm](https://pnpm.io) (see `packageManager` in `package.json`).
|
|
626
|
+
|
|
627
|
+
```bash
|
|
628
|
+
pnpm install
|
|
629
|
+
pnpm build # tsc --noEmit && tsdown (ESM + .d.ts + source maps)
|
|
630
|
+
pnpm test # vitest run
|
|
631
|
+
pnpm lint # oxlint
|
|
632
|
+
pnpm fmt # oxfmt
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
Useful variants: `pnpm test:watch`, `pnpm lint:fix`, `pnpm fmt:check`, `pnpm lint:types` (TypeScript check only).
|
|
636
|
+
|
|
618
637
|
## Requirements
|
|
619
638
|
|
|
620
639
|
- **Node.js** ≥ 20
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as WritableAtom, i as Atom, o as AtomConfig, p as WritableAtomContext } from "./store-
|
|
1
|
+
import { f as WritableAtom, i as Atom, o as AtomConfig, p as WritableAtomContext } from "./store-Cng0OzIQ.js";
|
|
2
2
|
|
|
3
3
|
//#region src/internal/atom.d.ts
|
|
4
4
|
type WritableConfig<Value, Deps extends Record<string, Atom<unknown>>, Args extends readonly unknown[]> = AtomConfig<Value, Deps, Args> & {
|
|
@@ -9,4 +9,4 @@ declare function atom<Value, Deps extends Record<string, Atom<unknown>> = Record
|
|
|
9
9
|
declare function atom<Value, Deps extends Record<string, Atom<unknown>> = Record<never, never>>(config: ReadonlyConfig<Value, Deps>): Atom<Value>;
|
|
10
10
|
//#endregion
|
|
11
11
|
export { atom as t };
|
|
12
|
-
//# sourceMappingURL=atom-
|
|
12
|
+
//# sourceMappingURL=atom-BC8W-H22.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atom-BC8W-H22.d.ts","names":[],"sources":["../src/internal/atom.ts"],"mappings":";;;KASK,cAAA,qBAEU,MAAA,SAAe,IAAA,+CAE1B,UAAA,CAAW,KAAA,EAAO,IAAA,EAAM,IAAA;EAC1B,GAAA,GAAM,GAAA,EAAK,mBAAA,CAAoB,IAAA,EAAM,KAAA,MAAW,IAAA,EAAM,IAAA,KAAS,OAAA;AAAA;AAAA,KAG5D,cAAA,qBAAmC,MAAA,SAAe,IAAA,cAAkB,IAAA,CACvE,UAAA,CAAW,KAAA,EAAO,IAAA;AAAA,iBAIJ,IAAA,qBAED,MAAA,SAAe,IAAA,aAAiB,MAAA,8DAAA,CAE7C,MAAA,EAAQ,cAAA,CAAe,KAAA,EAAO,IAAA,EAAM,IAAA,IAAQ,YAAA,CAAa,KAAA,EAAO,IAAA;AAAA,iBAElD,IAAA,qBAAyB,MAAA,SAAe,IAAA,aAAiB,MAAA,eAAA,CACvE,MAAA,EAAQ,cAAA,CAAe,KAAA,EAAO,IAAA,IAC7B,IAAA,CAAK,KAAA"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { c as AtomState, d as StalePolicy, f as WritableAtom, i as Atom, l as AtomValue, o as AtomConfig, r as createStore, s as AtomContext } from "./store-
|
|
2
|
-
import { t as atom } from "./atom-
|
|
1
|
+
import { c as AtomState, d as StalePolicy, f as WritableAtom, i as Atom, l as AtomValue, o as AtomConfig, r as createStore, s as AtomContext } from "./store-Cng0OzIQ.js";
|
|
2
|
+
import { t as atom } from "./atom-BC8W-H22.js";
|
|
3
3
|
|
|
4
4
|
//#region src/internal/family.d.ts
|
|
5
5
|
type AtomFamilyOptions<Param, Value, Deps extends Record<string, Atom<unknown>>, Args extends readonly unknown[], Key = Param> = {
|
|
@@ -46,4 +46,4 @@ declare function atomFamily<Param, Value, Deps extends Record<string, Atom<unkno
|
|
|
46
46
|
declare function stableFamilyKey(value: unknown): string;
|
|
47
47
|
//#endregion
|
|
48
48
|
export { type Atom, type AtomConfig, type AtomContext, type AtomFamily, type AtomFamilyOptions, type AtomState, type AtomValue, type StalePolicy, type WritableAtom, atom, atomFamily, createStore, stableFamilyKey };
|
|
49
|
-
//# sourceMappingURL=family.d.
|
|
49
|
+
//# sourceMappingURL=family.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"family.d.ts","names":[],"sources":["../src/internal/family.ts","../src/internal/stable-family-key.ts"],"mappings":";;;;KAKY,iBAAA,4BAGG,MAAA,SAAe,IAAA,mDAEtB,KAAA;EAEN,YAAA,IAAgB,KAAA,EAAO,KAAA,KAAU,IAAA;EACjC,WAAA,GAAc,WAAA;EACd,WAAA;EACA,OAAA;EACA,UAAA;EAR4B;;;;;;;;;;EAmB5B,QAAA,IAAY,KAAA,EAAO,KAAA,KAAU,GAAA;EAC7B,GAAA,GAAM,KAAA,EAAO,KAAA,MAAW,GAAA,EAAK,WAAA,CAAY,IAAA,MAAU,OAAA,CAAQ,KAAA;EAC3D,GAAA,IAAO,KAAA,EAAO,KAAA,MAAW,GAAA,EAAK,WAAA,CAAY,IAAA,MAAU,IAAA,EAAM,IAAA,KAAS,OAAA;AAAA;AAAA,UAGpD,UAAA,qBAA+B,KAAA;EAAA,CAC7C,KAAA,EAAO,KAAA,GAAQ,IAAA,CAAK,KAAA;EACrB,UAAA,CAAW,KAAA,EAAO,KAAA;EAClB,aAAA;EACA,MAAA,CAAO,KAAA,EAAO,KAAA;EACd,QAAA,IAAY,WAAA,CAAY,GAAA,EAAK,IAAA,CAAK,KAAA;AAAA;AAAA,iBAYpB,UAAA,4BAGD,MAAA,SAAe,IAAA,aAAiB,MAAA,qEAEvC,KAAA,CAAA,CACN,OAAA,EAAS,iBAAA,CAAkB,KAAA,EAAO,KAAA,EAAO,IAAA,EAAM,IAAA,EAAM,GAAA,IAAO,UAAA,CAAW,KAAA,EAAO,KAAA,EAAO,GAAA;;;;;;;AAlDvF;;;;;;;iBC0BgB,eAAA,CAAgB,KAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as atomFamily, r as atom, t as createStore } from "./store-
|
|
1
|
+
import { n as atomFamily, r as atom, t as createStore } from "./store-DuKg4_ng.js";
|
|
2
2
|
//#region src/internal/stable-family-key.ts
|
|
3
3
|
/**
|
|
4
4
|
* Produces a deterministic string key for plain-object/array/primitive values.
|
|
@@ -12,21 +12,26 @@ import { n as atomFamily, r as atom, t as createStore } from "./store-j2laS7Q2.m
|
|
|
12
12
|
* computed via a WeakMap memo will silently return the stale cached key.
|
|
13
13
|
*/
|
|
14
14
|
const memo = /* @__PURE__ */ new WeakMap();
|
|
15
|
+
function formatUnknownForError(value) {
|
|
16
|
+
switch (typeof value) {
|
|
17
|
+
case "string": return JSON.stringify(value);
|
|
18
|
+
case "number":
|
|
19
|
+
case "boolean":
|
|
20
|
+
case "bigint": return String(value);
|
|
21
|
+
case "symbol": return value.description ?? "(symbol)";
|
|
22
|
+
case "function": return `function ${value.name}`;
|
|
23
|
+
default: return "(unknown)";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
15
26
|
function stableFamilyKey(value) {
|
|
16
|
-
if (value
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const obj = value;
|
|
25
|
-
const cached = memo.get(obj);
|
|
26
|
-
if (cached !== void 0) return cached;
|
|
27
|
-
const result = serializeValue(obj);
|
|
28
|
-
memo.set(obj, result);
|
|
29
|
-
return result;
|
|
27
|
+
if (value !== null && typeof value === "object") {
|
|
28
|
+
const cached = memo.get(value);
|
|
29
|
+
if (cached !== void 0) return cached;
|
|
30
|
+
const result = serializeValue(value);
|
|
31
|
+
memo.set(value, result);
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
return serializeValue(value);
|
|
30
35
|
}
|
|
31
36
|
function serializeValue(value) {
|
|
32
37
|
if (value === null) return "null";
|
|
@@ -36,10 +41,13 @@ function serializeValue(value) {
|
|
|
36
41
|
if (t === "number") return String(value);
|
|
37
42
|
if (t === "boolean") return String(value);
|
|
38
43
|
if (t === "bigint") return `${String(value)}n`;
|
|
39
|
-
if (t !== "object") throw new TypeError(`stableFamilyKey: unsupported type "${t}" (${
|
|
44
|
+
if (t !== "object") throw new TypeError(`stableFamilyKey: unsupported type "${t}" (${formatUnknownForError(value)})`);
|
|
40
45
|
if (Array.isArray(value)) return `[${value.map(serializeValue).join(",")}]`;
|
|
41
46
|
const proto = Object.getPrototypeOf(value);
|
|
42
|
-
if (proto !== Object.prototype && proto !== null)
|
|
47
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
48
|
+
const ctorName = value.constructor?.name ?? "unknown";
|
|
49
|
+
throw new TypeError(`stableFamilyKey: only plain objects are supported (got ${ctorName})`);
|
|
50
|
+
}
|
|
43
51
|
return `{${Object.keys(value).sort().map((k) => {
|
|
44
52
|
const v = value[k];
|
|
45
53
|
return `${JSON.stringify(k)}:${serializeValue(v)}`;
|
|
@@ -48,4 +56,4 @@ function serializeValue(value) {
|
|
|
48
56
|
//#endregion
|
|
49
57
|
export { atom, atomFamily, createStore, stableFamilyKey };
|
|
50
58
|
|
|
51
|
-
//# sourceMappingURL=family.
|
|
59
|
+
//# sourceMappingURL=family.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"family.js","names":[],"sources":["../src/internal/stable-family-key.ts"],"sourcesContent":["/**\n * Produces a deterministic string key for plain-object/array/primitive values.\n *\n * Supported types: string, number, boolean, null, undefined, bigint,\n * plain objects (incl. nested), arrays.\n *\n * Not supported (throws or produces non-unique output): circular references,\n * class instances other than plain Object/Array, Map, Set, Symbol, functions.\n * Treat the parameter as immutable — mutating an object after its first key is\n * computed via a WeakMap memo will silently return the stale cached key.\n */\n\nconst memo = new WeakMap<object, string>();\n\nfunction formatUnknownForError(value: unknown): string {\n switch (typeof value) {\n case \"string\":\n return JSON.stringify(value);\n case \"number\":\n case \"boolean\":\n case \"bigint\":\n return String(value);\n case \"symbol\":\n return value.description ?? \"(symbol)\";\n case \"function\":\n return `function ${value.name}`;\n default:\n return \"(unknown)\";\n }\n}\n\nexport function stableFamilyKey(value: unknown): string {\n if (value !== null && typeof value === \"object\") {\n const cached = memo.get(value);\n if (cached !== undefined) {\n return cached;\n }\n\n const result = serializeValue(value);\n memo.set(value, result);\n return result;\n }\n\n return serializeValue(value);\n}\n\nfunction serializeValue(value: unknown): string {\n if (value === null) {\n return \"null\";\n }\n if (value === undefined) {\n return \"undefined\";\n }\n\n const t = typeof value;\n\n if (t === \"string\") {\n return JSON.stringify(value);\n }\n if (t === \"number\") {\n return String(value as number);\n }\n if (t === \"boolean\") {\n return String(value as boolean);\n }\n if (t === \"bigint\") {\n return `${String(value as bigint)}n`;\n }\n\n if (t !== \"object\") {\n throw new TypeError(`stableFamilyKey: unsupported type \"${t}\" (${formatUnknownForError(value)})`);\n }\n\n if (Array.isArray(value)) {\n const items = (value as unknown[]).map(serializeValue);\n return `[${items.join(\",\")}]`;\n }\n\n const proto = Object.getPrototypeOf(value as object) as object | null;\n if (proto !== Object.prototype && proto !== null) {\n const ctorName = (value as { constructor?: { name?: string } }).constructor?.name ?? \"unknown\";\n throw new TypeError(`stableFamilyKey: only plain objects are supported (got ${ctorName})`);\n }\n\n const keys = Object.keys(value as Record<string, unknown>).sort();\n const pairs = keys.map((k) => {\n const v = (value as Record<string, unknown>)[k];\n return `${JSON.stringify(k)}:${serializeValue(v)}`;\n });\n return `{${pairs.join(\",\")}}`;\n}\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAM,uBAAO,IAAI,SAAyB;AAE1C,SAAS,sBAAsB,OAAwB;AACrD,SAAQ,OAAO,OAAf;EACE,KAAK,SACH,QAAO,KAAK,UAAU,MAAM;EAC9B,KAAK;EACL,KAAK;EACL,KAAK,SACH,QAAO,OAAO,MAAM;EACtB,KAAK,SACH,QAAO,MAAM,eAAe;EAC9B,KAAK,WACH,QAAO,YAAY,MAAM;EAC3B,QACE,QAAO;;;AAIb,SAAgB,gBAAgB,OAAwB;AACtD,KAAI,UAAU,QAAQ,OAAO,UAAU,UAAU;EAC/C,MAAM,SAAS,KAAK,IAAI,MAAM;AAC9B,MAAI,WAAW,KAAA,EACb,QAAO;EAGT,MAAM,SAAS,eAAe,MAAM;AACpC,OAAK,IAAI,OAAO,OAAO;AACvB,SAAO;;AAGT,QAAO,eAAe,MAAM;;AAG9B,SAAS,eAAe,OAAwB;AAC9C,KAAI,UAAU,KACZ,QAAO;AAET,KAAI,UAAU,KAAA,EACZ,QAAO;CAGT,MAAM,IAAI,OAAO;AAEjB,KAAI,MAAM,SACR,QAAO,KAAK,UAAU,MAAM;AAE9B,KAAI,MAAM,SACR,QAAO,OAAO,MAAgB;AAEhC,KAAI,MAAM,UACR,QAAO,OAAO,MAAiB;AAEjC,KAAI,MAAM,SACR,QAAO,GAAG,OAAO,MAAgB,CAAC;AAGpC,KAAI,MAAM,SACR,OAAM,IAAI,UAAU,sCAAsC,EAAE,KAAK,sBAAsB,MAAM,CAAC,GAAG;AAGnG,KAAI,MAAM,QAAQ,MAAM,CAEtB,QAAO,IADQ,MAAoB,IAAI,eAAe,CACrC,KAAK,IAAI,CAAC;CAG7B,MAAM,QAAQ,OAAO,eAAe,MAAgB;AACpD,KAAI,UAAU,OAAO,aAAa,UAAU,MAAM;EAChD,MAAM,WAAY,MAA8C,aAAa,QAAQ;AACrF,QAAM,IAAI,UAAU,0DAA0D,SAAS,GAAG;;AAQ5F,QAAO,IALM,OAAO,KAAK,MAAiC,CAAC,MAAM,CAC9C,KAAK,MAAM;EAC5B,MAAM,IAAK,MAAkC;AAC7C,SAAO,GAAG,KAAK,UAAU,EAAE,CAAC,GAAG,eAAe,EAAE;GAChD,CACe,KAAK,IAAI,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as AtomArgs, c as AtomState, d as StalePolicy, f as WritableAtom, i as Atom, l as AtomValue, n as StoreClient, o as AtomConfig, p as WritableAtomContext, r as createStore, s as AtomContext, u as IsWritable } from "./store-
|
|
2
|
-
import { t as atom } from "./atom-
|
|
1
|
+
import { a as AtomArgs, c as AtomState, d as StalePolicy, f as WritableAtom, i as Atom, l as AtomValue, n as StoreClient, o as AtomConfig, p as WritableAtomContext, r as createStore, s as AtomContext, u as IsWritable } from "./store-Cng0OzIQ.js";
|
|
2
|
+
import { t as atom } from "./atom-BC8W-H22.js";
|
|
3
3
|
export { type Atom, type AtomArgs, type AtomConfig, type AtomContext, type AtomState, type AtomValue, type IsWritable, type StalePolicy, type StoreClient, type WritableAtom, type WritableAtomContext, atom, createStore };
|
package/dist/index.js
ADDED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as WritableAtom, i as Atom, n as StoreClient, t as Store } from "../store-
|
|
1
|
+
import { f as WritableAtom, i as Atom, n as StoreClient, t as Store } from "../store-Cng0OzIQ.js";
|
|
2
2
|
import { ReactNode } from "react";
|
|
3
3
|
|
|
4
4
|
//#region src/react/provider.d.ts
|
|
@@ -33,4 +33,4 @@ declare function useAtom<V, A extends readonly unknown[]>(atom: WritableAtom<V,
|
|
|
33
33
|
declare function useAtomContext<R>(callback: (ctx: StoreClient) => Promise<R>): () => Promise<R>;
|
|
34
34
|
//#endregion
|
|
35
35
|
export { Provider, useAtom, useAtomContext, useAtomValue, useSetAtom, useStore };
|
|
36
|
-
//# sourceMappingURL=index.d.
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/react/provider.tsx","../../src/react/use-atom-value.ts","../../src/react/use-set-atom.ts","../../src/react/use-atom.ts","../../src/react/use-atom-context.ts"],"mappings":";;;;KAKY,aAAA;EACV,KAAA,EAAO,KAAA;EACP,QAAA,EAAU,SAAA;AAAA;AAAA,iBAGI,QAAA,CAAA;EAAW,KAAA;EAAO;AAAA,GAAY,aAAA,GAAgB,SAAA;AAAA,iBAI9C,QAAA,CAAA,GAAY,KAAA;;;KCTvB,aAAA;EACH,KAAA,EAAO,CAAA;EACP,OAAA;EACA,KAAA;AAAA;AAAA,iBAGc,YAAA,GAAA,CAAgB,IAAA,EAAM,IAAA,CAAK,CAAA,IAAK,CAAA;AAAA,iBAChC,YAAA,GAAA,CAAgB,IAAA,EAAM,IAAA,CAAK,CAAA,GAAI,IAAA;EAAQ,OAAA;AAAA,IAAkB,aAAA,CAAc,CAAA;;;iBCRvE,UAAA,iCAAA,CACd,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,QACjB,IAAA,EAAM,CAAA,KAAM,OAAA;;;iBCFH,OAAA,iCAAA,CACd,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,cACX,CAAA,MAAO,IAAA,EAAM,CAAA,KAAM,OAAA;;;iBCFhB,cAAA,GAAA,CAAkB,QAAA,GAAW,GAAA,EAAK,WAAA,KAAgB,OAAA,CAAQ,CAAA,UAAW,OAAA,CAAQ,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as CONFIG } from "../types-B2mzc3A5.js";
|
|
2
2
|
import { createContext, useCallback, useContext, useSyncExternalStore } from "react";
|
|
3
3
|
import { jsx } from "react/jsx-runtime";
|
|
4
4
|
//#region src/react/provider.tsx
|
|
@@ -66,4 +66,4 @@ function useAtomContext(callback) {
|
|
|
66
66
|
//#endregion
|
|
67
67
|
export { Provider, useAtom, useAtomContext, useAtomValue, useSetAtom, useStore };
|
|
68
68
|
|
|
69
|
-
//# sourceMappingURL=index.
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/react/provider.tsx","../../src/react/use-atom-value.ts","../../src/react/use-set-atom.ts","../../src/react/use-atom.ts","../../src/react/use-atom-context.ts"],"sourcesContent":["import { createContext, useContext, type ReactNode } from \"react\";\nimport type { Store } from \"@/internal/store.js\";\n\nconst StoreContext = createContext<Store | null>(null);\n\nexport type ProviderProps = {\n store: Store;\n children: ReactNode;\n};\n\nexport function Provider({ store, children }: ProviderProps): ReactNode {\n return <StoreContext value={store}>{children}</StoreContext>;\n}\n\nexport function useStore(): Store {\n const store = useContext(StoreContext);\n if (store == null) {\n throw new Error(\"useStore must be used within a <Provider>\");\n }\n return store;\n}\n","import { useSyncExternalStore } from \"react\";\nimport type { Atom, AtomState, StalePolicy } from \"@/internal/types.js\";\nimport { CONFIG } from \"@/internal/types.js\";\nimport { useStore } from \"@/react/provider.js\";\n\ntype ObservedValue<V> = {\n value: V;\n isStale: boolean;\n error: unknown;\n};\n\nexport function useAtomValue<V>(atom: Atom<V>): V;\nexport function useAtomValue<V>(atom: Atom<V>, opts: { observe: true }): ObservedValue<V>;\nexport function useAtomValue<V>(atom: Atom<V>, opts?: { observe: true }): V | ObservedValue<V> {\n const store = useStore();\n\n const snapshot: AtomState<V> = useSyncExternalStore(\n (notify) => store.subscribe(atom, notify),\n () => store.getSnapshot(atom),\n () => store.getServerSnapshot(atom),\n );\n\n const stalePolicy: StalePolicy = atom[CONFIG].stalePolicy ?? \"keep\";\n\n if (snapshot.status === \"pending\") {\n throw store.resolve(atom);\n }\n\n if (snapshot.status === \"stale\") {\n if (stalePolicy === \"suspend\") {\n throw store.resolve(atom);\n }\n // \"keep\" or \"reset\" while stale: trigger background revalidation\n void store.resolve(atom);\n\n if (opts?.observe === true) {\n return { value: snapshot.value, isStale: true, error: undefined };\n }\n return snapshot.value;\n }\n\n if (snapshot.status === \"error\") {\n if (opts?.observe === true && snapshot.value !== undefined) {\n return {\n value: snapshot.value as V,\n isStale: false,\n error: snapshot.error,\n };\n }\n throw snapshot.error;\n }\n\n // status === \"fresh\"\n if (opts?.observe === true) {\n return { value: snapshot.value, isStale: false, error: undefined };\n }\n return snapshot.value;\n}\n","import { useCallback } from \"react\";\nimport type { WritableAtom } from \"@/internal/types.js\";\nimport { useStore } from \"@/react/provider.js\";\n\nexport function useSetAtom<V, A extends readonly unknown[]>(\n atom: WritableAtom<V, A>,\n): (...args: A) => Promise<void> {\n const store = useStore();\n return useCallback((...args: A): Promise<void> => store.set(atom, ...args), [store, atom]);\n}\n","import type { WritableAtom } from \"@/internal/types.js\";\nimport { useAtomValue } from \"@/react/use-atom-value.js\";\nimport { useSetAtom } from \"@/react/use-set-atom.js\";\n\nexport function useAtom<V, A extends readonly unknown[]>(\n atom: WritableAtom<V, A>,\n): readonly [V, (...args: A) => Promise<void>] {\n const value = useAtomValue(atom);\n const setter = useSetAtom(atom);\n return [value, setter] as const;\n}\n","import { useCallback } from \"react\";\nimport type { StoreClient } from \"@/internal/store.js\";\nimport { useStore } from \"@/react/provider.js\";\n\nexport function useAtomContext<R>(callback: (ctx: StoreClient) => Promise<R>): () => Promise<R> {\n const store = useStore();\n return useCallback((): Promise<R> => callback(store.getClient()), [store, callback]);\n}\n"],"mappings":";;;;AAGA,MAAM,eAAe,cAA4B,KAAK;AAOtD,SAAgB,S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8D;CAC9F,MAAM,QAAQ,UAAU;AACxB,QAAO,kBAA8B,SAAS,MAAM,WAAW,CAAC,EAAE,CAAC,OAAO,SAAS,CAAC"}
|
|
@@ -80,4 +80,4 @@ declare class Store {
|
|
|
80
80
|
declare function createStore(): Store;
|
|
81
81
|
//#endregion
|
|
82
82
|
export { AtomArgs as a, AtomState as c, StalePolicy as d, WritableAtom as f, Atom as i, AtomValue as l, StoreClient as n, AtomConfig as o, WritableAtomContext as p, createStore as r, AtomContext as s, Store as t, IsWritable as u };
|
|
83
|
-
//# sourceMappingURL=store-
|
|
83
|
+
//# sourceMappingURL=store-Cng0OzIQ.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-Cng0OzIQ.d.ts","names":[],"sources":["../src/internal/types.ts","../src/internal/store.ts"],"mappings":";cAAa,MAAA;AAAA,cACA,QAAA;AAAA,KAGD,SAAA;EACN,MAAA;EAAmB,KAAA;EAAkB,KAAA;AAAA;EACrC,MAAA;EAAiB,KAAA,EAAO,KAAA;EAAO,KAAA;AAAA;EAC/B,MAAA;EAAiB,KAAA,EAAO,KAAA;EAAO,KAAA;AAAA;EAC/B,MAAA;EAAiB,KAAA,EAAO,KAAA;EAAmB,KAAA;AAAA;AAAA,KAErC,WAAA;AAAA,KAEA,WAAA,cAAyB,MAAA,SAAe,IAAA;EAClD,GAAA,mBAAsB,IAAA,EAAM,GAAA,EAAK,CAAA,KAAM,OAAA,CAAQ,SAAA,CAAU,IAAA,CAAK,CAAA;EAC9D,MAAA,EAAQ,WAAA;AAAA;AAAA,UAGO,mBAAA,cACF,MAAA,SAAe,IAAA,2BAEpB,WAAA,CAAY,IAAA;EACpB,kBAAA,CAAmB,KAAA,EAAO,KAAA;EAC1B,kBAAA,CAAmB,MAAA,GAAS,IAAA,EAAM,KAAA,iBAAsB,KAAA;AAAA;AAAA,KAG9C,UAAA,qBAEG,MAAA,SAAe,IAAA;EAG5B,YAAA,GAAe,IAAA;EACf,WAAA,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AAA;EACzD,UAAA,CAAW,IAAA,EAAM,IAAA;EACjB,cAAA,CAAe,KAAA,EAAO,aAAA,CAAc,IAAA;EACpC,SAAA,IAAa,IAAA,EAAM,IAAA,CAAK,CAAA,GAAI,QAAA,GAAW,KAAA,EAAO,SAAA,CAAU,CAAA;AAAA;AAAA,cAS7C,KAAA;EAAA;EA0BX,OAAA,GAAA,CAAW,IAAA,EAAM,IAAA,CAAK,CAAA,IAAK,OAAA,CAAQ,CAAA;EAsDnC,UAAA,CAAW,IAAA,EAAM,IAAA;EAMjB,cAAA,CAAe,KAAA,EAAO,aAAA,CAAc,IAAA;EAkHpC,SAAA,GAAA,CAAa,IAAA,EAAM,IAAA,CAAK,CAAA,GAAI,QAAA;EAmD5B,WAAA,GAAA,CAAe,IAAA,EAAM,IAAA,CAAK,CAAA,IAAK,SAAA,CAAU,CAAA;EAIzC,iBAAA,GAAA,CAAqB,IAAA,EAAM,IAAA,CAAK,CAAA,IAAK,SAAA,CAAU,CAAA;EAQzC,GAAA,iCAAA,CAAqC,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,MAAO,IAAA,EAAM,CAAA,GAAI,OAAA;EAiDlF,KAAA,GAAA,CAAS,IAAA,EAAM,IAAA,CAAK,CAAA,GAAI,KAAA,EAAO,CAAA;EAC/B,KAAA,GAAA,CAAS,IAAA,EAAM,IAAA,CAAK,CAAA,GAAI,MAAA,GAAS,IAAA,EAAM,CAAA,iBAAkB,CAAA;EAezD,SAAA,CAAA,GAAa,WAAA;AAAA;AAAA,iBAsBC,WAAA,CAAA,GAAe,KAAA"}
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
const CONFIG = Symbol("kvark.config");
|
|
3
|
-
const WRITABLE = Symbol("kvark.writable");
|
|
4
|
-
const FAMILY_LINK = Symbol("kvark.familyLink");
|
|
5
|
-
//#endregion
|
|
1
|
+
import { n as FAMILY_LINK, r as WRITABLE, t as CONFIG } from "./types-B2mzc3A5.js";
|
|
6
2
|
//#region src/internal/atom.ts
|
|
7
3
|
function atom(config) {
|
|
8
4
|
const internal = Object.create(null);
|
|
@@ -23,15 +19,113 @@ function atom(config) {
|
|
|
23
19
|
};
|
|
24
20
|
}
|
|
25
21
|
//#endregion
|
|
22
|
+
//#region src/internal/lru-cache.ts
|
|
23
|
+
/**
|
|
24
|
+
* Fixed-capacity cache with O(1) get, set, delete and LRU eviction.
|
|
25
|
+
*
|
|
26
|
+
* Pass `maxSize: Infinity` to disable eviction (keep-all semantics).
|
|
27
|
+
*
|
|
28
|
+
* LRU end (oldest): head.next — MRU end (newest): tail.prev.
|
|
29
|
+
*/
|
|
30
|
+
var LruCache = class {
|
|
31
|
+
#maxSize;
|
|
32
|
+
#nodeMap = /* @__PURE__ */ new Map();
|
|
33
|
+
#valueMap = /* @__PURE__ */ new Map();
|
|
34
|
+
#head;
|
|
35
|
+
#tail;
|
|
36
|
+
constructor(maxSize) {
|
|
37
|
+
this.#maxSize = maxSize;
|
|
38
|
+
this.#head = {
|
|
39
|
+
key: void 0,
|
|
40
|
+
prev: null,
|
|
41
|
+
next: null
|
|
42
|
+
};
|
|
43
|
+
this.#tail = {
|
|
44
|
+
key: void 0,
|
|
45
|
+
prev: this.#head,
|
|
46
|
+
next: null
|
|
47
|
+
};
|
|
48
|
+
this.#head.next = this.#tail;
|
|
49
|
+
}
|
|
50
|
+
/** Returns the value and marks the entry as most-recently used. */
|
|
51
|
+
get(key) {
|
|
52
|
+
const node = this.#nodeMap.get(key);
|
|
53
|
+
if (node == null) return;
|
|
54
|
+
this.#moveToMru(node);
|
|
55
|
+
return this.#valueMap.get(key);
|
|
56
|
+
}
|
|
57
|
+
/** Returns the value without affecting LRU order. */
|
|
58
|
+
peek(key) {
|
|
59
|
+
return this.#valueMap.get(key);
|
|
60
|
+
}
|
|
61
|
+
/** Inserts or updates a value and evicts the LRU entry if over capacity. */
|
|
62
|
+
set(key, value) {
|
|
63
|
+
const existing = this.#nodeMap.get(key);
|
|
64
|
+
if (existing != null) {
|
|
65
|
+
this.#valueMap.set(key, value);
|
|
66
|
+
this.#moveToMru(existing);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const node = {
|
|
70
|
+
key,
|
|
71
|
+
prev: null,
|
|
72
|
+
next: null
|
|
73
|
+
};
|
|
74
|
+
this.#nodeMap.set(key, node);
|
|
75
|
+
this.#valueMap.set(key, value);
|
|
76
|
+
this.#insertBeforeTail(node);
|
|
77
|
+
this.#evict();
|
|
78
|
+
}
|
|
79
|
+
delete(key) {
|
|
80
|
+
const node = this.#nodeMap.get(key);
|
|
81
|
+
if (node == null) return;
|
|
82
|
+
this.#detach(node);
|
|
83
|
+
this.#nodeMap.delete(key);
|
|
84
|
+
this.#valueMap.delete(key);
|
|
85
|
+
}
|
|
86
|
+
values() {
|
|
87
|
+
return this.#valueMap.values();
|
|
88
|
+
}
|
|
89
|
+
/** Returns a live read-only view of the underlying map. */
|
|
90
|
+
asReadonlyMap() {
|
|
91
|
+
return this.#valueMap;
|
|
92
|
+
}
|
|
93
|
+
#moveToMru(node) {
|
|
94
|
+
this.#detach(node);
|
|
95
|
+
this.#insertBeforeTail(node);
|
|
96
|
+
}
|
|
97
|
+
#detach(node) {
|
|
98
|
+
const { prev, next } = node;
|
|
99
|
+
if (prev == null || next == null) return;
|
|
100
|
+
prev.next = next;
|
|
101
|
+
next.prev = prev;
|
|
102
|
+
}
|
|
103
|
+
#insertBeforeTail(node) {
|
|
104
|
+
const before = this.#tail.prev;
|
|
105
|
+
if (before == null) return;
|
|
106
|
+
before.next = node;
|
|
107
|
+
node.prev = before;
|
|
108
|
+
node.next = this.#tail;
|
|
109
|
+
this.#tail.prev = node;
|
|
110
|
+
}
|
|
111
|
+
#evict() {
|
|
112
|
+
if (!Number.isFinite(this.#maxSize)) return;
|
|
113
|
+
while (this.#nodeMap.size > this.#maxSize) {
|
|
114
|
+
const oldest = this.#head.next;
|
|
115
|
+
if (oldest == null) break;
|
|
116
|
+
this.#detach(oldest);
|
|
117
|
+
this.#nodeMap.delete(oldest.key);
|
|
118
|
+
this.#valueMap.delete(oldest.key);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
//#endregion
|
|
26
123
|
//#region src/internal/family.ts
|
|
27
124
|
function getFamilyLink(target) {
|
|
28
125
|
return target[FAMILY_LINK];
|
|
29
126
|
}
|
|
30
127
|
function atomFamily(options) {
|
|
31
|
-
const
|
|
32
|
-
const lruSize = options.lruSize ?? 100;
|
|
33
|
-
const cache = /* @__PURE__ */ new Map();
|
|
34
|
-
const lruOrder = [];
|
|
128
|
+
const cache = new LruCache(options.cachePolicy === "lru" ? options.lruSize ?? 100 : Infinity);
|
|
35
129
|
const storeCallbacks = /* @__PURE__ */ new Set();
|
|
36
130
|
function keyOf(param) {
|
|
37
131
|
return options.paramKey != null ? options.paramKey(param) : param;
|
|
@@ -53,10 +147,7 @@ function atomFamily(options) {
|
|
|
53
147
|
function getOrCreate(param) {
|
|
54
148
|
const key = keyOf(param);
|
|
55
149
|
const cached = cache.get(key);
|
|
56
|
-
if (cached != null)
|
|
57
|
-
if (cachePolicy === "lru") touchLru(key);
|
|
58
|
-
return cached;
|
|
59
|
-
}
|
|
150
|
+
if (cached != null) return cached;
|
|
60
151
|
const deps = options.dependencies?.(param);
|
|
61
152
|
const getFn = options.get(param);
|
|
62
153
|
const setFn = options.set?.(param);
|
|
@@ -64,10 +155,6 @@ function atomFamily(options) {
|
|
|
64
155
|
const created = createAtom(options.debugLabel != null ? `${options.debugLabel}(${labelSuffix})` : void 0, deps, getFn, setFn);
|
|
65
156
|
attachLink(created);
|
|
66
157
|
cache.set(key, created);
|
|
67
|
-
if (cachePolicy === "lru") {
|
|
68
|
-
lruOrder.push(key);
|
|
69
|
-
evictLru();
|
|
70
|
-
}
|
|
71
158
|
return created;
|
|
72
159
|
}
|
|
73
160
|
function createAtom(label, deps, getFn, setFn) {
|
|
@@ -81,35 +168,18 @@ function atomFamily(options) {
|
|
|
81
168
|
}
|
|
82
169
|
return atom(base);
|
|
83
170
|
}
|
|
84
|
-
function touchLru(key) {
|
|
85
|
-
const idx = lruOrder.indexOf(key);
|
|
86
|
-
if (idx !== -1) lruOrder.splice(idx, 1);
|
|
87
|
-
lruOrder.push(key);
|
|
88
|
-
}
|
|
89
|
-
function evictLru() {
|
|
90
|
-
while (lruOrder.length > lruSize) {
|
|
91
|
-
const evicted = lruOrder.shift();
|
|
92
|
-
if (evicted !== void 0) cache.delete(evicted);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
171
|
const family = getOrCreate;
|
|
96
172
|
family.invalidate = (param) => {
|
|
97
|
-
const
|
|
98
|
-
const cached = cache.get(key);
|
|
173
|
+
const cached = cache.peek(keyOf(param));
|
|
99
174
|
if (cached != null) link.invalidateAtom(cached);
|
|
100
175
|
};
|
|
101
176
|
family.invalidateAll = () => {
|
|
102
177
|
for (const cached of cache.values()) link.invalidateAtom(cached);
|
|
103
178
|
};
|
|
104
179
|
family.remove = (param) => {
|
|
105
|
-
|
|
106
|
-
cache.delete(key);
|
|
107
|
-
if (cachePolicy === "lru") {
|
|
108
|
-
const idx = lruOrder.indexOf(key);
|
|
109
|
-
if (idx !== -1) lruOrder.splice(idx, 1);
|
|
110
|
-
}
|
|
180
|
+
cache.delete(keyOf(param));
|
|
111
181
|
};
|
|
112
|
-
family.getCache = () => cache;
|
|
182
|
+
family.getCache = () => cache.asReadonlyMap();
|
|
113
183
|
return family;
|
|
114
184
|
}
|
|
115
185
|
//#endregion
|
|
@@ -127,6 +197,7 @@ var Store = class {
|
|
|
127
197
|
#familyUnsubs = /* @__PURE__ */ new Set();
|
|
128
198
|
#registeredFamilies = /* @__PURE__ */ new WeakSet();
|
|
129
199
|
#client = null;
|
|
200
|
+
#flushScheduled = false;
|
|
130
201
|
#getOrCreate(atom) {
|
|
131
202
|
let entry = this.#atoms.get(atom);
|
|
132
203
|
if (entry == null) {
|
|
@@ -189,18 +260,14 @@ var Store = class {
|
|
|
189
260
|
invalidate(atom) {
|
|
190
261
|
this.#markStale(atom);
|
|
191
262
|
this.#pending.add(atom);
|
|
192
|
-
|
|
193
|
-
this.#flushPending();
|
|
194
|
-
});
|
|
263
|
+
this.#schedulePendingFlush();
|
|
195
264
|
}
|
|
196
265
|
invalidateMany(atoms) {
|
|
197
266
|
for (const a of atoms) {
|
|
198
267
|
this.#markStale(a);
|
|
199
268
|
this.#pending.add(a);
|
|
200
269
|
}
|
|
201
|
-
if (this.#pending.size > 0)
|
|
202
|
-
this.#flushPending();
|
|
203
|
-
});
|
|
270
|
+
if (this.#pending.size > 0) this.#schedulePendingFlush();
|
|
204
271
|
}
|
|
205
272
|
#markStale(atom, visited = /* @__PURE__ */ new Set()) {
|
|
206
273
|
if (visited.has(atom)) return;
|
|
@@ -239,11 +306,18 @@ var Store = class {
|
|
|
239
306
|
}
|
|
240
307
|
for (const l of toNotify) l();
|
|
241
308
|
}
|
|
309
|
+
#schedulePendingFlush() {
|
|
310
|
+
if (!this.#flushScheduled) {
|
|
311
|
+
this.#flushScheduled = true;
|
|
312
|
+
queueMicrotask(() => {
|
|
313
|
+
this.#flushScheduled = false;
|
|
314
|
+
this.#flushPending();
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
242
318
|
#scheduleNotify(atom) {
|
|
243
319
|
this.#pending.add(atom);
|
|
244
|
-
|
|
245
|
-
this.#flushPending();
|
|
246
|
-
});
|
|
320
|
+
this.#schedulePendingFlush();
|
|
247
321
|
}
|
|
248
322
|
#registerRdeps(atom, deps) {
|
|
249
323
|
for (const dep of Object.values(deps)) {
|
|
@@ -393,6 +467,6 @@ function extractPreviousValue(state) {
|
|
|
393
467
|
if (state.status === "error") return state.value;
|
|
394
468
|
}
|
|
395
469
|
//#endregion
|
|
396
|
-
export {
|
|
470
|
+
export { atomFamily as n, atom as r, createStore as t };
|
|
397
471
|
|
|
398
|
-
//# sourceMappingURL=store-
|
|
472
|
+
//# sourceMappingURL=store-DuKg4_ng.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-DuKg4_ng.js","names":["#maxSize","#nodeMap","#valueMap","#head","#tail","#moveToMru","#insertBeforeTail","#evict","#detach","#atoms","#rdeps","#pending","#controllers","#familyUnsubs","#registeredFamilies","#getOrCreate","#runGet","#registerRdeps","#makeCtx","#scheduleNotify","#markStale","#schedulePendingFlush","#flushScheduled","#flushPending","#autoRegisterFamily","#markReverseDependentsStale","#client"],"sources":["../src/internal/atom.ts","../src/internal/lru-cache.ts","../src/internal/family.ts","../src/internal/store.ts"],"sourcesContent":["import type {\n Atom,\n AtomConfig,\n InternalAtomConfig,\n WritableAtom,\n WritableAtomContext,\n} from \"@/internal/types.js\";\nimport { CONFIG, WRITABLE } from \"@/internal/types.js\";\n\ntype WritableConfig<\n Value,\n Deps extends Record<string, Atom<unknown>>,\n Args extends readonly unknown[],\n> = AtomConfig<Value, Deps, Args> & {\n set: (ctx: WritableAtomContext<Deps, Value>, ...args: Args) => Promise<void>;\n};\n\ntype ReadonlyConfig<Value, Deps extends Record<string, Atom<unknown>>> = Omit<\n AtomConfig<Value, Deps, readonly []>,\n \"set\"\n>;\n\nexport function atom<\n Value,\n Deps extends Record<string, Atom<unknown>> = Record<never, never>,\n Args extends readonly unknown[] = readonly [],\n>(config: WritableConfig<Value, Deps, Args>): WritableAtom<Value, Args>;\n\nexport function atom<Value, Deps extends Record<string, Atom<unknown>> = Record<never, never>>(\n config: ReadonlyConfig<Value, Deps>,\n): Atom<Value>;\n\nexport function atom<\n Value,\n Deps extends Record<string, Atom<unknown>> = Record<never, never>,\n Args extends readonly unknown[] = readonly [],\n>(config: AtomConfig<Value, Deps, Args>): Atom<Value> {\n const internal: InternalAtomConfig<Value> = Object.create(null) as InternalAtomConfig<Value>;\n internal.get = config.get as InternalAtomConfig<Value>[\"get\"];\n\n if (config.debugLabel != null) {\n internal.debugLabel = config.debugLabel;\n }\n if (config.dependencies != null) {\n internal.dependencies = config.dependencies as Record<string, Atom<unknown>>;\n }\n if (config.stalePolicy != null) {\n internal.stalePolicy = config.stalePolicy;\n }\n if (config.set != null) {\n internal.set = config.set as NonNullable<InternalAtomConfig<Value>[\"set\"]>;\n }\n if (config.onMount != null) {\n internal.onMount = config.onMount as NonNullable<InternalAtomConfig<Value>[\"onMount\"]>;\n }\n\n if (config.set != null) {\n return {\n [CONFIG]: internal,\n [WRITABLE]: [] as unknown as Args,\n debugLabel: config.debugLabel,\n } as unknown as Atom<Value>;\n }\n\n return {\n [CONFIG]: internal,\n debugLabel: config.debugLabel,\n } as Atom<Value>;\n}\n","type LruNode<K> = {\n key: K;\n prev: LruNode<K> | null;\n next: LruNode<K> | null;\n};\n\n/**\n * Fixed-capacity cache with O(1) get, set, delete and LRU eviction.\n *\n * Pass `maxSize: Infinity` to disable eviction (keep-all semantics).\n *\n * LRU end (oldest): head.next — MRU end (newest): tail.prev.\n */\nexport class LruCache<K, V> {\n readonly #maxSize: number;\n readonly #nodeMap = new Map<K, LruNode<K>>();\n readonly #valueMap = new Map<K, V>();\n readonly #head: LruNode<K>;\n readonly #tail: LruNode<K>;\n\n constructor(maxSize: number) {\n this.#maxSize = maxSize;\n this.#head = { key: undefined as unknown as K, prev: null, next: null };\n this.#tail = { key: undefined as unknown as K, prev: this.#head, next: null };\n this.#head.next = this.#tail;\n }\n\n /** Returns the value and marks the entry as most-recently used. */\n get(key: K): V | undefined {\n const node = this.#nodeMap.get(key);\n if (node == null) {\n return undefined;\n }\n this.#moveToMru(node);\n return this.#valueMap.get(key);\n }\n\n /** Returns the value without affecting LRU order. */\n peek(key: K): V | undefined {\n return this.#valueMap.get(key);\n }\n\n /** Inserts or updates a value and evicts the LRU entry if over capacity. */\n set(key: K, value: V): void {\n const existing = this.#nodeMap.get(key);\n if (existing != null) {\n this.#valueMap.set(key, value);\n this.#moveToMru(existing);\n return;\n }\n\n const node: LruNode<K> = { key, prev: null, next: null };\n this.#nodeMap.set(key, node);\n this.#valueMap.set(key, value);\n this.#insertBeforeTail(node);\n this.#evict();\n }\n\n delete(key: K): void {\n const node = this.#nodeMap.get(key);\n if (node == null) {\n return;\n }\n this.#detach(node);\n this.#nodeMap.delete(key);\n this.#valueMap.delete(key);\n }\n\n values(): IterableIterator<V> {\n return this.#valueMap.values();\n }\n\n /** Returns a live read-only view of the underlying map. */\n asReadonlyMap(): ReadonlyMap<K, V> {\n return this.#valueMap;\n }\n\n #moveToMru(node: LruNode<K>): void {\n this.#detach(node);\n this.#insertBeforeTail(node);\n }\n\n #detach(node: LruNode<K>): void {\n const { prev, next } = node;\n if (prev == null || next == null) {\n return;\n }\n prev.next = next;\n next.prev = prev;\n }\n\n #insertBeforeTail(node: LruNode<K>): void {\n const before = this.#tail.prev;\n if (before == null) {\n return;\n }\n before.next = node;\n node.prev = before;\n node.next = this.#tail;\n this.#tail.prev = node;\n }\n\n #evict(): void {\n if (!Number.isFinite(this.#maxSize)) {\n return;\n }\n while (this.#nodeMap.size > this.#maxSize) {\n const oldest = this.#head.next;\n if (oldest == null) {\n break;\n }\n this.#detach(oldest);\n this.#nodeMap.delete(oldest.key);\n this.#valueMap.delete(oldest.key);\n }\n }\n}\n","import type { Atom, AtomContext, StalePolicy } from \"@/internal/types.js\";\nimport { FAMILY_LINK } from \"@/internal/types.js\";\nimport { atom } from \"@/internal/atom.js\";\nimport { LruCache } from \"@/internal/lru-cache.js\";\n\nexport type AtomFamilyOptions<\n Param,\n Value,\n Deps extends Record<string, Atom<unknown>>,\n Args extends readonly unknown[],\n Key = Param,\n> = {\n dependencies?: (param: Param) => Deps;\n stalePolicy?: StalePolicy;\n cachePolicy?: \"keep-all\" | \"lru\";\n lruSize?: number;\n debugLabel?: string;\n /**\n * Optional function to derive a stable cache key from `param`.\n * Use when `Param` is an object and you want equality-by-value semantics.\n * The returned key is used for all cache operations (get, set, LRU, invalidate, remove).\n * The original `param` is still passed to `get`, `set`, and `dependencies`.\n *\n * For plain objects/arrays you can use `stableFamilyKey` from `@kdeveloper/kvark/family`.\n * Treat the param object as immutable after the first call — `stableFamilyKey` memoises\n * by object reference via a WeakMap.\n */\n paramKey?: (param: Param) => Key;\n get: (param: Param) => (ctx: AtomContext<Deps>) => Promise<Value>;\n set?: (param: Param) => (ctx: AtomContext<Deps>, ...args: Args) => Promise<void>;\n};\n\nexport interface AtomFamily<Param, Value, Key = Param> {\n (param: Param): Atom<Value>;\n invalidate(param: Param): void;\n invalidateAll(): void;\n remove(param: Param): void;\n getCache(): ReadonlyMap<Key, Atom<Value>>;\n}\n\nexport interface FamilyLink {\n invalidateAtom: (atom: Atom<unknown>) => void;\n registerStore: (cb: (atom: Atom<unknown>) => void) => () => void;\n}\n\nexport function getFamilyLink(target: Atom<unknown>): FamilyLink | undefined {\n return (target as unknown as Record<symbol, FamilyLink | undefined>)[FAMILY_LINK];\n}\n\nexport function atomFamily<\n Param,\n Value,\n Deps extends Record<string, Atom<unknown>> = Record<never, never>,\n Args extends readonly unknown[] = readonly [],\n Key = Param,\n>(options: AtomFamilyOptions<Param, Value, Deps, Args, Key>): AtomFamily<Param, Value, Key> {\n const maxSize = options.cachePolicy === \"lru\" ? (options.lruSize ?? 100) : Infinity;\n const cache = new LruCache<Key, Atom<Value>>(maxSize);\n const storeCallbacks = new Set<(atom: Atom<unknown>) => void>();\n\n function keyOf(param: Param): Key {\n return options.paramKey != null ? options.paramKey(param) : (param as unknown as Key);\n }\n\n const link: FamilyLink = {\n invalidateAtom(target: Atom<unknown>): void {\n for (const cb of storeCallbacks) {\n cb(target);\n }\n },\n registerStore(cb: (atom: Atom<unknown>) => void): () => void {\n storeCallbacks.add(cb);\n return () => {\n storeCallbacks.delete(cb);\n };\n },\n };\n\n function attachLink(target: Atom<Value>): void {\n (target as unknown as Record<symbol, FamilyLink>)[FAMILY_LINK] = link;\n }\n\n function getOrCreate(param: Param): Atom<Value> {\n const key = keyOf(param);\n const cached = cache.get(key);\n if (cached != null) {\n return cached;\n }\n\n const deps = options.dependencies?.(param);\n const getFn = options.get(param);\n const setFn = options.set?.(param);\n\n const labelSuffix = options.paramKey != null ? String(key) : String(param);\n const label = options.debugLabel != null ? `${options.debugLabel}(${labelSuffix})` : undefined;\n\n const created = createAtom(label, deps, getFn, setFn);\n attachLink(created);\n cache.set(key, created);\n\n return created;\n }\n\n function createAtom(\n label: string | undefined,\n deps: Deps | undefined,\n getFn: (ctx: AtomContext<Deps>) => Promise<Value>,\n setFn: ((ctx: AtomContext<Deps>, ...args: Args) => Promise<void>) | undefined,\n ): Atom<Value> {\n const base = {\n get: getFn as (ctx: AtomContext<Deps>) => Promise<Value>,\n } as {\n debugLabel?: string;\n dependencies?: Deps;\n stalePolicy?: StalePolicy;\n get: (ctx: AtomContext<Deps>) => Promise<Value>;\n set?: (ctx: AtomContext<Deps>, ...args: Args) => Promise<void>;\n };\n\n if (label != null) {\n base.debugLabel = label;\n }\n if (deps != null) {\n base.dependencies = deps;\n }\n if (options.stalePolicy != null) {\n base.stalePolicy = options.stalePolicy;\n }\n\n if (setFn != null) {\n base.set = setFn;\n return atom(\n base as typeof base & {\n set: (ctx: AtomContext<Deps>, ...args: Args) => Promise<void>;\n },\n ) as Atom<Value>;\n }\n\n return atom(base as Omit<typeof base, \"set\">);\n }\n\n const family = getOrCreate as AtomFamily<Param, Value, Key>;\n\n family.invalidate = (param: Param): void => {\n const cached = cache.peek(keyOf(param));\n if (cached != null) {\n link.invalidateAtom(cached);\n }\n };\n\n family.invalidateAll = (): void => {\n for (const cached of cache.values()) {\n link.invalidateAtom(cached);\n }\n };\n\n family.remove = (param: Param): void => {\n cache.delete(keyOf(param));\n };\n\n family.getCache = (): ReadonlyMap<Key, Atom<Value>> => cache.asReadonlyMap();\n\n return family;\n}\n","import type {\n Atom,\n AtomContext,\n AtomState,\n InternalAtomConfig,\n StalePolicy,\n WritableAtom,\n WritableAtomContext,\n} from \"@/internal/types.js\";\nimport { CONFIG } from \"@/internal/types.js\";\nimport { getFamilyLink } from \"@/internal/family.js\";\n\ntype AtomEntry<V> = {\n state: AtomState<V>;\n version: number;\n promise: Promise<V> | null;\n listeners: Set<() => void>;\n mountCount: number;\n unmount: (() => void) | null;\n};\n\nexport interface StoreClient {\n get<V>(atom: Atom<V>): Promise<V>;\n set<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>, ...args: A): Promise<void>;\n write<V>(atom: Atom<V>, value: V): void;\n write<V>(atom: Atom<V>, mutate: (prev: V | undefined) => V): void;\n invalidate(atom: Atom<unknown>): void;\n invalidateMany(atoms: ReadonlyArray<Atom<unknown>>): void;\n subscribe<V>(atom: Atom<V>, listener: (state: AtomState<V>) => void): () => void;\n}\n\nconst PENDING_STATE: AtomState<never> = {\n status: \"pending\",\n value: undefined,\n error: undefined,\n} as AtomState<never>;\n\nexport class Store {\n readonly #atoms = new WeakMap<Atom<unknown>, AtomEntry<unknown>>();\n readonly #rdeps = new Map<Atom<unknown>, Set<Atom<unknown>>>();\n readonly #pending = new Set<Atom<unknown>>();\n readonly #controllers = new WeakMap<Atom<unknown>, AbortController>();\n readonly #familyUnsubs = new Set<() => void>();\n readonly #registeredFamilies = new WeakSet<object>();\n #client: StoreClient | null = null;\n #flushScheduled = false;\n\n #getOrCreate<V>(atom: Atom<V>): AtomEntry<V> {\n let entry = this.#atoms.get(atom) as AtomEntry<V> | undefined;\n if (entry == null) {\n entry = {\n state: PENDING_STATE as AtomState<V>,\n version: 0,\n promise: null,\n listeners: new Set(),\n mountCount: 0,\n unmount: null,\n };\n this.#atoms.set(atom, entry as AtomEntry<unknown>);\n }\n return entry;\n }\n\n resolve<V>(atom: Atom<V>): Promise<V> {\n const entry = this.#getOrCreate(atom);\n if (entry.promise != null) {\n return entry.promise;\n }\n\n const promise = this.#runGet(atom).finally(() => {\n const e = this.#atoms.get(atom) as AtomEntry<V> | undefined;\n if (e != null) {\n e.promise = null;\n }\n });\n entry.promise = promise;\n return promise;\n }\n\n async #runGet<V>(atom: Atom<V>): Promise<V> {\n const config: InternalAtomConfig<V> = atom[CONFIG];\n const dependencies = config.dependencies;\n const stalePolicy: StalePolicy = config.stalePolicy ?? \"keep\";\n const entry = this.#getOrCreate(atom);\n\n if (dependencies != null) {\n await Promise.all(Object.values(dependencies).map((dep) => this.resolve(dep)));\n this.#registerRdeps(atom, dependencies);\n }\n\n const ctx = this.#makeCtx(atom, dependencies ?? {});\n\n try {\n const value = await config.get(ctx);\n if (ctx.signal.aborted) {\n throw new DOMException(\"The operation was aborted\", \"AbortError\");\n }\n entry.state = { status: \"fresh\", value, error: undefined };\n entry.version++;\n this.#scheduleNotify(atom);\n return value;\n } catch (e: unknown) {\n if (isAbortError(e)) {\n throw e;\n }\n const prevValue = extractPreviousValue<V>(entry.state);\n entry.state = {\n status: \"error\",\n value: stalePolicy === \"keep\" ? prevValue : undefined,\n error: e,\n } as AtomState<V>;\n entry.version++;\n this.#scheduleNotify(atom);\n throw e;\n }\n }\n\n invalidate(atom: Atom<unknown>): void {\n this.#markStale(atom);\n this.#pending.add(atom);\n this.#schedulePendingFlush();\n }\n\n invalidateMany(atoms: ReadonlyArray<Atom<unknown>>): void {\n for (const a of atoms) {\n this.#markStale(a);\n this.#pending.add(a);\n }\n if (this.#pending.size > 0) {\n this.#schedulePendingFlush();\n }\n }\n\n #markStale(atom: Atom<unknown>, visited = new Set<Atom<unknown>>()): void {\n if (visited.has(atom)) {\n return;\n }\n visited.add(atom);\n\n const entry = this.#atoms.get(atom);\n if (entry != null) {\n const stalePolicy: StalePolicy = atom[CONFIG].stalePolicy ?? \"keep\";\n\n if (entry.state.status === \"fresh\") {\n if (stalePolicy === \"reset\") {\n entry.state = PENDING_STATE;\n } else {\n entry.state = { ...entry.state, status: \"stale\" };\n }\n }\n\n entry.promise = null;\n this.#controllers.get(atom)?.abort();\n }\n\n const rdeps = this.#rdeps.get(atom);\n if (rdeps != null) {\n for (const dep of rdeps) {\n this.#markStale(dep, visited);\n }\n }\n }\n\n #markReverseDependentsStale(atom: Atom<unknown>): void {\n const rdeps = this.#rdeps.get(atom);\n if (rdeps != null) {\n const visited = new Set<Atom<unknown>>([atom]);\n for (const dep of rdeps) {\n this.#markStale(dep, visited);\n this.#pending.add(dep);\n }\n }\n }\n\n #flushPending(): void {\n const batch = [...this.#pending];\n this.#pending.clear();\n const toNotify = new Set<() => void>();\n for (const a of batch) {\n const entry = this.#atoms.get(a);\n if (entry != null) {\n for (const l of entry.listeners) {\n toNotify.add(l);\n }\n }\n }\n for (const l of toNotify) {\n l();\n }\n }\n\n #schedulePendingFlush(): void {\n if (!this.#flushScheduled) {\n this.#flushScheduled = true;\n queueMicrotask(() => {\n this.#flushScheduled = false;\n this.#flushPending();\n });\n }\n }\n\n #scheduleNotify(atom: Atom<unknown>): void {\n this.#pending.add(atom);\n this.#schedulePendingFlush();\n }\n\n #registerRdeps(atom: Atom<unknown>, deps: Record<string, Atom<unknown>>): void {\n for (const dep of Object.values(deps)) {\n let set = this.#rdeps.get(dep);\n if (set == null) {\n set = new Set();\n this.#rdeps.set(dep, set);\n }\n set.add(atom);\n }\n }\n\n #makeCtx(\n atom: Atom<unknown>,\n deps: Record<string, Atom<unknown>>,\n ): AtomContext<Record<string, Atom<unknown>>> {\n this.#controllers.get(atom)?.abort();\n const controller = new AbortController();\n this.#controllers.set(atom, controller);\n\n return {\n signal: controller.signal,\n get: async (key: string) => {\n const dep = deps[key];\n if (dep == null) {\n throw new Error(`Unknown dependency key: \"${String(key)}\"`);\n }\n return this.resolve(dep);\n },\n };\n }\n\n subscribe<V>(atom: Atom<V>, listener: () => void): () => void {\n const entry = this.#getOrCreate(atom);\n entry.listeners.add(listener);\n\n this.#autoRegisterFamily(atom);\n\n if (entry.state.status === \"pending\" && entry.promise == null) {\n void this.resolve(atom);\n }\n\n entry.mountCount++;\n if (entry.mountCount === 1) {\n const config: InternalAtomConfig<V> = atom[CONFIG];\n if (config.onMount != null) {\n const setValue = (value: V): void => {\n entry.state = { status: \"fresh\", value, error: undefined };\n entry.version++;\n this.#scheduleNotify(atom);\n };\n const cleanup = config.onMount(setValue as (value: unknown) => void);\n if (typeof cleanup === \"function\") {\n entry.unmount = cleanup;\n }\n }\n }\n\n return () => {\n entry.listeners.delete(listener);\n entry.mountCount--;\n if (entry.mountCount === 0 && entry.unmount != null) {\n entry.unmount();\n entry.unmount = null;\n }\n };\n }\n\n #autoRegisterFamily(atom: Atom<unknown>): void {\n const link = getFamilyLink(atom);\n if (link == null) {\n return;\n }\n if (this.#registeredFamilies.has(link)) {\n return;\n }\n this.#registeredFamilies.add(link);\n const unsub = link.registerStore((target: Atom<unknown>) => {\n this.invalidate(target);\n });\n this.#familyUnsubs.add(unsub);\n }\n\n getSnapshot<V>(atom: Atom<V>): AtomState<V> {\n return this.#getOrCreate(atom).state;\n }\n\n getServerSnapshot<V>(atom: Atom<V>): AtomState<V> {\n const entry = this.#atoms.get(atom) as AtomEntry<V> | undefined;\n if (entry != null) {\n return entry.state;\n }\n return PENDING_STATE as AtomState<V>;\n }\n\n async set<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>, ...args: A): Promise<void> {\n const config: InternalAtomConfig<V> = atom[CONFIG];\n if (config.set == null) {\n throw new Error(\n `Atom${atom.debugLabel != null ? ` \"${atom.debugLabel}\"` : \"\"} is not writable`,\n );\n }\n\n const deps = config.dependencies ?? {};\n const baseCtx = this.#makeCtx(atom, deps);\n const entry = this.#getOrCreate(atom);\n\n // Ref: assignments inside setOptimisticValue are not tracked on a plain `let` for catch narrowing.\n const optimisticSnapshot: {\n current: { state: AtomState<V>; version: number } | null;\n } = { current: null };\n\n const ctx: WritableAtomContext<Record<string, Atom<unknown>>, V> = {\n ...baseCtx,\n setOptimisticValue: (valueOrMutate: V | ((prev: V | undefined) => V)): void => {\n if (optimisticSnapshot.current == null) {\n optimisticSnapshot.current = { state: entry.state, version: entry.version };\n }\n const next =\n typeof valueOrMutate === \"function\"\n ? (valueOrMutate as (prev: V | undefined) => V)(extractPreviousValue(entry.state))\n : valueOrMutate;\n entry.state = { status: \"fresh\", value: next, error: undefined };\n entry.version++;\n this.#scheduleNotify(atom);\n this.#markReverseDependentsStale(atom);\n },\n };\n\n try {\n await config.set(ctx, ...args);\n } catch (e: unknown) {\n const snap = optimisticSnapshot.current;\n if (snap != null) {\n entry.state = snap.state;\n entry.version = snap.version;\n this.#scheduleNotify(atom);\n }\n throw e;\n }\n\n this.invalidate(atom);\n }\n\n write<V>(atom: Atom<V>, value: V): void;\n write<V>(atom: Atom<V>, mutate: (prev: V | undefined) => V): void;\n write<V>(atom: Atom<V>, valueOrMutate: V | ((prev: V | undefined) => V)): void {\n const entry = this.#getOrCreate(atom);\n this.#controllers.get(atom)?.abort();\n entry.promise = null;\n const next =\n typeof valueOrMutate === \"function\"\n ? (valueOrMutate as (prev: V | undefined) => V)(extractPreviousValue(entry.state))\n : valueOrMutate;\n entry.state = { status: \"fresh\", value: next, error: undefined };\n entry.version++;\n this.#scheduleNotify(atom);\n this.#markReverseDependentsStale(atom);\n }\n\n getClient(): StoreClient {\n if (this.#client != null) {\n return this.#client;\n }\n\n this.#client = {\n get: <V>(atom: Atom<V>): Promise<V> => this.resolve(atom),\n set: <V, A extends readonly unknown[]>(atom: WritableAtom<V, A>, ...args: A): Promise<void> =>\n this.set(atom, ...args),\n write: <V>(atom: Atom<V>, valueOrMutate: V | ((prev: V | undefined) => V)): void =>\n this.write(atom, valueOrMutate as V),\n invalidate: (atom: Atom<unknown>): void => this.invalidate(atom),\n invalidateMany: (atoms: ReadonlyArray<Atom<unknown>>): void => this.invalidateMany(atoms),\n subscribe: <V>(atom: Atom<V>, listener: (state: AtomState<V>) => void): (() => void) =>\n this.subscribe(atom, () => {\n listener(this.getSnapshot(atom));\n }),\n };\n return this.#client;\n }\n}\n\nexport function createStore(): Store {\n return new Store();\n}\n\nfunction isAbortError(e: unknown): boolean {\n return e instanceof DOMException && e.name === \"AbortError\";\n}\n\nfunction extractPreviousValue<V>(state: AtomState<V>): V | undefined {\n if (state.status === \"stale\" || state.status === \"fresh\") {\n return state.value;\n }\n if (state.status === \"error\") {\n return state.value;\n }\n return undefined;\n}\n"],"mappings":";;AAgCA,SA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;AACvB,MAAI,MAAA,UAAgB,KAClB,QAAO,MAAA;AAGT,QAAA,SAAe;GACb,MAAS,SAA8B,KAAK,QAAQ,KAAK;GACzD,MAAuC,MAA0B,GAAG,SAClE,KAAK,IAAI,MAAM,GAAG,KAAK;GACzB,QAAW,MAAe,kBACxB,KAAK,MAAM,MAAM,cAAmB;GACtC,aAAa,SAA8B,KAAK,WAAW,KAAK;GAChE,iBAAiB,UAA8C,KAAK,eAAe,MAAM;GACzF,YAAe,MAAe,aAC5B,KAAK,UAAU,YAAY;AACzB,aAAS,KAAK,YAAY,KAAK,CAAC;KAChC;GACL;AACD,SAAO,MAAA;;;AAIX,SAAgB,cAAqB;AACnC,QAAO,IAAI,OAAO;;AAGpB,SAAS,aAAa,GAAqB;AACzC,QAAO,aAAa,gBAAgB,EAAE,SAAS;;AAGjD,SAAS,qBAAwB,OAAoC;AACnE,KAAI,MAAM,WAAW,WAAW,MAAM,WAAW,QAC/C,QAAO,MAAM;AAEf,KAAI,MAAM,WAAW,QACnB,QAAO,MAAM"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/internal/types.ts
|
|
2
|
+
const CONFIG = Symbol("kvark.config");
|
|
3
|
+
const WRITABLE = Symbol("kvark.writable");
|
|
4
|
+
const FAMILY_LINK = Symbol("kvark.familyLink");
|
|
5
|
+
//#endregion
|
|
6
|
+
export { FAMILY_LINK as n, WRITABLE as r, CONFIG as t };
|
|
7
|
+
|
|
8
|
+
//# sourceMappingURL=types-B2mzc3A5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-B2mzc3A5.js","names":[],"sources":["../src/internal/types.ts"],"sourcesContent":["export const CONFIG = Symbol(\"kvark.config\");\nexport const WRITABLE = Symbol(\"kvark.writable\");\nexport const FAMILY_LINK = Symbol(\"kvark.familyLink\");\n\nexport type AtomState<Value> =\n | { status: \"pending\"; value: undefined; error: undefined }\n | { status: \"stale\"; value: Value; error: undefined }\n | { status: \"fresh\"; value: Value; error: undefined }\n | { status: \"error\"; value: Value | undefined; error: unknown };\n\nexport type StalePolicy = \"keep\" | \"suspend\" | \"reset\";\n\nexport type AtomContext<Deps extends Record<string, Atom<unknown>>> = {\n get: <K extends keyof Deps>(key: K) => Promise<AtomValue<Deps[K]>>;\n signal: AbortSignal;\n};\n\nexport interface WritableAtomContext<\n Deps extends Record<string, Atom<unknown>>,\n Value,\n> extends AtomContext<Deps> {\n setOptimisticValue(value: Value): void;\n setOptimisticValue(mutate: (prev: Value | undefined) => Value): void;\n}\n\nexport type AtomConfig<\n Value,\n Deps extends Record<string, Atom<unknown>>,\n Args extends readonly unknown[],\n> = {\n dependencies?: Deps;\n stalePolicy?: StalePolicy;\n debugLabel?: string;\n get: (ctx: AtomContext<Deps>) => Promise<Value>;\n set?: (ctx: WritableAtomContext<Deps, Value>, ...args: Args) => Promise<void>;\n onMount?: (set: (value: Value) => void) => (() => void) | void;\n};\n\nexport interface InternalAtomConfig<Value> {\n dependencies?: Record<string, Atom<unknown>>;\n stalePolicy?: StalePolicy;\n debugLabel?: string;\n get: (ctx: AtomContext<Record<string, Atom<unknown>>>) => Promise<Value>;\n // Internal erased config: `Value` in WritableAtomContext is invariant; `any` matches all atom instantiations.\n set?: (\n // oxlint-disable-next-line typescript/no-explicit-any\n ctx: WritableAtomContext<Record<string, Atom<unknown>>, any>,\n ...args: readonly unknown[]\n ) => Promise<void>;\n onMount?: (set: (value: unknown) => void) => (() => void) | void;\n}\n\nexport interface Atom<out Value> {\n readonly [CONFIG]: InternalAtomConfig<Value>;\n readonly debugLabel: string | undefined;\n}\n\nexport interface WritableAtom<\n out Value,\n in out Args extends readonly unknown[],\n> extends Atom<Value> {\n readonly [WRITABLE]: Args;\n}\n\nexport type AtomValue<A> = A extends Atom<infer V> ? V : never;\nexport type IsWritable<A> = A extends WritableAtom<unknown, readonly unknown[]> ? true : false;\nexport type AtomArgs<A> = A extends WritableAtom<unknown, infer Args> ? Args : never;\n"],"mappings":";AAAA,MAAa,SAAS,OAAO,eAAe;AAC5C,MAAa,WAAW,OAAO,iBAAiB;AAChD,MAAa,cAAc,OAAO,mBAAmB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as WritableAtom, i as Atom, n as StoreClient, t as Store } from "../store-
|
|
1
|
+
import { f as WritableAtom, i as Atom, n as StoreClient, t as Store } from "../store-Cng0OzIQ.js";
|
|
2
2
|
import * as _$vue from "vue";
|
|
3
3
|
import { PropType, ShallowRef } from "vue";
|
|
4
4
|
|
|
@@ -20,25 +20,26 @@ declare function useStore(): Store;
|
|
|
20
20
|
//#endregion
|
|
21
21
|
//#region src/vue/use-atom-value.d.ts
|
|
22
22
|
type ObservedValue<V> = {
|
|
23
|
-
value: V;
|
|
23
|
+
value: V | undefined;
|
|
24
24
|
isStale: boolean;
|
|
25
25
|
error: unknown;
|
|
26
26
|
};
|
|
27
|
-
type ThenableShallowRef<V> = Readonly<ShallowRef<V>> & PromiseLike<Readonly<ShallowRef<V>>>;
|
|
27
|
+
type ThenableShallowRef<V> = Readonly<ShallowRef<V | undefined>> & PromiseLike<Readonly<ShallowRef<V>>>;
|
|
28
|
+
type ThenableObservedShallowRef<V> = Readonly<ShallowRef<ObservedValue<V>>> & PromiseLike<Readonly<ShallowRef<ObservedValue<V>>>>;
|
|
28
29
|
declare function useAtomValue<V>(atom: Atom<V>): ThenableShallowRef<V>;
|
|
29
30
|
declare function useAtomValue<V>(atom: Atom<V>, opts: {
|
|
30
31
|
observe: true;
|
|
31
|
-
}):
|
|
32
|
+
}): ThenableObservedShallowRef<V>;
|
|
32
33
|
//#endregion
|
|
33
34
|
//#region src/vue/use-set-atom.d.ts
|
|
34
35
|
declare function useSetAtom<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>): (...args: A) => Promise<void>;
|
|
35
36
|
//#endregion
|
|
36
37
|
//#region src/vue/use-atom.d.ts
|
|
37
|
-
type ThenableAtomTuple<V, A extends readonly unknown[]> = readonly [ThenableShallowRef<V>, (...args: A) => Promise<void>] & PromiseLike<readonly [
|
|
38
|
+
type ThenableAtomTuple<V, A extends readonly unknown[]> = readonly [ThenableShallowRef<V>, (...args: A) => Promise<void>] & PromiseLike<readonly [Readonly<ShallowRef<V>>, (...args: A) => Promise<void>]>;
|
|
38
39
|
declare function useAtom<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>): ThenableAtomTuple<V, A>;
|
|
39
40
|
//#endregion
|
|
40
41
|
//#region src/vue/use-atom-context.d.ts
|
|
41
42
|
declare function useAtomContext<R>(callback: (ctx: StoreClient) => Promise<R>): () => Promise<R>;
|
|
42
43
|
//#endregion
|
|
43
|
-
export { Provider, type ThenableShallowRef, useAtom, useAtomContext, useAtomValue, useSetAtom, useStore };
|
|
44
|
-
//# sourceMappingURL=index.d.
|
|
44
|
+
export { type ObservedValue, Provider, type ThenableObservedShallowRef, type ThenableShallowRef, useAtom, useAtomContext, useAtomValue, useSetAtom, useStore };
|
|
45
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/vue/provider.ts","../../src/vue/use-atom-value.ts","../../src/vue/use-set-atom.ts","../../src/vue/use-atom.ts","../../src/vue/use-atom-context.ts"],"mappings":";;;;;cAKa,QAAA,QAAQ,eAAA,CAGgB,KAAA,CAHhB,gBAAA;;UAGQ,QAAA,CAAS,KAAA;;;sBAHjB,KAAA,CAAA,YAAA;;gIAGgB,KAAA,CAAA,gBAAA;;UAAR,QAAA,CAAS,KAAA;;;;iBAQtB,QAAA,CAAA,GAAY,KAAA;;;KCZhB,aAAA;EACV,KAAA,EAAO,CAAA;EACP,OAAA;EACA,KAAA;AAAA;AAAA,KAGU,kBAAA,MAAwB,QAAA,CAAS,UAAA,CAAW,CAAA,iBACtD,WAAA,CAAY,QAAA,CAAS,UAAA,CAAW,CAAA;AAAA,KAEtB,0BAAA,MAAgC,QAAA,CAAS,UAAA,CAAW,aAAA,CAAc,CAAA,MAC5E,WAAA,CAAY,QAAA,CAAS,UAAA,CAAW,aAAA,CAAc,CAAA;AAAA,iBAEhC,YAAA,GAAA,CAAgB,IAAA,EAAM,IAAA,CAAK,CAAA,IAAK,kBAAA,CAAmB,CAAA;AAAA,iBACnD,YAAA,GAAA,CACd,IAAA,EAAM,IAAA,CAAK,CAAA,GACX,IAAA;EAAQ,OAAA;AAAA,IACP,0BAAA,CAA2B,CAAA;;;iBCjBd,UAAA,iCAAA,CACd,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,QACjB,IAAA,EAAM,CAAA,KAAM,OAAA;;;KCAd,iBAAA,8CACH,kBAAA,CAAmB,CAAA,OACf,IAAA,EAAM,CAAA,KAAM,OAAA,UAEhB,WAAA,WAAsB,QAAA,CAAS,UAAA,CAAW,CAAA,QAAS,IAAA,EAAM,CAAA,KAAM,OAAA;AAAA,iBAEjD,OAAA,iCAAA,CACd,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,IACrB,iBAAA,CAAkB,CAAA,EAAG,CAAA;;;iBCVR,cAAA,GAAA,CAAkB,QAAA,GAAW,GAAA,EAAK,WAAA,KAAgB,OAAA,CAAQ,CAAA,UAAW,OAAA,CAAQ,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/vue/provider.ts","../../src/vue/use-atom-value.ts","../../src/vue/use-set-atom.ts","../../src/vue/use-atom.ts","../../src/vue/use-atom-context.ts"],"sourcesContent":["import { type InjectionKey, type PropType, defineComponent, inject, provide } from \"vue\";\nimport type { Store } from \"@/internal/store.js\";\n\nconst StoreKey: InjectionKey<Store> = Symbol(\"kvark-store\");\n\nexport const Provider = defineComponent({\n name: \"KvarkProvider\",\n props: {\n store: { type: Object as PropType<Store>, required: true },\n },\n setup(props, { slots }) {\n provide(StoreKey, props.store);\n return () => slots[\"default\"]?.();\n },\n});\n\nexport function useStore(): Store {\n const store = inject(StoreKey);\n if (store == null) {\n throw new Error(\"useStore must be used within a <Provider>\");\n }\n return store;\n}\n","import { type ShallowRef, readonly, shallowRef, watchEffect } from \"vue\";\nimport type { Atom, AtomState } from \"@/internal/types.js\";\nimport { useStore } from \"@/vue/provider.js\";\n\nexport type ObservedValue<V> = {\n value: V | undefined;\n isStale: boolean;\n error: unknown;\n};\n\nexport type ThenableShallowRef<V> = Readonly<ShallowRef<V | undefined>> &\n PromiseLike<Readonly<ShallowRef<V>>>;\n\nexport type ThenableObservedShallowRef<V> = Readonly<ShallowRef<ObservedValue<V>>> &\n PromiseLike<Readonly<ShallowRef<ObservedValue<V>>>>;\n\nexport function useAtomValue<V>(atom: Atom<V>): ThenableShallowRef<V>;\nexport function useAtomValue<V>(\n atom: Atom<V>,\n opts: { observe: true },\n): ThenableObservedShallowRef<V>;\nexport function useAtomValue<V>(\n atom: Atom<V>,\n opts?: { observe: true },\n): ThenableShallowRef<V> | ThenableObservedShallowRef<V> {\n const store = useStore();\n\n function derive(snapshot: AtomState<V>): V | ObservedValue<V> {\n if (snapshot.status === \"pending\") {\n void store.resolve(atom);\n if (opts?.observe === true) {\n return { value: undefined as V, isStale: false, error: undefined };\n }\n return undefined as V;\n }\n\n if (snapshot.status === \"stale\") {\n void store.resolve(atom);\n if (opts?.observe === true) {\n return { value: snapshot.value, isStale: true, error: undefined };\n }\n return snapshot.value;\n }\n\n if (snapshot.status === \"error\") {\n if (opts?.observe === true && snapshot.value !== undefined) {\n return { value: snapshot.value as V, isStale: false, error: snapshot.error };\n }\n throw snapshot.error;\n }\n\n // status === \"fresh\"\n if (opts?.observe === true) {\n return { value: snapshot.value, isStale: false, error: undefined };\n }\n return snapshot.value;\n }\n\n const result = shallowRef(derive(store.getSnapshot(atom)));\n\n watchEffect((onCleanup) => {\n const unsub = store.subscribe(atom, () => {\n result.value = derive(store.getSnapshot(atom));\n });\n onCleanup(unsub);\n });\n\n const ref = readonly(result);\n\n return new Proxy(ref, {\n get(target, prop, receiver) {\n if (prop === \"then\") {\n return (onFulfilled?: (v: unknown) => unknown, onRejected?: (e: unknown) => unknown) =>\n store\n .resolve(atom)\n .then(() => ref, onRejected)\n .then(onFulfilled as never);\n }\n return Reflect.get(target, prop, receiver);\n },\n }) as ThenableShallowRef<V> | ThenableObservedShallowRef<V>;\n}\n","import type { WritableAtom } from \"@/internal/types.js\";\nimport { useStore } from \"@/vue/provider.js\";\n\nexport function useSetAtom<V, A extends readonly unknown[]>(\n atom: WritableAtom<V, A>,\n): (...args: A) => Promise<void> {\n const store = useStore();\n return (...args: A): Promise<void> => store.set(atom, ...args);\n}\n","import type { ShallowRef } from \"vue\";\nimport type { WritableAtom } from \"@/internal/types.js\";\nimport { type ThenableShallowRef, useAtomValue } from \"@/vue/use-atom-value.js\";\nimport { useSetAtom } from \"@/vue/use-set-atom.js\";\n\ntype ThenableAtomTuple<V, A extends readonly unknown[]> = readonly [\n ThenableShallowRef<V>,\n (...args: A) => Promise<void>,\n] &\n PromiseLike<readonly [Readonly<ShallowRef<V>>, (...args: A) => Promise<void>]>;\n\nexport function useAtom<V, A extends readonly unknown[]>(\n atom: WritableAtom<V, A>,\n): ThenableAtomTuple<V, A> {\n const value = useAtomValue(atom);\n const setter = useSetAtom(atom);\n const tuple = [value, setter] as const;\n\n return Object.assign(tuple, {\n // oxlint-disable-next-line unicorn/no-thenable -- PromiseLike for await useAtom()\n then(onFulfilled?: (v: typeof tuple) => unknown, onRejected?: (e: unknown) => unknown) {\n return Promise.resolve(value)\n .then(() => [value, setter] as const, onRejected)\n .then(onFulfilled as never);\n },\n }) as ThenableAtomTuple<V, A>;\n}\n","import type { StoreClient } from \"@/internal/store.js\";\nimport { useStore } from \"@/vue/provider.js\";\n\nexport function useAtomContext<R>(callback: (ctx: StoreClient) => Promise<R>): () => Promise<R> {\n const store = useStore();\n return (): Promise<R> => callback(store.getClient());\n}\n"],"mappings":";;AAGA,MAAM,WAAgC,OAAO,cAAc;AAE3D,MAAa,WAAW,gBAAgB;CACtC,MAAM;CACN,OAAO,EACL,OA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SAAS,OACX,SAAQ,aAAuC,eAC7C,MACG,QAAQ,KAAK,CACb,WAAW,KAAK,WAAW,CAC3B,KAAK,YAAqB;AAEjC,SAAO,QAAQ,IAAI,QAAQ,MAAM,SAAS;IAE7C,CAAC;;;;AC7EJ,SAAgB,WACd,MAC+B;CAC/B,MAAM,QAAQ,UAAU;AACxB,SAAQ,GAAG,SAA2B,MAAM,IAAI,MAAM,GAAG,KAAK;;;;ACIhE,SAAgB,QACd,MACyB;CACzB,MAAM,QAAQ,aAAa,KAAK;CAChC,MAAM,SAAS,WAAW,KAAK;AAG/B,QAAO,OAAO,OAFA,CAAC,OAAO,OAAO,EAED,EAE1B,KAAK,aAA4C,YAAsC;AACrF,SAAO,QAAQ,QAAQ,MAAM,CAC1B,WAAW,CAAC,OAAO,OAAO,EAAW,WAAW,CAChD,KAAK,YAAqB;IAEhC,CAAC;;;;ACtBJ,SAAgB,eAAkB,UAA8D;CAC9F,MAAM,QAAQ,UAAU;AACxB,cAAyB,SAAS,MAAM,WAAW,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,45 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kdeveloper/kvark",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.6",
|
|
4
4
|
"description": "Atomic state management with explicit dependency graphs",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
8
8
|
],
|
|
9
9
|
"type": "module",
|
|
10
|
-
"main": "./dist/index.
|
|
11
|
-
"module": "./dist/index.
|
|
12
|
-
"types": "./dist/index.d.
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
13
|
"exports": {
|
|
14
14
|
".": {
|
|
15
|
-
"types": "./dist/index.d.
|
|
16
|
-
"import": "./dist/index.
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
17
|
},
|
|
18
18
|
"./react": {
|
|
19
|
-
"types": "./dist/react/index.d.
|
|
20
|
-
"import": "./dist/react/index.
|
|
19
|
+
"types": "./dist/react/index.d.ts",
|
|
20
|
+
"import": "./dist/react/index.js"
|
|
21
21
|
},
|
|
22
22
|
"./vue": {
|
|
23
|
-
"types": "./dist/vue/index.d.
|
|
24
|
-
"import": "./dist/vue/index.
|
|
23
|
+
"types": "./dist/vue/index.d.ts",
|
|
24
|
+
"import": "./dist/vue/index.js"
|
|
25
25
|
},
|
|
26
26
|
"./family": {
|
|
27
|
-
"types": "./dist/family.d.
|
|
28
|
-
"import": "./dist/family.
|
|
27
|
+
"types": "./dist/family.d.ts",
|
|
28
|
+
"import": "./dist/family.js"
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@testing-library/dom": "^10.4.1",
|
|
33
33
|
"@testing-library/react": "^16.3.2",
|
|
34
|
+
"@types/node": "^25.5.0",
|
|
34
35
|
"@types/react": "^19.2.14",
|
|
35
36
|
"@types/react-dom": "^19.2.3",
|
|
36
37
|
"@vue/test-utils": "^2.4.6",
|
|
37
38
|
"jsdom": "^29.0.1",
|
|
38
39
|
"oxfmt": "^0.42.0",
|
|
39
40
|
"oxlint": "^1.57.0",
|
|
41
|
+
"oxlint-tsgolint": "^0.18.1",
|
|
40
42
|
"react": ">=18",
|
|
41
43
|
"react-dom": "^19.2.4",
|
|
42
|
-
"tsdown": "^0.21.
|
|
44
|
+
"tsdown": "^0.21.7",
|
|
43
45
|
"typescript": "^6.0.2",
|
|
44
46
|
"vitest": "^4.1.2",
|
|
45
47
|
"vue": "^3.5.31"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"atom-DEAcunGv.d.mts","names":[],"sources":["../src/internal/atom.ts"],"mappings":";;;KASK,cAAA,qBAEU,MAAA,SAAe,IAAA,+CAE1B,UAAA,CAAW,KAAA,EAAO,IAAA,EAAM,IAAA;EAC1B,GAAA,GA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AO,KAAA;EACd,QAAA,IAAY,WAAA,CAAY,GAAA,EAAK,IAAA,CAAK,KAAA;AAAA;AAAA,iBAYpB,UAAA,4BAGD,MAAA,SAAe,IAAA,aAAiB,MAAA,qEAEvC,KAAA,CAAA,CACN,OAAA,EAAS,iBAAA,CAAkB,KAAA,EAAO,KAAA,EAAO,IAAA,EAAM,IAAA,EAAM,GAAA,IAAO,UAAA,CAAW,KAAA,EAAO,KAAA,EAAO,GAAA;;;;;;;AAlDvF;;;;;;;iBCUgB,eAAA,CAAgB,KAAA"}
|
package/dist/family.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"family.mjs","names":[],"sources":["../src/internal/stable-family-key.ts"],"sourcesContent":["/**\n * Produces a deterministic string key for plain-object/array/primitive values.\n *\n * Supported types: string, number, boolean, null, undefined, bigint,\n * plain objects (incl. nested), arrays.\n *\n * Not supported (throws or produces non-unique output): circular references,\n * class instances other than plain Object/Array, Map, Set, Symbol, functions.\n * Treat the parameter as immutable — mutating an object after its first key is\n * computed via a WeakMap memo will silently return the stale cached key.\n */\n\nconst memo = new WeakMap<object, string>();\n\nexport function stableFamilyKey(value: unknown): string {\n if (value === null) return \"null\";\n if (value === undefined) return \"undefined\";\n\n const t = typeof value;\n\n if (t === \"string\") return JSON.stringify(value);\n if (t === \"number\") return String(value);\n if (t === \"boolean\") return String(value);\n if (t === \"bigint\") return `${String(value)}n`;\n\n if (t !== \"object\") {\n throw new TypeError(`stableFamilyKey: unsupported type \"${t}\" (${String(value)})`);\n }\n\n const obj = value as object;\n const cached = memo.get(obj);\n if (cached !== undefined) return cached;\n\n const result = serializeValue(obj);\n memo.set(obj, result);\n return result;\n}\n\nfunction serializeValue(value: unknown): string {\n if (value === null) return \"null\";\n if (value === undefined) return \"undefined\";\n\n const t = typeof value;\n\n if (t === \"string\") return JSON.stringify(value);\n if (t === \"number\") return String(value);\n if (t === \"boolean\") return String(value);\n if (t === \"bigint\") return `${String(value)}n`;\n\n if (t !== \"object\") {\n throw new TypeError(`stableFamilyKey: unsupported type \"${t}\" (${String(value)})`);\n }\n\n if (Array.isArray(value)) {\n const items = (value as unknown[]).map(serializeValue);\n return `[${items.join(\",\")}]`;\n }\n\n const proto = Object.getPrototypeOf(value);\n if (proto !== Object.prototype && proto !== null) {\n throw new TypeError(\n `stableFamilyKey: only plain objects are supported (got ${String(proto?.constructor?.name ?? \"unknown\")})`,\n );\n }\n\n const keys = Object.keys(value as Record<string, unknown>).sort();\n const pairs = keys.map((k) => {\n const v = (value as Record<string, unknown>)[k];\n return `${JSON.stringify(k)}:${serializeValue(v)}`;\n });\n return `{${pairs.join(\",\")}}`;\n}\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAM,uBAAO,IAAI,SAAyB;AAE1C,SAAgB,gBAAgB,OAAwB;AACtD,KAAI,UAAU,KAAM,QAAO;AAC3B,KAAI,UAAU,KAAA,EAAW,QAAO;CAEhC,MAAM,IAAI,OAAO;AAEjB,KAAI,MAAM,SAAU,QAAO,KAAK,UAAU,MAAM;AAChD,KAAI,MAAM,SAAU,QAAO,OAAO,MAAM;AACxC,KAAI,MAAM,UAAW,QAAO,OAAO,MAAM;AACzC,KAAI,MAAM,SAAU,QAAO,GAAG,OAAO,MAAM,CAAC;AAE5C,KAAI,MAAM,SACR,OAAM,IAAI,UAAU,sCAAsC,EAAE,KAAK,OAAO,MAAM,CAAC,GAAG;CAGpF,MAAM,MAAM;CACZ,MAAM,SAAS,KAAK,IAAI,IAAI;AAC5B,KAAI,WAAW,KAAA,EAAW,QAAO;CAEjC,MAAM,SAAS,eAAe,IAAI;AAClC,MAAK,IAAI,KAAK,OAAO;AACrB,QAAO;;AAGT,SAAS,eAAe,OAAwB;AAC9C,KAAI,UAAU,KAAM,QAAO;AAC3B,KAAI,UAAU,KAAA,EAAW,QAAO;CAEhC,MAAM,IAAI,OAAO;AAEjB,KAAI,MAAM,SAAU,QAAO,KAAK,UAAU,MAAM;AAChD,KAAI,MAAM,SAAU,QAAO,OAAO,MAAM;AACxC,KAAI,MAAM,UAAW,QAAO,OAAO,MAAM;AACzC,KAAI,MAAM,SAAU,QAAO,GAAG,OAAO,MAAM,CAAC;AAE5C,KAAI,MAAM,SACR,OAAM,IAAI,UAAU,sCAAsC,EAAE,KAAK,OAAO,MAAM,CAAC,GAAG;AAGpF,KAAI,MAAM,QAAQ,MAAM,CAEtB,QAAO,IADQ,MAAoB,IAAI,eAAe,CACrC,KAAK,IAAI,CAAC;CAG7B,MAAM,QAAQ,OAAO,eAAe,MAAM;AAC1C,KAAI,UAAU,OAAO,aAAa,UAAU,KAC1C,OAAM,IAAI,UACR,0DAA0D,OAAO,OAAO,aAAa,QAAQ,UAAU,CAAC,GACzG;AAQH,QAAO,IALM,OAAO,KAAK,MAAiC,CAAC,MAAM,CAC9C,KAAK,MAAM;EAC5B,MAAM,IAAK,MAAkC;AAC7C,SAAO,GAAG,KAAK,UAAU,EAAE,CAAC,GAAG,eAAe,EAAE;GAChD,CACe,KAAK,IAAI,CAAC"}
|
package/dist/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/react/provider.tsx","../../src/react/use-atom-value.ts","../../src/react/use-set-atom.ts","../../src/react/use-atom.ts","../../src/react/use-atom-context.ts"],"mappings":";;;;KAKY,aAAA;EACV,KAAA,EAAO,KAAA;EACP,QAAA,EAAU,SAAA;AAAA;AAAA,iBAGI,QAAA,CAAA;EAAW,KAAA;EAAO;AAAA,GAAY,aAAA,GAAgB,SAAA;AAAA,iBAI9C,QAAA,CAAA,GAAY,KAAA;;;KCTvB,aAAA;EACH,KAAA,EAAO,CAAA;EACP,OAAA;EACA,KAAA;AAAA;AAAA,iBAGc,YAAA,GAAA,CAAgB,IAAA,EAAM,IAAA,CAAK,CAAA,IAAK,CAAA;AAAA,iBAChC,YAAA,GAAA,CAAgB,IAAA,EAAM,IAAA,CAAK,CAAA,GAAI,IAAA;EAAQ,OAAA;AAAA,IAAkB,aAAA,CAAc,CAAA;;;iBCRvE,UAAA,iCAAA,CACd,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,QACjB,IAAA,EAAM,CAAA,KAAM,OAAA;;;iBCFH,OAAA,iCAAA,CACd,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,cACX,CAAA,MAAO,IAAA,EAAM,CAAA,KAAM,OAAA;;;iBCFhB,cAAA,GAAA,CAAkB,QAAA,GAAW,GAAA,EAAK,WAAA,KAAgB,OAAA,CAAQ,CAAA,UAAW,OAAA,CAAQ,CAAA"}
|
package/dist/react/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/react/provider.tsx","../../src/react/use-atom-value.ts","../../src/react/use-set-atom.ts","../../src/react/use-atom.ts","../../src/react/use-atom-context.ts"],"sourcesContent":["import { createContext, useContext, type ReactNode } from \"react\";\nimport { Store } from \"@/internal/store.js\";\n\nconst StoreContext = createContext<Store | null>(null);\n\nexport type ProviderProps = {\n store: Store;\n children: ReactNode;\n};\n\nexport function Provider({ store, children }: ProviderProps): ReactNode {\n return <StoreContext value={store}>{children}</StoreContext>;\n}\n\nexport function useStore(): Store {\n const store = useContext(StoreContext);\n if (store == null) {\n throw new Error(\"useStore must be used within a <Provider>\");\n }\n return store;\n}\n","import { useSyncExternalStore } from \"react\";\nimport type { Atom, AtomState, StalePolicy } from \"@/internal/types.js\";\nimport { CONFIG } from \"@/internal/types.js\";\nimport { useStore } from \"@/react/provider.js\";\n\ntype ObservedValue<V> = {\n value: V;\n isStale: boolean;\n error: unknown;\n};\n\nexport function useAtomValue<V>(atom: Atom<V>): V;\nexport function useAtomValue<V>(atom: Atom<V>, opts: { observe: true }): ObservedValue<V>;\nexport function useAtomValue<V>(atom: Atom<V>, opts?: { observe: true }): V | ObservedValue<V> {\n const store = useStore();\n\n const snapshot: AtomState<V> = useSyncExternalStore(\n (notify) => store.subscribe(atom, notify),\n () => store.getSnapshot(atom),\n () => store.getServerSnapshot(atom),\n );\n\n const stalePolicy: StalePolicy = atom[CONFIG].stalePolicy ?? \"keep\";\n\n if (snapshot.status === \"pending\") {\n throw store.resolve(atom);\n }\n\n if (snapshot.status === \"stale\") {\n if (stalePolicy === \"suspend\") {\n throw store.resolve(atom);\n }\n // \"keep\" or \"reset\" while stale: trigger background revalidation\n void store.resolve(atom);\n\n if (opts?.observe === true) {\n return { value: snapshot.value, isStale: true, error: undefined };\n }\n return snapshot.value;\n }\n\n if (snapshot.status === \"error\") {\n if (opts?.observe === true && snapshot.value !== undefined) {\n return {\n value: snapshot.value as V,\n isStale: false,\n error: snapshot.error,\n };\n }\n throw snapshot.error;\n }\n\n // status === \"fresh\"\n if (opts?.observe === true) {\n return { value: snapshot.value, isStale: false, error: undefined };\n }\n return snapshot.value;\n}\n","import { useCallback } from \"react\";\nimport type { WritableAtom } from \"@/internal/types.js\";\nimport { useStore } from \"@/react/provider.js\";\n\nexport function useSetAtom<V, A extends readonly unknown[]>(\n atom: WritableAtom<V, A>,\n): (...args: A) => Promise<void> {\n const store = useStore();\n return useCallback((...args: A): Promise<void> => store.set(atom, ...args), [store, atom]);\n}\n","import type { WritableAtom } from \"@/internal/types.js\";\nimport { useAtomValue } from \"@/react/use-atom-value.js\";\nimport { useSetAtom } from \"@/react/use-set-atom.js\";\n\nexport function useAtom<V, A extends readonly unknown[]>(\n atom: WritableAtom<V, A>,\n): readonly [V, (...args: A) => Promise<void>] {\n const value = useAtomValue(atom);\n const setter = useSetAtom(atom);\n return [value, setter] as const;\n}\n","import { useCallback } from \"react\";\nimport type { StoreClient } from \"@/internal/store.js\";\nimport { useStore } from \"@/react/provider.js\";\n\nexport function useAtomContext<R>(callback: (ctx: StoreClient) => Promise<R>): () => Promise<R> {\n const store = useStore();\n return useCallback((): Promise<R> => callback(store.getClient()), [store, callback]);\n}\n"],"mappings":";;;;AAGA,MAAM,eAAe,cAA4B,KAAK;AAOtD,SAAgB,SAA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nC,UAAA,CAAW,IAAA,EAAM,IAAA;EAUjB,cAAA,CAAe,KAAA,EAAO,aAAA,CAAc,IAAA;EA8GpC,SAAA,GAAA,CAAa,IAAA,EAAM,IAAA,CAAK,CAAA,GAAI,QAAA;EAmD5B,WAAA,GAAA,CAAe,IAAA,EAAM,IAAA,CAAK,CAAA,IAAK,SAAA,CAAU,CAAA;EAIzC,iBAAA,GAAA,CAAqB,IAAA,EAAM,IAAA,CAAK,CAAA,IAAK,SAAA,CAAU,CAAA;EAQzC,GAAA,iCAAA,CAAqC,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,MAAO,IAAA,EAAM,CAAA,GAAI,OAAA;EAiDlF,KAAA,GAAA,CAAS,IAAA,EAAM,IAAA,CAAK,CAAA,GAAI,KAAA,EAAO,CAAA;EAC/B,KAAA,GAAA,CAAS,IAAA,EAAM,IAAA,CAAK,CAAA,GAAI,MAAA,GAAS,IAAA,EAAM,CAAA,iBAAkB,CAAA;EAezD,SAAA,CAAA,GAAa,WAAA;AAAA;AAAA,iBAsBC,WAAA,CAAA,GAAe,KAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"store-j2laS7Q2.mjs","names":["#atoms","#rdeps","#pending","#controllers","#familyUnsubs","#registeredFamilies","#getOrCreate","#runGet","#registerRdeps","#makeCtx","#scheduleNotify","#markStale","#flushPending","#autoRegisterFamily","#markReverseDependentsStale","#client"],"sources":["../src/internal/types.ts","../src/internal/atom.ts","../src/internal/family.ts","../src/internal/store.ts"],"sourcesContent":["export const CONFIG = Symbol(\"kvark.config\");\nexport const WRITABLE = Symbol(\"kvark.writable\");\nexport const FAMILY_LINK = Symbol(\"kvark.familyLink\");\n\nexport type AtomState<Value> =\n | { status: \"pending\"; value: undefined; error: undefined }\n | { status: \"stale\"; value: Value; error: undefined }\n | { status: \"fresh\"; value: Value; error: undefined }\n | { status: \"error\"; value: Value | undefined; error: unknown };\n\nexport type StalePolicy = \"keep\" | \"suspend\" | \"reset\";\n\nexport type AtomContext<Deps extends Record<string, Atom<unknown>>> = {\n get: <K extends keyof Deps>(key: K) => Promise<AtomValue<Deps[K]>>;\n signal: AbortSignal;\n};\n\nexport interface WritableAtomContext<\n Deps extends Record<string, Atom<unknown>>,\n Value,\n> extends AtomContext<Deps> {\n setOptimisticValue(value: Value): void;\n setOptimisticValue(mutate: (prev: Value | undefined) => Value): void;\n}\n\nexport type AtomConfig<\n Value,\n Deps extends Record<string, Atom<unknown>>,\n Args extends readonly unknown[],\n> = {\n dependencies?: Deps;\n stalePolicy?: StalePolicy;\n debugLabel?: string;\n get: (ctx: AtomContext<Deps>) => Promise<Value>;\n set?: (ctx: WritableAtomContext<Deps, Value>, ...args: Args) => Promise<void>;\n onMount?: (set: (value: Value) => void) => (() => void) | void;\n};\n\nexport interface InternalAtomConfig<Value> {\n dependencies?: Record<string, Atom<unknown>>;\n stalePolicy?: StalePolicy;\n debugLabel?: string;\n get: (ctx: AtomContext<Record<string, Atom<unknown>>>) => Promise<Value>;\n set?: (\n ctx: WritableAtomContext<Record<string, Atom<unknown>>, any>,\n ...args: readonly unknown[]\n ) => Promise<void>;\n onMount?: (set: (value: unknown) => void) => (() => void) | void;\n}\n\nexport interface Atom<out Value> {\n readonly [CONFIG]: InternalAtomConfig<Value>;\n readonly debugLabel: string | undefined;\n}\n\nexport interface WritableAtom<\n out Value,\n in out Args extends readonly unknown[],\n> extends Atom<Value> {\n readonly [WRITABLE]: Args;\n}\n\nexport type AtomValue<A> = A extends Atom<infer V> ? V : never;\nexport type IsWritable<A> = A extends WritableAtom<unknown, readonly unknown[]> ? true : false;\nexport type AtomArgs<A> = A extends WritableAtom<unknown, infer Args> ? Args : never;\n","import type {\n Atom,\n AtomConfig,\n InternalAtomConfig,\n WritableAtom,\n WritableAtomContext,\n} from \"@/internal/types.js\";\nimport { CONFIG, WRITABLE } from \"@/internal/types.js\";\n\ntype WritableConfig<\n Value,\n Deps extends Record<string, Atom<unknown>>,\n Args extends readonly unknown[],\n> = AtomConfig<Value, Deps, Args> & {\n set: (ctx: WritableAtomContext<Deps, Value>, ...args: Args) => Promise<void>;\n};\n\ntype ReadonlyConfig<Value, Deps extends Record<string, Atom<unknown>>> = Omit<\n AtomConfig<Value, Deps, readonly []>,\n \"set\"\n>;\n\nexport function atom<\n Value,\n Deps extends Record<string, Atom<unknown>> = Record<never, never>,\n Args extends readonly unknown[] = readonly [],\n>(config: WritableConfig<Value, Deps, Args>): WritableAtom<Value, Args>;\n\nexport function atom<Value, Deps extends Record<string, Atom<unknown>> = Record<never, never>>(\n config: ReadonlyConfig<Value, Deps>,\n): Atom<Value>;\n\nexport function atom<\n Value,\n Deps extends Record<string, Atom<unknown>> = Record<never, never>,\n Args extends readonly unknown[] = readonly [],\n>(config: AtomConfig<Value, Deps, Args>): Atom<Value> {\n const internal: InternalAtomConfig<Value> = Object.create(null) as InternalAtomConfig<Value>;\n internal.get = config.get as InternalAtomConfig<Value>[\"get\"];\n\n if (config.debugLabel != null) {\n internal.debugLabel = config.debugLabel;\n }\n if (config.dependencies != null) {\n internal.dependencies = config.dependencies as Record<string, Atom<unknown>>;\n }\n if (config.stalePolicy != null) {\n internal.stalePolicy = config.stalePolicy;\n }\n if (config.set != null) {\n internal.set = config.set as NonNullable<InternalAtomConfig<Value>[\"set\"]>;\n }\n if (config.onMount != null) {\n internal.onMount = config.onMount as NonNullable<InternalAtomConfig<Value>[\"onMount\"]>;\n }\n\n if (config.set != null) {\n return {\n [CONFIG]: internal,\n [WRITABLE]: [] as unknown as Args,\n debugLabel: config.debugLabel,\n } as unknown as Atom<Value>;\n }\n\n return {\n [CONFIG]: internal,\n debugLabel: config.debugLabel,\n } as Atom<Value>;\n}\n","import type { Atom, AtomContext, StalePolicy } from \"@/internal/types.js\";\nimport { FAMILY_LINK } from \"@/internal/types.js\";\nimport { atom } from \"@/internal/atom.js\";\n\nexport type AtomFamilyOptions<\n Param,\n Value,\n Deps extends Record<string, Atom<unknown>>,\n Args extends readonly unknown[],\n Key = Param,\n> = {\n dependencies?: (param: Param) => Deps;\n stalePolicy?: StalePolicy;\n cachePolicy?: \"keep-all\" | \"lru\";\n lruSize?: number;\n debugLabel?: string;\n /**\n * Optional function to derive a stable cache key from `param`.\n * Use when `Param` is an object and you want equality-by-value semantics.\n * The returned key is used for all cache operations (get, set, LRU, invalidate, remove).\n * The original `param` is still passed to `get`, `set`, and `dependencies`.\n *\n * For plain objects/arrays you can use `stableFamilyKey` from `@kdeveloper/kvark/family`.\n * Treat the param object as immutable after the first call — `stableFamilyKey` memoises\n * by object reference via a WeakMap.\n */\n paramKey?: (param: Param) => Key;\n get: (param: Param) => (ctx: AtomContext<Deps>) => Promise<Value>;\n set?: (param: Param) => (ctx: AtomContext<Deps>, ...args: Args) => Promise<void>;\n};\n\nexport interface AtomFamily<Param, Value, Key = Param> {\n (param: Param): Atom<Value>;\n invalidate(param: Param): void;\n invalidateAll(): void;\n remove(param: Param): void;\n getCache(): ReadonlyMap<Key, Atom<Value>>;\n}\n\nexport interface FamilyLink {\n invalidateAtom: (atom: Atom<unknown>) => void;\n registerStore: (cb: (atom: Atom<unknown>) => void) => () => void;\n}\n\nexport function getFamilyLink(target: Atom<unknown>): FamilyLink | undefined {\n return (target as unknown as Record<symbol, FamilyLink | undefined>)[FAMILY_LINK];\n}\n\nexport function atomFamily<\n Param,\n Value,\n Deps extends Record<string, Atom<unknown>> = Record<never, never>,\n Args extends readonly unknown[] = readonly [],\n Key = Param,\n>(options: AtomFamilyOptions<Param, Value, Deps, Args, Key>): AtomFamily<Param, Value, Key> {\n const cachePolicy = options.cachePolicy ?? \"keep-all\";\n const lruSize = options.lruSize ?? 100;\n const cache = new Map<Key, Atom<Value>>();\n const lruOrder: Key[] = [];\n const storeCallbacks = new Set<(atom: Atom<unknown>) => void>();\n\n function keyOf(param: Param): Key {\n return options.paramKey != null ? options.paramKey(param) : (param as unknown as Key);\n }\n\n const link: FamilyLink = {\n invalidateAtom(target: Atom<unknown>): void {\n for (const cb of storeCallbacks) {\n cb(target);\n }\n },\n registerStore(cb: (atom: Atom<unknown>) => void): () => void {\n storeCallbacks.add(cb);\n return () => {\n storeCallbacks.delete(cb);\n };\n },\n };\n\n function attachLink(target: Atom<Value>): void {\n (target as unknown as Record<symbol, FamilyLink>)[FAMILY_LINK] = link;\n }\n\n function getOrCreate(param: Param): Atom<Value> {\n const key = keyOf(param);\n const cached = cache.get(key);\n if (cached != null) {\n if (cachePolicy === \"lru\") {\n touchLru(key);\n }\n return cached;\n }\n\n const deps = options.dependencies?.(param);\n const getFn = options.get(param);\n const setFn = options.set?.(param);\n\n const labelSuffix = options.paramKey != null ? String(key) : String(param);\n const label = options.debugLabel != null ? `${options.debugLabel}(${labelSuffix})` : undefined;\n\n const created = createAtom(label, deps, getFn, setFn);\n attachLink(created);\n cache.set(key, created);\n\n if (cachePolicy === \"lru\") {\n lruOrder.push(key);\n evictLru();\n }\n\n return created;\n }\n\n function createAtom(\n label: string | undefined,\n deps: Deps | undefined,\n getFn: (ctx: AtomContext<Deps>) => Promise<Value>,\n setFn: ((ctx: AtomContext<Deps>, ...args: Args) => Promise<void>) | undefined,\n ): Atom<Value> {\n const base = {\n get: getFn as (ctx: AtomContext<Deps>) => Promise<Value>,\n } as {\n debugLabel?: string;\n dependencies?: Deps;\n stalePolicy?: StalePolicy;\n get: (ctx: AtomContext<Deps>) => Promise<Value>;\n set?: (ctx: AtomContext<Deps>, ...args: Args) => Promise<void>;\n };\n\n if (label != null) {\n base.debugLabel = label;\n }\n if (deps != null) {\n base.dependencies = deps;\n }\n if (options.stalePolicy != null) {\n base.stalePolicy = options.stalePolicy;\n }\n\n if (setFn != null) {\n base.set = setFn;\n return atom(\n base as typeof base & {\n set: (ctx: AtomContext<Deps>, ...args: Args) => Promise<void>;\n },\n ) as Atom<Value>;\n }\n\n return atom(base as Omit<typeof base, \"set\">);\n }\n\n function touchLru(key: Key): void {\n const idx = lruOrder.indexOf(key);\n if (idx !== -1) {\n lruOrder.splice(idx, 1);\n }\n lruOrder.push(key);\n }\n\n function evictLru(): void {\n while (lruOrder.length > lruSize) {\n const evicted = lruOrder.shift();\n if (evicted !== undefined) {\n cache.delete(evicted);\n }\n }\n }\n\n const family = getOrCreate as AtomFamily<Param, Value, Key>;\n\n family.invalidate = (param: Param): void => {\n const key = keyOf(param);\n const cached = cache.get(key);\n if (cached != null) {\n link.invalidateAtom(cached);\n }\n };\n\n family.invalidateAll = (): void => {\n for (const cached of cache.values()) {\n link.invalidateAtom(cached);\n }\n };\n\n family.remove = (param: Param): void => {\n const key = keyOf(param);\n cache.delete(key);\n if (cachePolicy === \"lru\") {\n const idx = lruOrder.indexOf(key);\n if (idx !== -1) {\n lruOrder.splice(idx, 1);\n }\n }\n };\n\n family.getCache = (): ReadonlyMap<Key, Atom<Value>> => cache;\n\n return family;\n}\n","import type {\n Atom,\n AtomContext,\n AtomState,\n InternalAtomConfig,\n StalePolicy,\n WritableAtom,\n WritableAtomContext,\n} from \"@/internal/types.js\";\nimport { CONFIG } from \"@/internal/types.js\";\nimport { getFamilyLink } from \"@/internal/family.js\";\n\ntype AtomEntry<V> = {\n state: AtomState<V>;\n version: number;\n promise: Promise<V> | null;\n listeners: Set<() => void>;\n mountCount: number;\n unmount: (() => void) | null;\n};\n\nexport interface StoreClient {\n get<V>(atom: Atom<V>): Promise<V>;\n set<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>, ...args: A): Promise<void>;\n write<V>(atom: Atom<V>, value: V): void;\n write<V>(atom: Atom<V>, mutate: (prev: V | undefined) => V): void;\n invalidate(atom: Atom<unknown>): void;\n invalidateMany(atoms: ReadonlyArray<Atom<unknown>>): void;\n subscribe<V>(atom: Atom<V>, listener: (state: AtomState<V>) => void): () => void;\n}\n\nconst PENDING_STATE: AtomState<never> = {\n status: \"pending\",\n value: undefined,\n error: undefined,\n} as AtomState<never>;\n\nexport class Store {\n readonly #atoms = new WeakMap<Atom<unknown>, AtomEntry<unknown>>();\n readonly #rdeps = new Map<Atom<unknown>, Set<Atom<unknown>>>();\n readonly #pending = new Set<Atom<unknown>>();\n readonly #controllers = new WeakMap<Atom<unknown>, AbortController>();\n readonly #familyUnsubs = new Set<() => void>();\n readonly #registeredFamilies = new WeakSet<object>();\n #client: StoreClient | null = null;\n\n #getOrCreate<V>(atom: Atom<V>): AtomEntry<V> {\n let entry = this.#atoms.get(atom) as AtomEntry<V> | undefined;\n if (entry == null) {\n entry = {\n state: PENDING_STATE as AtomState<V>,\n version: 0,\n promise: null,\n listeners: new Set(),\n mountCount: 0,\n unmount: null,\n };\n this.#atoms.set(atom, entry as AtomEntry<unknown>);\n }\n return entry;\n }\n\n resolve<V>(atom: Atom<V>): Promise<V> {\n const entry = this.#getOrCreate(atom);\n if (entry.promise != null) {\n return entry.promise;\n }\n\n const promise = this.#runGet(atom).finally(() => {\n const e = this.#atoms.get(atom) as AtomEntry<V> | undefined;\n if (e != null) {\n e.promise = null;\n }\n });\n entry.promise = promise;\n return promise;\n }\n\n async #runGet<V>(atom: Atom<V>): Promise<V> {\n const config: InternalAtomConfig<V> = atom[CONFIG];\n const dependencies = config.dependencies;\n const stalePolicy: StalePolicy = config.stalePolicy ?? \"keep\";\n const entry = this.#getOrCreate(atom);\n\n if (dependencies != null) {\n await Promise.all(Object.values(dependencies).map((dep) => this.resolve(dep)));\n this.#registerRdeps(atom, dependencies);\n }\n\n const ctx = this.#makeCtx(atom, dependencies ?? {});\n\n try {\n const value = await config.get(ctx);\n if (ctx.signal.aborted) {\n throw new DOMException(\"The operation was aborted\", \"AbortError\");\n }\n entry.state = { status: \"fresh\", value, error: undefined };\n entry.version++;\n this.#scheduleNotify(atom);\n return value;\n } catch (e: unknown) {\n if (isAbortError(e)) {\n throw e;\n }\n const prevValue = extractPreviousValue<V>(entry.state);\n entry.state = {\n status: \"error\",\n value: stalePolicy === \"keep\" ? prevValue : undefined,\n error: e,\n } as AtomState<V>;\n entry.version++;\n this.#scheduleNotify(atom);\n throw e;\n }\n }\n\n invalidate(atom: Atom<unknown>): void {\n this.#markStale(atom);\n this.#pending.add(atom);\n if (this.#pending.size === 1) {\n queueMicrotask(() => {\n this.#flushPending();\n });\n }\n }\n\n invalidateMany(atoms: ReadonlyArray<Atom<unknown>>): void {\n for (const a of atoms) {\n this.#markStale(a);\n this.#pending.add(a);\n }\n if (this.#pending.size > 0) {\n queueMicrotask(() => {\n this.#flushPending();\n });\n }\n }\n\n #markStale(atom: Atom<unknown>, visited = new Set<Atom<unknown>>()): void {\n if (visited.has(atom)) {\n return;\n }\n visited.add(atom);\n\n const entry = this.#atoms.get(atom);\n if (entry != null) {\n const stalePolicy: StalePolicy = atom[CONFIG].stalePolicy ?? \"keep\";\n\n if (entry.state.status === \"fresh\") {\n if (stalePolicy === \"reset\") {\n entry.state = PENDING_STATE;\n } else {\n entry.state = { ...entry.state, status: \"stale\" };\n }\n }\n\n entry.promise = null;\n this.#controllers.get(atom)?.abort();\n }\n\n const rdeps = this.#rdeps.get(atom);\n if (rdeps != null) {\n for (const dep of rdeps) {\n this.#markStale(dep, visited);\n }\n }\n }\n\n #markReverseDependentsStale(atom: Atom<unknown>): void {\n const rdeps = this.#rdeps.get(atom);\n if (rdeps != null) {\n const visited = new Set<Atom<unknown>>([atom]);\n for (const dep of rdeps) {\n this.#markStale(dep, visited);\n this.#pending.add(dep);\n }\n }\n }\n\n #flushPending(): void {\n const batch = [...this.#pending];\n this.#pending.clear();\n const toNotify = new Set<() => void>();\n for (const a of batch) {\n const entry = this.#atoms.get(a);\n if (entry != null) {\n for (const l of entry.listeners) {\n toNotify.add(l);\n }\n }\n }\n for (const l of toNotify) {\n l();\n }\n }\n\n #scheduleNotify(atom: Atom<unknown>): void {\n this.#pending.add(atom);\n if (this.#pending.size === 1) {\n queueMicrotask(() => {\n this.#flushPending();\n });\n }\n }\n\n #registerRdeps(atom: Atom<unknown>, deps: Record<string, Atom<unknown>>): void {\n for (const dep of Object.values(deps)) {\n let set = this.#rdeps.get(dep);\n if (set == null) {\n set = new Set();\n this.#rdeps.set(dep, set);\n }\n set.add(atom);\n }\n }\n\n #makeCtx(\n atom: Atom<unknown>,\n deps: Record<string, Atom<unknown>>,\n ): AtomContext<Record<string, Atom<unknown>>> {\n this.#controllers.get(atom)?.abort();\n const controller = new AbortController();\n this.#controllers.set(atom, controller);\n\n return {\n signal: controller.signal,\n get: async (key: string) => {\n const dep = deps[key];\n if (dep == null) {\n throw new Error(`Unknown dependency key: \"${String(key)}\"`);\n }\n return this.resolve(dep);\n },\n };\n }\n\n subscribe<V>(atom: Atom<V>, listener: () => void): () => void {\n const entry = this.#getOrCreate(atom);\n entry.listeners.add(listener);\n\n this.#autoRegisterFamily(atom);\n\n if (entry.state.status === \"pending\" && entry.promise == null) {\n void this.resolve(atom);\n }\n\n entry.mountCount++;\n if (entry.mountCount === 1) {\n const config: InternalAtomConfig<V> = atom[CONFIG];\n if (config.onMount != null) {\n const setValue = (value: V): void => {\n entry.state = { status: \"fresh\", value, error: undefined };\n entry.version++;\n this.#scheduleNotify(atom);\n };\n const cleanup = config.onMount(setValue as (value: unknown) => void);\n if (typeof cleanup === \"function\") {\n entry.unmount = cleanup;\n }\n }\n }\n\n return () => {\n entry.listeners.delete(listener);\n entry.mountCount--;\n if (entry.mountCount === 0 && entry.unmount != null) {\n entry.unmount();\n entry.unmount = null;\n }\n };\n }\n\n #autoRegisterFamily(atom: Atom<unknown>): void {\n const link = getFamilyLink(atom);\n if (link == null) {\n return;\n }\n if (this.#registeredFamilies.has(link)) {\n return;\n }\n this.#registeredFamilies.add(link);\n const unsub = link.registerStore((target: Atom<unknown>) => {\n this.invalidate(target);\n });\n this.#familyUnsubs.add(unsub);\n }\n\n getSnapshot<V>(atom: Atom<V>): AtomState<V> {\n return this.#getOrCreate(atom).state;\n }\n\n getServerSnapshot<V>(atom: Atom<V>): AtomState<V> {\n const entry = this.#atoms.get(atom) as AtomEntry<V> | undefined;\n if (entry != null) {\n return entry.state;\n }\n return PENDING_STATE as AtomState<V>;\n }\n\n async set<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>, ...args: A): Promise<void> {\n const config: InternalAtomConfig<V> = atom[CONFIG];\n if (config.set == null) {\n throw new Error(\n `Atom${atom.debugLabel != null ? ` \"${atom.debugLabel}\"` : \"\"} is not writable`,\n );\n }\n\n const deps = config.dependencies ?? {};\n const baseCtx = this.#makeCtx(atom, deps);\n const entry = this.#getOrCreate(atom);\n\n // Ref: assignments inside setOptimisticValue are not tracked on a plain `let` for catch narrowing.\n const optimisticSnapshot: {\n current: { state: AtomState<V>; version: number } | null;\n } = { current: null };\n\n const ctx: WritableAtomContext<Record<string, Atom<unknown>>, V> = {\n ...baseCtx,\n setOptimisticValue: (valueOrMutate: V | ((prev: V | undefined) => V)): void => {\n if (optimisticSnapshot.current == null) {\n optimisticSnapshot.current = { state: entry.state, version: entry.version };\n }\n const next =\n typeof valueOrMutate === \"function\"\n ? (valueOrMutate as (prev: V | undefined) => V)(extractPreviousValue(entry.state))\n : valueOrMutate;\n entry.state = { status: \"fresh\", value: next, error: undefined };\n entry.version++;\n this.#scheduleNotify(atom);\n this.#markReverseDependentsStale(atom);\n },\n };\n\n try {\n await config.set(ctx, ...args);\n } catch (e: unknown) {\n const snap = optimisticSnapshot.current;\n if (snap != null) {\n entry.state = snap.state;\n entry.version = snap.version;\n this.#scheduleNotify(atom);\n }\n throw e;\n }\n\n this.invalidate(atom);\n }\n\n write<V>(atom: Atom<V>, value: V): void;\n write<V>(atom: Atom<V>, mutate: (prev: V | undefined) => V): void;\n write<V>(atom: Atom<V>, valueOrMutate: V | ((prev: V | undefined) => V)): void {\n const entry = this.#getOrCreate(atom);\n this.#controllers.get(atom)?.abort();\n entry.promise = null;\n const next =\n typeof valueOrMutate === \"function\"\n ? (valueOrMutate as (prev: V | undefined) => V)(extractPreviousValue(entry.state))\n : valueOrMutate;\n entry.state = { status: \"fresh\", value: next, error: undefined };\n entry.version++;\n this.#scheduleNotify(atom);\n this.#markReverseDependentsStale(atom);\n }\n\n getClient(): StoreClient {\n if (this.#client != null) {\n return this.#client;\n }\n\n this.#client = {\n get: <V>(atom: Atom<V>): Promise<V> => this.resolve(atom),\n set: <V, A extends readonly unknown[]>(atom: WritableAtom<V, A>, ...args: A): Promise<void> =>\n this.set(atom, ...args),\n write: <V>(atom: Atom<V>, valueOrMutate: V | ((prev: V | undefined) => V)): void =>\n this.write(atom, valueOrMutate as V),\n invalidate: (atom: Atom<unknown>): void => this.invalidate(atom),\n invalidateMany: (atoms: ReadonlyArray<Atom<unknown>>): void => this.invalidateMany(atoms),\n subscribe: <V>(atom: Atom<V>, listener: (state: AtomState<V>) => void): (() => void) =>\n this.subscribe(atom, () => {\n listener(this.getSnapshot(atom));\n }),\n };\n return this.#client;\n }\n}\n\nexport function createStore(): Store {\n return new Store();\n}\n\nfunction isAbortError(e: unknown): boolean {\n return e instanceof DOMException && e.name === \"AbortError\";\n}\n\nfunction extractPreviousValue<V>(state: AtomState<V>): V | undefined {\n if (state.status === \"stale\" || state.status === \"fresh\") {\n return state.value;\n }\n if (state.status === \"error\") {\n return state.value;\n }\n return undefined;\n}\n"],"mappings":";AAAA,MAAa,SAAS,OAAO,eAAe;AAC5C,MAAa,WAAW,OAAO,iBAAiB;AAChD,MAAa,cAAc,OAAO,mBAAmB;;;AC8BrD,SAAgB,KAId,QAAoD;CACpD,MAAM,WAAsC,OAAO,OAAO,KAAK;AAC/D,UAAS,MAAM,OAAO;AAEtB,KAAI,OAAO,cAAc,KACvB,UAAS,aAAa,OAAO;AAE/B,KAAI,OAAO,gBAAgB,KACzB,UAAS,eAAe,OAAO;AAEjC,KAAI,OAAO,eAAe,KACxB,UAAS,cAAc,OAAO;AAEhC,KAAI,OAAO,OAAO,KAChB,UAAS,MAAM,OAAO;AAExB,KAAI,OAAO,WAAW,KACpB,UAAS,UAAU,OAAO;AAG5B,KAAI,OAAO,OAAO,KAChB,QAAO;GACJ,SAAS;GACT,WAAW,EAAE;EACd,YAAY,OAAO;EACpB;AAGH,QAAO;GACJ,SAAS;EACV,YAAY,OAAO;EACpB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GpB,SAAS,aAAa,GAAqB;AACzC,QAAO,aAAa,gBAAgB,EAAE,SAAS;;AAGjD,SAAS,qBAAwB,OAAoC;AACnE,KAAI,MAAM,WAAW,WAAW,MAAM,WAAW,QAC/C,QAAO,MAAM;AAEf,KAAI,MAAM,WAAW,QACnB,QAAO,MAAM"}
|
package/dist/vue/index.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/vue/provider.ts","../../src/vue/use-atom-value.ts","../../src/vue/use-set-atom.ts","../../src/vue/use-atom.ts","../../src/vue/use-atom-context.ts"],"mappings":";;;;;cAKa,QAAA,QAAQ,eAAA,CAGgB,KAAA,CAHhB,gBAAA;;UAGQ,QAAA,CAAS,KAAA;;;sBAHjB,KAAA,CAAA,YAAA;;gIAGgB,KAAA,CAAA,gBAAA;;UAAR,QAAA,CAAS,KAAA;;;;iBAQtB,QAAA,CAAA,GAAY,KAAA;;;KCZvB,aAAA;EACH,KAAA,EAAO,CAAA;EACP,OAAA;EACA,KAAA;AAAA;AAAA,KAGU,kBAAA,MAAwB,QAAA,CAAS,UAAA,CAAW,CAAA,KAAM,WAAA,CAAY,QAAA,CAAS,UAAA,CAAW,CAAA;AAAA,iBAE9E,YAAA,GAAA,CAAgB,IAAA,EAAM,IAAA,CAAK,CAAA,IAAK,kBAAA,CAAmB,CAAA;AAAA,iBACnD,YAAA,GAAA,CACd,IAAA,EAAM,IAAA,CAAK,CAAA,GACX,IAAA;EAAQ,OAAA;AAAA,IACP,kBAAA,CAAmB,aAAA,CAAc,CAAA;;;iBCbpB,UAAA,iCAAA,CACd,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,QACjB,IAAA,EAAM,CAAA,KAAM,OAAA;;;KCDd,iBAAA,8CACH,kBAAA,CAAmB,CAAA,OACf,IAAA,EAAM,CAAA,KAAM,OAAA,UAEhB,WAAA,WAAsB,kBAAA,CAAmB,CAAA,OAAQ,IAAA,EAAM,CAAA,KAAM,OAAA;AAAA,iBAE/C,OAAA,iCAAA,CACd,IAAA,EAAM,YAAA,CAAa,CAAA,EAAG,CAAA,IACrB,iBAAA,CAAkB,CAAA,EAAG,CAAA;;;iBCTR,cAAA,GAAA,CAAkB,QAAA,GAAW,GAAA,EAAK,WAAA,KAAgB,OAAA,CAAQ,CAAA,UAAW,OAAA,CAAQ,CAAA"}
|
package/dist/vue/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/vue/provider.ts","../../src/vue/use-atom-value.ts","../../src/vue/use-set-atom.ts","../../src/vue/use-atom.ts","../../src/vue/use-atom-context.ts"],"sourcesContent":["import { type InjectionKey, type PropType, defineComponent, inject, provide } from \"vue\";\nimport { Store } from \"@/internal/store.js\";\n\nconst StoreKey: InjectionKey<Store> = Symbol(\"kvark-store\");\n\nexport const Provider = defineComponent({\n name: \"KvarkProvider\",\n props: {\n store: { type: Object as PropType<Store>, required: true },\n },\n setup(props, { slots }) {\n provide(StoreKey, props.store);\n return () => slots[\"default\"]?.();\n },\n});\n\nexport function useStore(): Store {\n const store = inject(StoreKey);\n if (store == null) {\n throw new Error(\"useStore must be used within a <Provider>\");\n }\n return store;\n}\n","import { type ShallowRef, readonly, shallowRef, watchEffect } from \"vue\";\nimport type { Atom, AtomState } from \"@/internal/types.js\";\nimport { useStore } from \"@/vue/provider.js\";\n\ntype ObservedValue<V> = {\n value: V;\n isStale: boolean;\n error: unknown;\n};\n\nexport type ThenableShallowRef<V> = Readonly<ShallowRef<V>> & PromiseLike<Readonly<ShallowRef<V>>>;\n\nexport function useAtomValue<V>(atom: Atom<V>): ThenableShallowRef<V>;\nexport function useAtomValue<V>(\n atom: Atom<V>,\n opts: { observe: true },\n): ThenableShallowRef<ObservedValue<V>>;\nexport function useAtomValue<V>(\n atom: Atom<V>,\n opts?: { observe: true },\n): ThenableShallowRef<V | ObservedValue<V>> {\n const store = useStore();\n\n function derive(snapshot: AtomState<V>): V | ObservedValue<V> {\n if (snapshot.status === \"pending\") {\n void store.resolve(atom);\n if (opts?.observe === true) {\n return { value: undefined as V, isStale: false, error: undefined };\n }\n return undefined as V;\n }\n\n if (snapshot.status === \"stale\") {\n void store.resolve(atom);\n if (opts?.observe === true) {\n return { value: snapshot.value, isStale: true, error: undefined };\n }\n return snapshot.value;\n }\n\n if (snapshot.status === \"error\") {\n if (opts?.observe === true && snapshot.value !== undefined) {\n return { value: snapshot.value as V, isStale: false, error: snapshot.error };\n }\n throw snapshot.error;\n }\n\n // status === \"fresh\"\n if (opts?.observe === true) {\n return { value: snapshot.value, isStale: false, error: undefined };\n }\n return snapshot.value;\n }\n\n const result = shallowRef(derive(store.getSnapshot(atom)));\n\n watchEffect((onCleanup) => {\n const unsub = store.subscribe(atom, () => {\n result.value = derive(store.getSnapshot(atom));\n });\n onCleanup(unsub);\n });\n\n const ref = readonly(result) as Readonly<ShallowRef<V | ObservedValue<V>>>;\n\n return new Proxy(ref, {\n get(target, prop, receiver) {\n if (prop === \"then\") {\n return (onFulfilled?: (v: typeof ref) => unknown, onRejected?: (e: unknown) => unknown) =>\n store\n .resolve(atom)\n .then(() => ref, onRejected)\n .then(onFulfilled as never);\n }\n return Reflect.get(target, prop, receiver);\n },\n }) as ThenableShallowRef<V | ObservedValue<V>>;\n}\n","import type { WritableAtom } from \"@/internal/types.js\";\nimport { useStore } from \"@/vue/provider.js\";\n\nexport function useSetAtom<V, A extends readonly unknown[]>(\n atom: WritableAtom<V, A>,\n): (...args: A) => Promise<void> {\n const store = useStore();\n return (...args: A): Promise<void> => store.set(atom, ...args);\n}\n","import type { WritableAtom } from \"@/internal/types.js\";\nimport { type ThenableShallowRef, useAtomValue } from \"@/vue/use-atom-value.js\";\nimport { useSetAtom } from \"@/vue/use-set-atom.js\";\n\ntype ThenableAtomTuple<V, A extends readonly unknown[]> = readonly [\n ThenableShallowRef<V>,\n (...args: A) => Promise<void>,\n] &\n PromiseLike<readonly [ThenableShallowRef<V>, (...args: A) => Promise<void>]>;\n\nexport function useAtom<V, A extends readonly unknown[]>(\n atom: WritableAtom<V, A>,\n): ThenableAtomTuple<V, A> {\n const value = useAtomValue(atom);\n const setter = useSetAtom(atom);\n const tuple = [value, setter] as const;\n\n return Object.assign(tuple, {\n then(onFulfilled?: (v: typeof tuple) => unknown, onRejected?: (e: unknown) => unknown) {\n return Promise.resolve(value)\n .then(() => [value, setter] as const, onRejected)\n .then(onFulfilled as never);\n },\n }) as ThenableAtomTuple<V, A>;\n}\n","import type { StoreClient } from \"@/internal/store.js\";\nimport { useStore } from \"@/vue/provider.js\";\n\nexport function useAtomContext<R>(callback: (ctx: StoreClient) => Promise<R>): () => Promise<R> {\n const store = useStore();\n return (): Promise<R> => callback(store.getClient());\n}\n"],"mappings":";;AAGA,MAAM,WAAgC,OAAO,cAAc;AAE3D,MAAa,WAAW,gBAAgB;CACtC,MAAM;CACN,OAAO,EACL,OAAO;EAAE,MAAM;EAA2B,UAAU;EAAM,EAC3D;CACD,MAAM,OAAO,EAAE,SAAS;AACtB,UAAQ,UAAU,MAAM,MAAM;AAC9B,eAAa,MAAM,cAAc;;CAEpC,CAAC;AAEF,SAAgB,WAAkB;CAChC,MAAM,QAAQ,OAAO,SAAS;AAC9B,KAAI,SAAS,KACX,OAAM,IAAI,MAAM,4CAA4C;AAE9D,QAAO;;;;ACJT,SAAgB,aACd,MACA,MAC0C;CAC1C,MAAM,QAAQ,UAAU;CAExB,SAAS,OAAO,UAA8C;AAC5D,MAAI,SAAS,WAAW,WAAW;AAC5B,SAAM,QAAQ,KAAK;AACxB,OAAI,MAAM,YAAY,KACpB,QAAO;IAAE,OAAO,KAAA;IAAgB,SAAS;IAAO,OAAO,KAAA;IAAW;AAEpE;;AAGF,MAAI,SAAS,WAAW,SAAS;AAC1B,SAAM,QAAQ,KAAK;AACxB,OAAI,MAAM,YAAY,KACpB,QAAO;IAAE,OAAO,SAAS;IAAO,SAAS;IAAM,OAAO,KAAA;IAAW;A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