Skip to content

Commit

Permalink
revert useShallow refactor in #2701 (#2703)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Sep 15, 2024
1 parent 88ee69f commit c08adc2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 32 deletions.
12 changes: 3 additions & 9 deletions src/react.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// import { useDebugValue, useSyncExternalStore } from 'react'
// That doesn't work in ESM, because React libs are CJS only.
// See: https://github.com/pmndrs/valtio/issues/452
// The following is a workaround until ESM is supported.
import ReactExports from 'react'
import React from 'react'
import { createStore } from './vanilla.ts'
import type {
Mutate,
Expand All @@ -11,8 +7,6 @@ import type {
StoreMutatorIdentifier,
} from './vanilla.ts'

const { useDebugValue, useSyncExternalStore } = ReactExports

type ExtractState<S> = S extends { getState: () => infer T } ? T : never

type ReadonlyStoreApi<T> = Pick<
Expand All @@ -34,12 +28,12 @@ export function useStore<TState, StateSlice>(
api: ReadonlyStoreApi<TState>,
selector: (state: TState) => StateSlice = identity as any,
) {
const slice = useSyncExternalStore(
const slice = React.useSyncExternalStore(
api.subscribe,
() => selector(api.getState()),
() => selector(api.getInitialState()),
)
useDebugValue(slice)
React.useDebugValue(slice)
return slice
}

Expand Down
20 changes: 5 additions & 15 deletions src/react/shallow.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
// import { useRef } from 'react'
// That doesnt work in ESM, because React libs are CJS only.
// The following is a workaround until ESM is supported.
import ReactExports from 'react'
import React from 'react'
import { shallow } from '../vanilla/shallow.ts'

const { useRef } = ReactExports

const sliceCache = new WeakMap<object, unknown>()

export function useShallow<S, U>(selector: (state: S) => U): (state: S) => U {
const key = useRef({}).current
const prev = React.useRef<U>()
return (state) => {
const prev = sliceCache.get(key) as U | undefined
const next = selector(state)
if (shallow(prev, next)) {
return prev as U
}
sliceCache.set(key, next)
return next
return shallow(prev.current, next)
? (prev.current as U)
: (prev.current = next)
}
}
10 changes: 2 additions & 8 deletions src/traditional.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
// import { useDebugValue } from 'react'
// import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'
// Those don't work in ESM, because React libs are CJS only.
// See: https://github.com/pmndrs/valtio/issues/452
// The following is a workaround until ESM is supported.
import ReactExports from 'react'
import React from 'react'
// eslint-disable-next-line import/extensions
import useSyncExternalStoreExports from 'use-sync-external-store/shim/with-selector'
import { createStore } from './vanilla.ts'
Expand All @@ -14,7 +9,6 @@ import type {
StoreMutatorIdentifier,
} from './vanilla.ts'

const { useDebugValue } = ReactExports
const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports

type ExtractState<S> = S extends { getState: () => infer T } ? T : never
Expand Down Expand Up @@ -48,7 +42,7 @@ export function useStoreWithEqualityFn<TState, StateSlice>(
selector,
equalityFn,
)
useDebugValue(slice)
React.useDebugValue(slice)
return slice
}

Expand Down

0 comments on commit c08adc2

Please sign in to comment.