-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(shallow): Extract shallow vanilla and react (#2097)
* Update readmes * Splitting shallow in two modules * Update tests * Minor changes * Minor changes * Rename shadow.tests.tsx to shallow.test.tsx * Add new entrypoint for shallow/react * Update structure * Update shallow to export from vanilla and react * Add vanilla/shallow and react/shallow entrypoints * Update tests * Update readmes * Update src/shallow.ts Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com> * Minor changes * Update readmes * Update readmes * Update tests * Minor changes --------- Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
- Loading branch information
1 parent
2be79c9
commit e414f7c
Showing
9 changed files
with
332 additions
and
166 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useRef } from 'react' | ||
import { shallow } from '../vanilla/shallow.ts' | ||
|
||
export function useShallow<S, U>(selector: (state: S) => U): (state: S) => U { | ||
const prev = useRef<U>() | ||
|
||
return (state) => { | ||
const next = selector(state) | ||
return shallow(prev.current, next) | ||
? (prev.current as U) | ||
: (prev.current = next) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export function shallow<T>(objA: T, objB: T) { | ||
if (Object.is(objA, objB)) { | ||
return true | ||
} | ||
if ( | ||
typeof objA !== 'object' || | ||
objA === null || | ||
typeof objB !== 'object' || | ||
objB === null | ||
) { | ||
return false | ||
} | ||
|
||
if (objA instanceof Map && objB instanceof Map) { | ||
if (objA.size !== objB.size) return false | ||
|
||
for (const [key, value] of objA) { | ||
if (!Object.is(value, objB.get(key))) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
if (objA instanceof Set && objB instanceof Set) { | ||
if (objA.size !== objB.size) return false | ||
|
||
for (const value of objA) { | ||
if (!objB.has(value)) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
const keysA = Object.keys(objA) | ||
if (keysA.length !== Object.keys(objB).length) { | ||
return false | ||
} | ||
for (let i = 0; i < keysA.length; i++) { | ||
if ( | ||
!Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) || | ||
!Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T]) | ||
) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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
Oops, something went wrong.
e414f7c
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:
zustand-demo – ./
zustand-demo-git-main-pmndrs.vercel.app
zustand-demo.pmnd.rs
zustand-demo-pmndrs.vercel.app
zustand-demo.vercel.app