|
| 1 | +# Objectives |
| 2 | + |
| 3 | +1. Derived atoms are not copied if they don’t depend on scoped atoms. |
| 4 | +2. When a derived atom starts depending on a scoped atom, a new atom state is created as the scoped atom state. |
| 5 | +3. When a derived atom stops depending on a scoped atom, it must be removed from the scope state and restored to the original atom state. |
| 6 | + a. When changing between scoped and unscoped, all subscibers must be notified. |
| 7 | + |
| 8 | + Fixes: |
| 9 | + |
| 10 | + - [Scope caused atomWithObservable to be out of sync](https://github.com/jotaijs/jotai-scope/issues/36) |
| 11 | + - [Computed atoms get needlessly triggered again](https://github.com/jotaijs/jotai-scope/issues/25) |
| 12 | + |
| 13 | +# Requirements |
| 14 | + |
| 15 | +1. Some way to track dependencies of computed atoms not in the scope without copying them. |
| 16 | +2. Some way to get whether the atom has been mounted. |
| 17 | + |
| 18 | +# Problem Statement |
| 19 | + |
| 20 | +A computed atom may or may not consume scoped atoms. This may also change as state changes. |
| 21 | + |
| 22 | +```tsx |
| 23 | +const providerAtom = atom('unscoped') |
| 24 | +const scopedProviderAtom = atom('scoped') |
| 25 | +const shouldConsumeScopedAtom = atom(false) |
| 26 | +const consumerAtom = atom((get) => { |
| 27 | + if (get(shouldConsumeScopedAtom)) { |
| 28 | + return get(scopedProviderAtom) |
| 29 | + } |
| 30 | + return get(providerAtom) |
| 31 | +}) |
| 32 | + |
| 33 | +function Component() { |
| 34 | + const value = useAtomValue(consumerAtom) |
| 35 | + return value |
| 36 | +} |
| 37 | + |
| 38 | +function App() { |
| 39 | + const setShouldConsumeScopedAtom = useSetAtom(shouldConsumeScopedAtom) |
| 40 | + useEffect(() => { |
| 41 | + const timeoutId = setTimeout(setShouldConsumeScopedAtom, 1000, true) |
| 42 | + return () => clearTimeout(timeoutId) |
| 43 | + }, []) |
| 44 | + |
| 45 | + return ( |
| 46 | + <ScopeProvider atoms={[scopedProviderAtom]}> |
| 47 | + <Component /> |
| 48 | + </ScopeProvider> |
| 49 | + ) |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +To properly handle `consumerAtom`, we need to track the dependencies of the computed atom. |
| 54 | + |
| 55 | +# Proxy State |
| 56 | + |
| 57 | +Atom state has the following shape; |
| 58 | + |
| 59 | +```ts |
| 60 | +type AtomState = { |
| 61 | + d: Map<AnyAtom, number>; // map of atom consumers to their epoch number |
| 62 | + p: Set<AnyAtom>; // set of pending atom consumers |
| 63 | + n: number; // epoch number |
| 64 | + m?: { |
| 65 | + l: Set<() => void>; // set of listeners |
| 66 | + d: Set<AnyAtom>; // set of mounted atom consumers |
| 67 | + t: Set<AnyAtom>; // set of mounted atom providers |
| 68 | + u?: (setSelf: () => any) => (void | () => void); // unmount function |
| 69 | + }; |
| 70 | + v?: any; // value |
| 71 | + e?: any; // error |
| 72 | +}; |
| 73 | +``` |
| 74 | + |
| 75 | +All computed atoms (`atom.read !== defaultRead`) will have their base atomState converted to a proxy state. The proxy state will track dependencies and notify when they change. |
| 76 | + |
| 77 | +0. Update all computed atoms with a proxy state in the parent store. |
| 78 | +1. If a computer atom does not depend on any scoped atoms, remove it from the unscopedComputed set |
| 79 | +2. If a computed atom starts depending on a scoped atom, add it to the scopedComputed set. |
| 80 | + a. If the scoped state does not already exist, create a new scoped atom state. |
| 81 | +3. If a computed atom stops depending on a scoped atom, remove it from the scopedComputed set. |
0 commit comments