Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert useShallow refactor in #2701 #2703

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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