Skip to content

Commit

Permalink
refactor(vanilla): simplify conditions (#2202)
Browse files Browse the repository at this point in the history
* refactor(vanilla): simplify conditions

* refactor for the recent changes
  • Loading branch information
dai-shi authored Oct 23, 2023
1 parent ac038ac commit acd62c6
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,20 @@ type AtomState<Value = AnyValue> = {
d: Dependencies
} & ({ e: AnyError } | { v: Value })

const isEqualAtomValue = <Value>(a: AtomState<Value>, b: AtomState<Value>) =>
'v' in a && 'v' in b && Object.is(a.v, b.v)
const isEqualAtomValue = <Value>(
a: AtomState<Value> | undefined,
b: AtomState<Value>
): a is AtomState<Value> => !!a && 'v' in a && 'v' in b && Object.is(a.v, b.v)

const isEqualAtomError = <Value>(a: AtomState<Value>, b: AtomState<Value>) =>
'e' in a && 'e' in b && Object.is(a.e, b.e)
const isEqualAtomError = <Value>(
a: AtomState<Value> | undefined,
b: AtomState<Value>
): a is AtomState<Value> => !!a && 'e' in a && 'e' in b && Object.is(a.e, b.e)

const hasPromiseAtomValue = <Value>(
a: AtomState<Value>
a: AtomState<Value> | undefined
): a is AtomState<Value> & { v: Value & Promise<unknown> } =>
'v' in a && a.v instanceof Promise
!!a && 'v' in a && a.v instanceof Promise

const isEqualPromiseAtomValue = <Value>(
a: AtomState<Promise<Value> & PromiseMeta<Value>>,
Expand Down Expand Up @@ -174,7 +178,7 @@ export const createStore = () => {
if (!pendingMap.has(atom)) {
pendingMap.set(atom, prevAtomState)
}
if (prevAtomState && hasPromiseAtomValue(prevAtomState)) {
if (hasPromiseAtomValue(prevAtomState)) {
const next =
'v' in atomState
? atomState.v instanceof Promise
Expand Down Expand Up @@ -226,15 +230,13 @@ export const createStore = () => {
updateDependencies(atom, nextAtomState, nextDependencies)
}
if (
prevAtomState &&
isEqualAtomValue(prevAtomState, nextAtomState) &&
prevAtomState.d === nextAtomState.d
) {
// bail out
return prevAtomState
}
if (
prevAtomState &&
hasPromiseAtomValue(prevAtomState) &&
hasPromiseAtomValue(nextAtomState) &&
isEqualPromiseAtomValue(prevAtomState, nextAtomState)
Expand Down Expand Up @@ -262,7 +264,6 @@ export const createStore = () => {
const updatePromiseDependencies = () => {
const prevAtomState = getAtomState(atom)
if (
!prevAtomState ||
!hasPromiseAtomValue(prevAtomState) ||
prevAtomState.v !== promise
) {
Expand Down Expand Up @@ -338,7 +339,6 @@ export const createStore = () => {
updateDependencies(atom, nextAtomState, nextDependencies)
}
if (
prevAtomState &&
isEqualAtomError(prevAtomState, nextAtomState) &&
prevAtomState.d === nextAtomState.d
) {
Expand Down Expand Up @@ -369,11 +369,9 @@ export const createStore = () => {
return true
}
const aState = readAtomState(a)
return (
aState === s ||
// We need to check values in case only dependencies are changed
(aState && isEqualAtomValue(aState, s))
)
// Check if the atom state is unchanged, or
// check the atom value in case only dependencies are changed
return aState === s || isEqualAtomValue(aState, s)
})
) {
return atomState
Expand Down Expand Up @@ -505,9 +503,7 @@ export const createStore = () => {
if (isChanged) {
const prevAtomState = getAtomState(dependent)
const nextAtomState = readAtomState(dependent, true)
isChanged =
!prevAtomState ||
!isEqualAtomValue(prevAtomState, nextAtomState)
isChanged = !isEqualAtomValue(prevAtomState, nextAtomState)
}
if (!isChanged) {
dependencyMap.forEach((s) => s.delete(dependent))
Expand Down Expand Up @@ -538,7 +534,7 @@ export const createStore = () => {
}
const prevAtomState = getAtomState(a)
const nextAtomState = setAtomValueOrPromise(a, args[0] as V)
if (!prevAtomState || !isEqualAtomValue(prevAtomState, nextAtomState)) {
if (!isEqualAtomValue(prevAtomState, nextAtomState)) {
recomputeDependents(a)
}
} else {
Expand Down Expand Up @@ -704,7 +700,6 @@ export const createStore = () => {
// TODO This seems pretty hacky. Hope to fix it.
// Maybe we could `mountDependencies` in `setAtomState`?
(
prevAtomState &&
!hasPromiseAtomValue(prevAtomState) &&
(isEqualAtomValue(prevAtomState, atomState) ||
isEqualAtomError(prevAtomState, atomState))
Expand Down

1 comment on commit acd62c6

@vercel
Copy link

@vercel vercel bot commented on acd62c6 Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.