Last active
May 15, 2024 18:05
-
-
Save probeiuscorp/0a3d22ddd1ec3e53b15d8e96916e30ff to your computer and use it in GitHub Desktop.
Files and changes I made to gather data for https://github.com/pmndrs/jotai/pull/2534
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
let n = 0 // Line 400 | |
const readAtomState = <Value>( | |
atom: Atom<Value>, | |
force?: boolean, | |
): AtomState<Value> => { | |
// See if we can skip recomputing this atom. | |
const atomState = getAtomState(atom) | |
const isInteresting = atom.debugLabel === 'filteredAtom' | |
const log = (...args) => isInteresting && console.log(...args) | |
if (isInteresting) { | |
// I used this to monkey patch force | |
if (n === 0) force = undefined | |
if (n === 1) force = true | |
if (n === 2) force = undefined | |
n++ | |
// End monkey patching | |
} | |
log('readAtomState', 'force:', force, 'atomState:', atomState) | |
if (isInteresting) { | |
console.log(new Error().stack) // see zz_doctoring-stack-trace.ts | |
} | |
if (!force && atomState) { | |
// If the atom is mounted, we can use the cache. | |
// because it should have been updated by dependencies. | |
if (mountedMap.has(atom)) { | |
log('<= returning from cache since mounted') | |
return atomState | |
} | |
// Otherwise, check if the dependencies have changed. | |
// If all dependencies haven't changed, we can use the cache. | |
if ( | |
Array.from(atomState.d).every(([a, s]) => { | |
// we shouldn't use isSelfAtom. https://github.com/pmndrs/jotai/pull/2371 | |
if (a === atom) { | |
return true | |
} | |
const aState = readAtomState(a) | |
// Check if the atom state is unchanged, or | |
// check the atom value in case only dependencies are changed | |
return aState === s || isEqualAtomValue(aState, s) | |
}) | |
) { | |
log('<= from cache since dependencies unchanged') | |
return atomState | |
} | |
} | |
log('<= not from cache: resolving dependencies') | |
// Compute a new state for this atom. | |
const nextDependencies: NextDependencies = new Map() |
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
import { atom, createStore } from './src/vanilla' | |
const dataAtom = atom<number[]>([100]) | |
const hasFilterAtom = atom(false) | |
const filteredAtom = atom((get) => { | |
const data = get(dataAtom) | |
const hasFilter = get(hasFilterAtom) | |
if (hasFilter) { | |
return [] | |
} else { | |
return data | |
} | |
}) | |
filteredAtom.debugLabel = 'filteredAtom' | |
const stageAtom = atom((get) => { | |
const hasFilter = get(hasFilterAtom) | |
if (hasFilter) { | |
const filtered = get(filteredAtom) | |
return filtered.length === 0 ? 'is-empty' : 'has-data' | |
} else { | |
return 'no-filter' | |
} | |
}) | |
const store = createStore() | |
store.sub(filteredAtom, () => undefined) | |
store.sub(stageAtom, () => undefined) | |
console.log('=> (should be "no-filter")', store.get(stageAtom)) | |
store.set(hasFilterAtom, true) | |
console.log('=> (should be "is-empty") ', store.get(stageAtom)) |
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
new Error().stack | |
?.split('\n') | |
.flatMap((line) => { | |
if (line.includes('global-require-patch') || line === 'Error' || line.includes('Module')) { | |
return [] | |
} | |
if (line.includes('at ')) { | |
const filename = line.slice( | |
Math.max(line.lastIndexOf('/'), line.indexOf('(')) + 1, | |
line.indexOf(')'), | |
) | |
return ( | |
'-' + | |
line.slice(line.indexOf('at') + 2, line.indexOf('(') + 1) + | |
filename + | |
')' | |
) | |
} | |
return line | |
}) | |
.join('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment