|
1 | 1 | import { Atom } from 'jotai'
|
2 | 2 |
|
3 |
| -export function atomTree<Param, AtomType extends Atom<unknown>>( |
4 |
| - initializeAtom: (path: [Param, ...Param[]]) => AtomType |
5 |
| -) { |
6 |
| - type Node = { m?: Map<Param, Node>; v?: AtomType } |
7 |
| - const root = new Map<Param, Node>() |
8 |
| - |
9 |
| - const createAtom = (path: [Param, ...Param[]]): AtomType => { |
10 |
| - if (path.length === 0) { |
11 |
| - throw new Error('Path must have at least one key') |
12 |
| - } |
13 |
| - let current = root |
14 |
| - for (let i = 0; i < path.length; i++) { |
15 |
| - const key = path[i]! |
16 |
| - let node = current.get(key) |
17 |
| - if (!node) { |
18 |
| - node = {} |
19 |
| - current.set(key, node) |
20 |
| - } |
21 |
| - if (i < path.length - 1) { |
22 |
| - current = node.m = new Map() |
23 |
| - continue |
| 3 | +type Node<AtomType extends Atom<unknown> = Atom<unknown>> = { |
| 4 | + children?: Map<unknown, Node<AtomType>> |
| 5 | + atom?: AtomType |
| 6 | +} |
| 7 | + |
| 8 | +type AtomTree<Path extends unknown[], AtomType extends Atom<unknown>> = { |
| 9 | + (path: Path): AtomType |
| 10 | + remove(path?: Path, removeSubTree?: boolean): void |
| 11 | + getSubTree(path?: Path): Node<AtomType> |
| 12 | + getNodePath(path?: Path): Node<AtomType>[] |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Creates a hierarchical structure of Jotai atoms. |
| 17 | + * |
| 18 | + * @template AtomType - The type of atom returned by the initialization function. |
| 19 | + * @param initializePathAtom - A function that takes a path array and returns an Atom. |
| 20 | + * @returns A function for creating and managing hierarchical atoms (with additional methods). |
| 21 | + */ |
| 22 | +export function atomTree< |
| 23 | + Path extends unknown[], |
| 24 | + AtomType extends Atom<unknown>, |
| 25 | +>(initializePathAtom: (path: Path) => AtomType): AtomTree<Path, AtomType> { |
| 26 | + const root: Node<AtomType> = {} |
| 27 | + const defaultPath = new Array() as Path |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates or retrieves an atom at the specified path in the hierarchy. |
| 31 | + * |
| 32 | + * @param path - Array of keys representing the location in the hierarchy. |
| 33 | + * @returns The Jotai atom for the specified path. |
| 34 | + */ |
| 35 | + const createAtom: AtomTree<Path, AtomType> = (path) => { |
| 36 | + let node = root |
| 37 | + for (const key of path) { |
| 38 | + node.children ??= new Map() |
| 39 | + if (!node.children.has(key)) { |
| 40 | + node.children.set(key, {}) |
24 | 41 | }
|
25 |
| - return (node.v = initializeAtom(path)) |
| 42 | + node = node.children.get(key)! |
26 | 43 | }
|
27 |
| - throw new Error('Unreachable') |
| 44 | + node.atom ??= initializePathAtom(path) |
| 45 | + return node.atom |
28 | 46 | }
|
29 |
| - createAtom.remove = (path: [Param, ...Param[]]) => { |
30 |
| - if (path.length === 0) { |
31 |
| - throw new Error('Path must have at least one key') |
32 |
| - } |
33 |
| - const current = root |
34 |
| - const nodePath: Node[] = [] |
35 |
| - for (const segment of path) { |
36 |
| - const node = current.get(segment) |
37 |
| - if (!node) { |
38 |
| - break |
39 |
| - } |
40 |
| - nodePath.push(node) |
| 47 | + |
| 48 | + /** |
| 49 | + * Removes an atom (and optionally its subtree) at the specified path. |
| 50 | + * |
| 51 | + * @param path - Array of keys representing the location in the hierarchy. Defaults to [] (root). |
| 52 | + * @param removeSubTree - If true, removes all children of that path as well. Defaults to false. |
| 53 | + * @throws Error if the path does not exist. |
| 54 | + */ |
| 55 | + createAtom.remove = (path = defaultPath, removeSubTree = false) => { |
| 56 | + const nodePath = createAtom.getNodePath(path) |
| 57 | + const node = nodePath.at(-1)! |
| 58 | + delete node.atom |
| 59 | + if (removeSubTree) { |
| 60 | + delete node.children |
41 | 61 | }
|
42 |
| - delete nodePath[nodePath.length - 1]!.v |
43 |
| - // delete empty subtrees |
| 62 | + // delete empty subtrees from bottom to top |
44 | 63 | for (let i = nodePath.length - 1; i >= 0; i--) {
|
45 |
| - const current = nodePath[i]! |
46 |
| - if (current.m?.size === 0 && i > 0) { |
47 |
| - nodePath[i - 1]!.m!.delete(path[i]!) |
| 64 | + const node = nodePath[i]! |
| 65 | + if (!node.children?.size && i > 0) { |
| 66 | + const parentNode = nodePath[i - 1]! |
| 67 | + parentNode.children!.delete(path[i]!) |
| 68 | + if (!parentNode.children!.size) { |
| 69 | + delete parentNode.children |
| 70 | + } |
48 | 71 | } else {
|
49 | 72 | break
|
50 | 73 | }
|
51 | 74 | }
|
52 | 75 | }
|
| 76 | + |
| 77 | + /** |
| 78 | + * Retrieves the internal node (subtree) at a specified path. |
| 79 | + * |
| 80 | + * @param path - Array of keys representing the location in the hierarchy. Defaults to [] (root). |
| 81 | + * @returns A Node object with possible `children` and `atom`. |
| 82 | + * @throws Error if the path does not exist. |
| 83 | + */ |
| 84 | + createAtom.getSubTree = (path = defaultPath) => { |
| 85 | + let node = root |
| 86 | + for (const key of path) { |
| 87 | + node = node.children?.get(key)! |
| 88 | + if (!node) { |
| 89 | + throw new Error('Path does not exist') |
| 90 | + } |
| 91 | + } |
| 92 | + return node |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Retrieves the internal node (subtree) at a specified path. |
| 97 | + * |
| 98 | + * @param path - Array of keys representing the location in the hierarchy. Defaults to [] (root). |
| 99 | + * @returns An array of Node objects representing the path. |
| 100 | + * @throws Error if the path does not exist. |
| 101 | + */ |
| 102 | + createAtom.getNodePath = (path = defaultPath) => { |
| 103 | + const nodePath = [root] |
| 104 | + let node: Node<AtomType> | undefined = root |
| 105 | + for (const key of path) { |
| 106 | + node = node.children?.get(key) |
| 107 | + if (!node) { |
| 108 | + throw new Error('Path does not exist') |
| 109 | + } |
| 110 | + nodePath.push(node) |
| 111 | + } |
| 112 | + return nodePath |
| 113 | + } |
| 114 | + |
53 | 115 | return createAtom
|
54 | 116 | }
|
0 commit comments