@kdeveloper/kvark 0.14.0 → 0.14.1

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.
Files changed (2) hide show
  1. package/README.md +50 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -509,6 +509,56 @@ postFamily.invalidate(42);
509
509
  postFamily.invalidateAll();
510
510
  ```
511
511
 
512
+ ### `atomFamily` as a dependency
513
+
514
+ A `dependencies` entry can be either a regular `Atom` or an `atomFamily`. When the entry is a family, supply the param at the call site as the **second positional argument** to `ctx.get`. Each unique param resolves to its own family member; reverse-dependency edges are wired up automatically, so invalidating any read member (or the whole family) propagates `stale` to the consumer.
515
+
516
+ ```ts
517
+ import { atom } from "@kdeveloper/kvark";
518
+ import { atomFamily } from "@kdeveloper/kvark/family";
519
+
520
+ const userFamily = atomFamily({
521
+ get: (id: number) => async () => fetch(`/api/users/${id}`).then((r) => r.json()),
522
+ });
523
+
524
+ const profileAtom = atom({
525
+ dependencies: { user: userFamily },
526
+ get: async (ctx) => {
527
+ const user = await ctx.get("user", 7); // resolves userFamily(7)
528
+ return `profile-of-${user.name}`;
529
+ },
530
+ });
531
+
532
+ // Invalidating a read family member cascades to consumers
533
+ userFamily.invalidate(7); // marks profileAtom stale
534
+ ```
535
+
536
+ The signature of `ctx.get` is keyed on the dependency entry:
537
+
538
+ | Entry | Call form | Compile-time errors |
539
+ | ------------ | --------------------- | ---------------------------------------------------- |
540
+ | `Atom<V>` | `ctx.get(key)` | passing a second arg → error |
541
+ | `atomFamily` | `ctx.get(key, param)` | omitting `param` → error; wrong `param` type → error |
542
+
543
+ ```ts
544
+ const a = atom({
545
+ dependencies: { base: countAtom, user: userFamily },
546
+ get: async (ctx) => {
547
+ await ctx.get("base"); // ok
548
+ await ctx.get("user", 1); // ok
549
+
550
+ // @ts-expect-error family-key requires a parameter
551
+ ctx.get("user");
552
+ // @ts-expect-error plain atom-key does not accept a parameter
553
+ ctx.get("base", 1);
554
+ // @ts-expect-error param type must match the family's `Param`
555
+ ctx.get("user", "not-a-number");
556
+ },
557
+ });
558
+ ```
559
+
560
+ Repeated reads with the same `param` are deduplicated through the in-flight promise of the resolved family member; reads with different params resolve to distinct atoms (and run independently). Family-typed dependencies are **not** pre-resolved before `get` runs (the param is only known at the `ctx.get` call site), so unread members never trigger a fetch.
561
+
512
562
  ### Object params and `paramKey`
513
563
 
514
564
  By default atoms are cached by **reference** (`===`). When you want equality-by-value — for example when the param is a filter object — supply a `paramKey` function that maps the param to a stable primitive key:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kdeveloper/kvark",
3
- "version": "0.14.0",
3
+ "version": "0.14.1",
4
4
  "description": "Atomic state management with explicit dependency graphs",
5
5
  "license": "MIT",
6
6
  "files": [