Skip to content

Commit

Permalink
add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jan 11, 2025
1 parent b3e3f65 commit 0fd8aa7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ type Mounted = {
/**
* Mutable atom state,
* tracked for both mounted and unmounted atoms in a store.
*
* This should be garbage collectable.
* We can mutate it during atom read. (except for fields with TODO)
*/
type AtomState<Value = AnyValue> = {
/**
Expand All @@ -95,20 +98,26 @@ type AtomState<Value = AnyValue> = {
* Set of atoms with pending promise that depend on the atom.
*
* This may cause memory leaks, but it's for the capability to continue promises
* TODO(daishi): revisit how to handle this
*/
readonly p: Set<AnyAtom>
/** The epoch number of the atom. */
n: EpochNumber
/** Object to store mounted state of the atom. */
/**
* Object to store mounted state of the atom.
* TODO(daishi): move this out of AtomState
*/
m?: Mounted // only available if the atom is mounted
/**
* Listener to notify when the atom value is updated.
* This is an experimental API and will be changed in the next minor.
* TODO(daishi): move this store hooks
*/
u?: () => void
/**
* Listener to notify when the atom is mounted or unmounted.
* This is an experimental API and will be changed in the next minor.
* TODO(daishi): move this store hooks
*/
h?: () => void
/** Atom value */
Expand Down Expand Up @@ -232,6 +241,8 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
return atomState
}

// These are store state.
// As they are not garbage collectable, they shouldn't be mutated during atom read.
const invalidatedAtoms = new WeakMap<AnyAtom, EpochNumber>()
const changedAtoms = new Map<AnyAtom, AtomState>()
const unmountCallbacks = new Set<() => void>()
Expand Down
2 changes: 1 addition & 1 deletion tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ it('mounted atom should be recomputed eagerly', () => {
expect(result).toEqual(['bRead', 'aCallback', 'bCallback'])
})

it('reading atom in write should notify subscribers', () => {
it('should notify subscription even with reading atom in write', () => {
const a = atom(1)
const b = atom((get) => get(a) * 2)
const c = atom((get) => get(b) + 1)
Expand Down

0 comments on commit 0fd8aa7

Please sign in to comment.