-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Snapshot-based devtools (1st iteration) (#425)
* snapshot wip * useAtomsSnapshot wip * Add type * Add first test * Return map from useAtomsSnapshot * wip snapshots * Change registeredAtoms to state Add tests * Add key * Update src/core/contexts.ts * Fixes after review * Merges * Fix test * add FIXME comments Co-authored-by: Daishi Kato <[email protected]> Co-authored-by: daishi <[email protected]>
- Loading branch information
1 parent
a683f39
commit 59359b2
Showing
8 changed files
with
178 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { useAtomDevtools } from './devtools/useAtomDevtools' | ||
export { useAtomsSnapshot } from './devtools/useAtomsSnapshot' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useCallback, useContext, useMemo } from 'react' | ||
import { SECRET_INTERNAL_getStoreContext as getStoreContext } from 'jotai' | ||
import { AtomState, State, subscribeAtom } from '../core/vanilla' | ||
import { RegisteredAtomsContext } from '../core/contexts' | ||
import { useMutableSource } from '../core/useMutableSource' | ||
import { AnyAtom } from '../core/types' | ||
|
||
type AtomsSnapshot = Map<AnyAtom, unknown> | ||
|
||
export function useAtomsSnapshot(): AtomsSnapshot { | ||
const StoreContext = getStoreContext() | ||
const [mutableSource] = useContext(StoreContext) | ||
const atoms = useContext(RegisteredAtomsContext) | ||
|
||
const subscribe = useCallback( | ||
(state: State, callback: () => void) => { | ||
// FIXME we don't need to resubscribe, just need to subscribe for new one | ||
const unsubs = atoms.map((atom) => subscribeAtom(state, atom, callback)) | ||
return () => { | ||
unsubs.forEach((unsub) => unsub()) | ||
} | ||
}, | ||
[atoms] | ||
) | ||
const state: State = useMutableSource(mutableSource, getState, subscribe) | ||
|
||
return useMemo(() => { | ||
const atomToAtomValueTuples = atoms | ||
.filter((atom) => !!state.m.get(atom)) | ||
.map<[AnyAtom, unknown]>((atom) => { | ||
const atomState = state.a.get(atom) ?? ({} as AtomState) | ||
return [atom, atomState.e || atomState.p || atomState.w || atomState.v] | ||
}) | ||
return new Map(atomToAtomValueTuples) | ||
}, [atoms, state]) | ||
} | ||
|
||
const getState = (state: State) => ({ ...state }) // shallow copy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, { useState } from 'react' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import { Provider, atom, useAtom } from '../../src/index' | ||
import { useAtomsSnapshot } from '../../src/devtools' | ||
|
||
beforeEach(() => { | ||
process.env.NODE_ENV = 'development' | ||
}) | ||
afterEach(() => { | ||
process.env.NODE_ENV = 'test' | ||
}) | ||
|
||
it('should register newly added atoms', async () => { | ||
const countAtom = atom(1) | ||
const petAtom = atom('cat') | ||
|
||
const DisplayCount: React.FC = () => { | ||
const [clicked, setClicked] = useState(false) | ||
const [count] = useAtom(countAtom) | ||
|
||
return ( | ||
<> | ||
<p>count: {count}</p> | ||
<button onClick={() => setClicked(true)}>click</button> | ||
{clicked && <DisplayPet />} | ||
</> | ||
) | ||
} | ||
|
||
const DisplayPet: React.FC = () => { | ||
const [pet] = useAtom(petAtom) | ||
return <p>pet: {pet}</p> | ||
} | ||
|
||
const RegisteredAtomsCount: React.FC = () => { | ||
const atoms = useAtomsSnapshot() | ||
|
||
return <p>atom count: {atoms.size}</p> | ||
} | ||
|
||
const { findByText, getByText } = render( | ||
<Provider> | ||
<DisplayCount /> | ||
<RegisteredAtomsCount /> | ||
</Provider> | ||
) | ||
|
||
await findByText('atom count: 1') | ||
fireEvent.click(getByText('click')) | ||
await findByText('atom count: 2') | ||
}) | ||
|
||
it('should let you access atoms and their state', async () => { | ||
const countAtom = atom(1) | ||
countAtom.debugLabel = 'countAtom' | ||
const petAtom = atom('cat') | ||
petAtom.debugLabel = 'petAtom' | ||
|
||
const Displayer: React.FC = () => { | ||
useAtom(countAtom) | ||
useAtom(petAtom) | ||
return null | ||
} | ||
|
||
const SimpleDevtools: React.FC = () => { | ||
const atoms = useAtomsSnapshot() | ||
|
||
return ( | ||
<div> | ||
{Array.from(atoms).map(([atom, atomValue]) => ( | ||
<p key={atom.debugLabel}>{`${atom.debugLabel}: ${atomValue}`}</p> | ||
))} | ||
</div> | ||
) | ||
} | ||
|
||
const { findByText } = render( | ||
<Provider> | ||
<Displayer /> | ||
<SimpleDevtools /> | ||
</Provider> | ||
) | ||
|
||
await findByText('countAtom: 1') | ||
await findByText('petAtom: cat') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59359b2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: