@kdeveloper/kvark 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -3
- package/dist/{index.d.ts → atom-CtZVA4ka.d.mts} +6 -5
- package/dist/atom-CtZVA4ka.d.mts.map +1 -0
- package/dist/family.d.mts +24 -0
- package/dist/family.d.mts.map +1 -0
- package/dist/family.mjs +2 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +2 -0
- package/dist/react/index.d.mts +36 -0
- package/dist/react/index.d.mts.map +1 -0
- package/dist/react/index.mjs +69 -0
- package/dist/react/index.mjs.map +1 -0
- package/dist/store-BbmNU3xn.d.mts +80 -0
- package/dist/store-BbmNU3xn.d.mts.map +1 -0
- package/dist/store-CmRXgcwh.mjs +404 -0
- package/dist/store-CmRXgcwh.mjs.map +1 -0
- package/package.json +13 -14
- package/dist/chunk-QWP5ITSY.js +0 -464
- package/dist/chunk-QWP5ITSY.js.map +0 -1
- package/dist/family.d.ts +0 -23
- package/dist/family.js +0 -11
- package/dist/family.js.map +0 -1
- package/dist/index.js +0 -9
- package/dist/index.js.map +0 -1
- package/dist/react/index.d.ts +0 -27
- package/dist/react/index.js +0 -87
- package/dist/react/index.js.map +0 -1
- package/dist/store-CAz6uZcN.d.ts +0 -75
package/README.md
CHANGED
|
@@ -296,12 +296,12 @@ Combines `useAtomValue` and `useSetAtom` into a `[value, setter]` tuple.
|
|
|
296
296
|
const [count, setCount] = useAtom(countAtom);
|
|
297
297
|
```
|
|
298
298
|
|
|
299
|
-
### `
|
|
299
|
+
### `useAtomContext`
|
|
300
300
|
|
|
301
301
|
Imperative access to the `StoreClient` inside a callback. Does not subscribe.
|
|
302
302
|
|
|
303
303
|
```tsx
|
|
304
|
-
const readBalance =
|
|
304
|
+
const readBalance = useAtomContext(async (client) => {
|
|
305
305
|
return client.get(balanceAtom);
|
|
306
306
|
});
|
|
307
307
|
|
|
@@ -348,12 +348,28 @@ const unsub = client.subscribe(userAtom, (state) => {
|
|
|
348
348
|
});
|
|
349
349
|
```
|
|
350
350
|
|
|
351
|
+
### Direct Write
|
|
352
|
+
|
|
353
|
+
When the server pushes a complete, authoritative value (e.g. via WebSocket or SSE), use `client.write` to store it directly — no refetch through `get` is triggered. Any in-flight `get` for the atom is aborted, and derived atoms that depend on it are marked `stale` so they re-compute.
|
|
354
|
+
|
|
355
|
+
```ts
|
|
356
|
+
ws.addEventListener("message", (event) => {
|
|
357
|
+
const msg = JSON.parse(event.data);
|
|
358
|
+
if (msg.type === "post.updated") {
|
|
359
|
+
client.write(postFamily(msg.postId), msg.post);
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
`write` works on any `Atom<V>` — the atom does not need a `set` config. This makes it the right tool when you already have the final value and want to skip a redundant network round-trip.
|
|
365
|
+
|
|
351
366
|
### `StoreClient` interface
|
|
352
367
|
|
|
353
368
|
```ts
|
|
354
369
|
interface StoreClient {
|
|
355
370
|
get<V>(atom: Atom<V>): Promise<V>;
|
|
356
371
|
set<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>, ...args: A): Promise<void>;
|
|
372
|
+
write<V>(atom: Atom<V>, value: V): void;
|
|
357
373
|
invalidate(atom: Atom<unknown>): void;
|
|
358
374
|
invalidateMany(atoms: ReadonlyArray<Atom<unknown>>): void;
|
|
359
375
|
subscribe<V>(atom: Atom<V>, listener: (state: AtomState<V>) => void): () => void;
|
|
@@ -395,7 +411,7 @@ type Writable = IsWritable<typeof countAtom>; // → true | false
|
|
|
395
411
|
| Import | Contents |
|
|
396
412
|
| -------------------------- | ---------------------------------------------------------------------- |
|
|
397
413
|
| `@kdeveloper/kvark` | `atom`, `createStore`, all types |
|
|
398
|
-
| `@kdeveloper/kvark/react` | `Provider`, `useAtomValue`, `useSetAtom`, `useAtom`, `
|
|
414
|
+
| `@kdeveloper/kvark/react` | `Provider`, `useAtomValue`, `useSetAtom`, `useAtom`, `useAtomContext` |
|
|
399
415
|
| `@kdeveloper/kvark/family` | `atomFamily` |
|
|
400
416
|
|
|
401
417
|
The core (`@kdeveloper/kvark`) has **zero runtime dependencies**. React is a peer dependency only needed when using the `/react` subpath.
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { i as AtomArgs, a as AtomContext, c as AtomState, d as AtomValue, I as IsWritable, S as StalePolicy, g as StoreClient, e as createStore } from './store-CAz6uZcN.js';
|
|
1
|
+
import { f as WritableAtom, i as Atom, o as AtomConfig, p as WritableAtomContext } from "./store-BbmNU3xn.mjs";
|
|
3
2
|
|
|
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> & {
|
|
5
|
-
|
|
5
|
+
set: (ctx: WritableAtomContext<Deps, Value>, ...args: Args) => Promise<void>;
|
|
6
6
|
};
|
|
7
7
|
type ReadonlyConfig<Value, Deps extends Record<string, Atom<unknown>>> = Omit<AtomConfig<Value, Deps, readonly []>, "set">;
|
|
8
8
|
declare function atom<Value, Deps extends Record<string, Atom<unknown>> = Record<never, never>, Args extends readonly unknown[] = readonly []>(config: WritableConfig<Value, Deps, Args>): WritableAtom<Value, Args>;
|
|
9
9
|
declare function atom<Value, Deps extends Record<string, Atom<unknown>> = Record<never, never>>(config: ReadonlyConfig<Value, Deps>): Atom<Value>;
|
|
10
|
-
|
|
11
|
-
export {
|
|
10
|
+
//#endregion
|
|
11
|
+
export { atom as t };
|
|
12
|
+
//# sourceMappingURL=atom-CtZVA4ka.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atom-CtZVA4ka.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,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"}
|
|
@@ -0,0 +1,24 @@
|
|
|
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-BbmNU3xn.mjs";
|
|
2
|
+
import { t as atom } from "./atom-CtZVA4ka.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/internal/family.d.ts
|
|
5
|
+
type AtomFamilyOptions<Param, Value, Deps extends Record<string, Atom<unknown>>, Args extends readonly unknown[]> = {
|
|
6
|
+
dependencies?: (param: Param) => Deps;
|
|
7
|
+
stalePolicy?: StalePolicy;
|
|
8
|
+
cachePolicy?: "keep-all" | "lru";
|
|
9
|
+
lruSize?: number;
|
|
10
|
+
debugLabel?: string;
|
|
11
|
+
get: (param: Param) => (ctx: AtomContext<Deps>) => Promise<Value>;
|
|
12
|
+
set?: (param: Param) => (ctx: AtomContext<Deps>, ...args: Args) => Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
interface AtomFamily<Param, Value> {
|
|
15
|
+
(param: Param): Atom<Value>;
|
|
16
|
+
invalidate(param: Param): void;
|
|
17
|
+
invalidateAll(): void;
|
|
18
|
+
remove(param: Param): void;
|
|
19
|
+
getCache(): ReadonlyMap<Param, Atom<Value>>;
|
|
20
|
+
}
|
|
21
|
+
declare function atomFamily<Param, Value, Deps extends Record<string, Atom<unknown>> = Record<never, never>, Args extends readonly unknown[] = readonly []>(options: AtomFamilyOptions<Param, Value, Deps, Args>): AtomFamily<Param, Value>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { type Atom, type AtomConfig, type AtomContext, type AtomFamily, type AtomFamilyOptions, type AtomState, type AtomValue, type StalePolicy, type WritableAtom, atom, atomFamily, createStore };
|
|
24
|
+
//# sourceMappingURL=family.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"family.d.mts","names":[],"sources":["../src/internal/family.ts"],"mappings":";;;;KAIY,iBAAA,4BAGG,MAAA,SAAe,IAAA;EAG5B,YAAA,IAAgB,KAAA,EAAO,KAAA,KAAU,IAAA;EACjC,WAAA,GAAc,WAAA;EACd,WAAA;EACA,OAAA;EACA,UAAA;EACA,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;EAAA,CACd,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,KAAA,EAAO,IAAA,CAAK,KAAA;AAAA;AAAA,iBAYtB,UAAA,4BAGD,MAAA,SAAe,IAAA,aAAiB,MAAA,8DAAA,CAE7C,OAAA,EAAS,iBAAA,CAAkB,KAAA,EAAO,KAAA,EAAO,IAAA,EAAM,IAAA,IAAQ,UAAA,CAAW,KAAA,EAAO,KAAA"}
|
package/dist/family.mjs
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +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-BbmNU3xn.mjs";
|
|
2
|
+
import { t as atom } from "./atom-CtZVA4ka.mjs";
|
|
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.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { f as WritableAtom, i as Atom, n as StoreClient, t as Store } from "../store-BbmNU3xn.mjs";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/react/provider.d.ts
|
|
5
|
+
type ProviderProps = {
|
|
6
|
+
store: Store;
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
};
|
|
9
|
+
declare function Provider({
|
|
10
|
+
store,
|
|
11
|
+
children
|
|
12
|
+
}: ProviderProps): ReactNode;
|
|
13
|
+
declare function useStore(): Store;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/react/use-atom-value.d.ts
|
|
16
|
+
type ObservedValue<V> = {
|
|
17
|
+
value: V;
|
|
18
|
+
isStale: boolean;
|
|
19
|
+
error: unknown;
|
|
20
|
+
};
|
|
21
|
+
declare function useAtomValue<V>(atom: Atom<V>): V;
|
|
22
|
+
declare function useAtomValue<V>(atom: Atom<V>, opts: {
|
|
23
|
+
observe: true;
|
|
24
|
+
}): ObservedValue<V>;
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/react/use-set-atom.d.ts
|
|
27
|
+
declare function useSetAtom<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>): (...args: A) => Promise<void>;
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/react/use-atom.d.ts
|
|
30
|
+
declare function useAtom<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>): readonly [V, (...args: A) => Promise<void>];
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/react/use-atom-context.d.ts
|
|
33
|
+
declare function useAtomContext<R>(callback: (ctx: StoreClient) => Promise<R>): () => Promise<R>;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { Provider, useAtom, useAtomContext, useAtomValue, useSetAtom, useStore };
|
|
36
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { i as CONFIG } from "../store-CmRXgcwh.mjs";
|
|
2
|
+
import { createContext, useCallback, useContext, useSyncExternalStore } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
//#region src/react/provider.tsx
|
|
5
|
+
const StoreContext = createContext(null);
|
|
6
|
+
function Provider({ store, children }) {
|
|
7
|
+
return /* @__PURE__ */ jsx(StoreContext, {
|
|
8
|
+
value: store,
|
|
9
|
+
children
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
function useStore() {
|
|
13
|
+
const store = useContext(StoreContext);
|
|
14
|
+
if (store == null) throw new Error("useStore must be used within a <Provider>");
|
|
15
|
+
return store;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/react/use-atom-value.ts
|
|
19
|
+
function useAtomValue(atom, opts) {
|
|
20
|
+
const store = useStore();
|
|
21
|
+
const snapshot = useSyncExternalStore((notify) => store.subscribe(atom, notify), () => store.getSnapshot(atom), () => store.getServerSnapshot(atom));
|
|
22
|
+
const stalePolicy = atom[CONFIG].stalePolicy ?? "keep";
|
|
23
|
+
if (snapshot.status === "pending") throw store.resolve(atom);
|
|
24
|
+
if (snapshot.status === "stale") {
|
|
25
|
+
if (stalePolicy === "suspend") throw store.resolve(atom);
|
|
26
|
+
store.resolve(atom);
|
|
27
|
+
if (opts?.observe === true) return {
|
|
28
|
+
value: snapshot.value,
|
|
29
|
+
isStale: true,
|
|
30
|
+
error: void 0
|
|
31
|
+
};
|
|
32
|
+
return snapshot.value;
|
|
33
|
+
}
|
|
34
|
+
if (snapshot.status === "error") {
|
|
35
|
+
if (opts?.observe === true && snapshot.value !== void 0) return {
|
|
36
|
+
value: snapshot.value,
|
|
37
|
+
isStale: false,
|
|
38
|
+
error: snapshot.error
|
|
39
|
+
};
|
|
40
|
+
throw snapshot.error;
|
|
41
|
+
}
|
|
42
|
+
if (opts?.observe === true) return {
|
|
43
|
+
value: snapshot.value,
|
|
44
|
+
isStale: false,
|
|
45
|
+
error: void 0
|
|
46
|
+
};
|
|
47
|
+
return snapshot.value;
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/react/use-set-atom.ts
|
|
51
|
+
function useSetAtom(atom) {
|
|
52
|
+
const store = useStore();
|
|
53
|
+
return useCallback((...args) => store.set(atom, ...args), [store, atom]);
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/react/use-atom.ts
|
|
57
|
+
function useAtom(atom) {
|
|
58
|
+
return [useAtomValue(atom), useSetAtom(atom)];
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/react/use-atom-context.ts
|
|
62
|
+
function useAtomContext(callback) {
|
|
63
|
+
const store = useStore();
|
|
64
|
+
return useCallback(() => callback(store.getClient()), [store, callback]);
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { Provider, useAtom, useAtomContext, useAtomValue, useSetAtom, useStore };
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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 \"./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 \"./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 \"./use-atom-value.js\";\nimport { useSetAtom } from \"./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 \"./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,SAAS,EAAE,OAAO,YAAsC;AACtE,QAAO,oBAAC,cAAD;EAAc,OAAO;EAAQ;EAAwB,CAAA;;AAG9D,SAAgB,WAAkB;CAChC,MAAM,QAAQ,WAAW,aAAa;AACtC,KAAI,SAAS,KACX,OAAM,IAAI,MAAM,4CAA4C;AAE9D,QAAO;;;;ACNT,SAAgB,aAAgB,MAAe,MAAgD;CAC7F,MAAM,QAAQ,UAAU;CAExB,MAAM,WAAyB,sBAC5B,WAAW,MAAM,UAAU,MAAM,OAAO,QACnC,MAAM,YAAY,KAAK,QACvB,MAAM,kBAAkB,KAAK,CACpC;CAED,MAAM,cAA2B,KAAK,QAAQ,eAAe;AAE7D,KAAI,SAAS,WAAW,UACtB,OAAM,MAAM,QAAQ,KAAK;AAG3B,KAAI,SAAS,WAAW,SAAS;AAC/B,MAAI,gBAAgB,UAClB,OAAM,MAAM,QAAQ,KAAK;AAGtB,QAAM,QAAQ,KAAK;AAExB,MAAI,MAAM,YAAY,KACpB,QAAO;GAAE,OAAO,SAAS;GAAO,SAAS;GAAM,OAAO,KAAA;GAAW;AAEnE,SAAO,SAAS;;AAGlB,KAAI,SAAS,WAAW,SAAS;AAC/B,MAAI,MAAM,YAAY,QAAQ,SAAS,UAAU,KAAA,EAC/C,QAAO;GACL,OAAO,SAAS;GAChB,SAAS;GACT,OAAO,SAAS;GACjB;AAEH,QAAM,SAAS;;AAIjB,KAAI,MAAM,YAAY,KACpB,QAAO;EAAE,OAAO,SAAS;EAAO,SAAS;EAAO,OAAO,KAAA;EAAW;AAEpE,QAAO,SAAS;;;;ACpDlB,SAAgB,WACd,MAC+B;CAC/B,MAAM,QAAQ,UAAU;AACxB,QAAO,aAAa,GAAG,SAA2B,MAAM,IAAI,MAAM,GAAG,KAAK,EAAE,CAAC,OAAO,KAAK,CAAC;;;;ACJ5F,SAAgB,QACd,MAC6C;AAG7C,QAAO,CAFO,aAAa,KAAK,EACjB,WAAW,KAAK,CACT;;;;ACLxB,SAAgB,eAAkB,UAA8D;CAC9F,MAAM,QAAQ,UAAU;AACxB,QAAO,kBAA8B,SAAS,MAAM,WAAW,CAAC,EAAE,CAAC,OAAO,SAAS,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//#region src/internal/types.d.ts
|
|
2
|
+
declare const CONFIG: unique symbol;
|
|
3
|
+
declare const WRITABLE: unique symbol;
|
|
4
|
+
type AtomState<Value> = {
|
|
5
|
+
status: "pending";
|
|
6
|
+
value: undefined;
|
|
7
|
+
error: undefined;
|
|
8
|
+
} | {
|
|
9
|
+
status: "stale";
|
|
10
|
+
value: Value;
|
|
11
|
+
error: undefined;
|
|
12
|
+
} | {
|
|
13
|
+
status: "fresh";
|
|
14
|
+
value: Value;
|
|
15
|
+
error: undefined;
|
|
16
|
+
} | {
|
|
17
|
+
status: "error";
|
|
18
|
+
value: Value | undefined;
|
|
19
|
+
error: unknown;
|
|
20
|
+
};
|
|
21
|
+
type StalePolicy = "keep" | "suspend" | "reset";
|
|
22
|
+
type AtomContext<Deps extends Record<string, Atom<unknown>>> = {
|
|
23
|
+
get: <K extends keyof Deps>(key: K) => Promise<AtomValue<Deps[K]>>;
|
|
24
|
+
signal: AbortSignal;
|
|
25
|
+
};
|
|
26
|
+
type WritableAtomContext<Deps extends Record<string, Atom<unknown>>, Value> = AtomContext<Deps> & {
|
|
27
|
+
setOptimisticValue: (value: Value) => void;
|
|
28
|
+
};
|
|
29
|
+
type AtomConfig<Value, Deps extends Record<string, Atom<unknown>>, Args extends readonly unknown[]> = {
|
|
30
|
+
dependencies?: Deps;
|
|
31
|
+
stalePolicy?: StalePolicy;
|
|
32
|
+
debugLabel?: string;
|
|
33
|
+
get: (ctx: AtomContext<Deps>) => Promise<Value>;
|
|
34
|
+
set?: (ctx: WritableAtomContext<Deps, Value>, ...args: Args) => Promise<void>;
|
|
35
|
+
onMount?: (set: (value: Value) => void) => (() => void) | void;
|
|
36
|
+
};
|
|
37
|
+
interface InternalAtomConfig<Value> {
|
|
38
|
+
dependencies?: Record<string, Atom<unknown>>;
|
|
39
|
+
stalePolicy?: StalePolicy;
|
|
40
|
+
debugLabel?: string;
|
|
41
|
+
get: (ctx: AtomContext<Record<string, Atom<unknown>>>) => Promise<Value>;
|
|
42
|
+
set?: (ctx: WritableAtomContext<Record<string, Atom<unknown>>, Value>, ...args: readonly unknown[]) => Promise<void>;
|
|
43
|
+
onMount?: (set: (value: unknown) => void) => (() => void) | void;
|
|
44
|
+
}
|
|
45
|
+
interface Atom<out Value> {
|
|
46
|
+
readonly [CONFIG]: InternalAtomConfig<Value>;
|
|
47
|
+
readonly debugLabel: string | undefined;
|
|
48
|
+
}
|
|
49
|
+
interface WritableAtom<out Value, in out Args extends readonly unknown[]> extends Atom<Value> {
|
|
50
|
+
readonly [WRITABLE]: Args;
|
|
51
|
+
}
|
|
52
|
+
type AtomValue<A> = A extends Atom<infer V> ? V : never;
|
|
53
|
+
type IsWritable<A> = A extends WritableAtom<unknown, readonly unknown[]> ? true : false;
|
|
54
|
+
type AtomArgs<A> = A extends WritableAtom<unknown, infer Args> ? Args : never;
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/internal/store.d.ts
|
|
57
|
+
interface StoreClient {
|
|
58
|
+
get<V>(atom: Atom<V>): Promise<V>;
|
|
59
|
+
set<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>, ...args: A): Promise<void>;
|
|
60
|
+
write<V>(atom: Atom<V>, value: V): void;
|
|
61
|
+
invalidate(atom: Atom<unknown>): void;
|
|
62
|
+
invalidateMany(atoms: ReadonlyArray<Atom<unknown>>): void;
|
|
63
|
+
subscribe<V>(atom: Atom<V>, listener: (state: AtomState<V>) => void): () => void;
|
|
64
|
+
}
|
|
65
|
+
declare class Store {
|
|
66
|
+
#private;
|
|
67
|
+
resolve<V>(atom: Atom<V>): Promise<V>;
|
|
68
|
+
invalidate(atom: Atom<unknown>): void;
|
|
69
|
+
invalidateMany(atoms: ReadonlyArray<Atom<unknown>>): void;
|
|
70
|
+
subscribe<V>(atom: Atom<V>, listener: () => void): () => void;
|
|
71
|
+
getSnapshot<V>(atom: Atom<V>): AtomState<V>;
|
|
72
|
+
getServerSnapshot<V>(atom: Atom<V>): AtomState<V>;
|
|
73
|
+
set<V, A extends readonly unknown[]>(atom: WritableAtom<V, A>, ...args: A): Promise<void>;
|
|
74
|
+
write<V>(atom: Atom<V>, value: V): void;
|
|
75
|
+
getClient(): StoreClient;
|
|
76
|
+
}
|
|
77
|
+
declare function createStore(): Store;
|
|
78
|
+
//#endregion
|
|
79
|
+
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 };
|
|
80
|
+
//# sourceMappingURL=store-BbmNU3xn.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-BbmNU3xn.d.mts","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,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