Skip to content

Commit

Permalink
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
Browse files Browse the repository at this point in the history
…options
  • Loading branch information
dai-shi committed Apr 7, 2022
2 parents b764020 + 4d8003b commit 2cb29bb
Show file tree
Hide file tree
Showing 8 changed files with 686 additions and 630 deletions.
13 changes: 13 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# These are supported funding model platforms

github: [dai-shi] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: ['https://daishi.gumroad.com/l/uaxms'] # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
4 changes: 4 additions & 0 deletions .github/workflows/test-multiple-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- run: yarn install --frozen-lockfile --check-files
- run: yarn build
- name: Use React 17 for production test
if: ${{ matrix.env == 'production' }}
run: |
yarn add -D [email protected] [email protected] @testing-library/[email protected]
- name: Patch for DEV-ONLY
if: ${{ matrix.env == 'development' }}
run: |
Expand Down
15 changes: 6 additions & 9 deletions .github/workflows/test-multiple-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ jobs:
react:
- 16.8.0
- 17.0.0
- 18.0.0-rc.1-next-629036a9c-20220224
- 0.0.0-experimental-629036a9c-20220224
testing: [default, alpha]
exclude:
- { react: 16.8.0, testing: alpha }
- { react: 17.0.0, testing: alpha }
- 18.0.0
- 18.1.0-next-af730436c-20220405
- 0.0.0-experimental-af730436c-20220405
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand All @@ -44,9 +41,9 @@ jobs:
cache: yarn
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- run: yarn install --frozen-lockfile --check-files
- name: Install alpha testing-library
if: ${{ matrix.testing == 'alpha' }}
run: yarn add -D @testing-library/react@alpha
- name: Install legacy testing-library
if: ${{ startsWith(matrix.react, '16.') || startsWith(matrix.react, '17.') }}
run: yarn add -D @testing-library/react@12.1.4
- name: Patch for React 16
if: ${{ startsWith(matrix.react, '16.') }}
run: |
Expand Down
49 changes: 23 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zustand",
"private": true,
"version": "3.7.1",
"version": "3.7.2",
"description": "🐻 Bear necessities for state management in React",
"main": "./index.js",
"types": "./index.d.ts",
Expand Down Expand Up @@ -131,7 +131,7 @@
]
},
"devDependencies": {
"@babel/core": "^7.17.5",
"@babel/core": "^7.17.9",
"@babel/plugin-external-helpers": "^7.16.7",
"@babel/plugin-transform-react-jsx": "^7.17.3",
"@babel/plugin-transform-runtime": "^7.17.0",
Expand All @@ -141,40 +141,37 @@
"@rollup/plugin-node-resolve": "^13.1.3",
"@rollup/plugin-replace": "^4.0.0",
"@rollup/plugin-typescript": "^8.3.1",
"@swc/core": "^1.2.146",
"@swc/core": "^1.2.164",
"@swc/jest": "^0.2.20",
"@testing-library/react": "^12.1.3",
"@testing-library/react": "^13.0.0",
"@types/jest": "^27.4.1",
"@types/react": "^17.0.39",
"@types/react-dom": "^17.0.11",
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"concurrently": "^7.0.0",
"esbuild": "^0.14.23",
"eslint": "^8.10.0",
"eslint-config-prettier": "^8.4.0",
"@types/react": "^17.0.43",
"@types/react-dom": "^17.0.14",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"concurrently": "^7.1.0",
"esbuild": "^0.14.34",
"eslint": "^8.12.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^26.1.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.1.3",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.29.2",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.4.0",
"husky": "^7.0.4",
"immer": "^9.0.12",
"jest": "^27.5.1",
"json": "^11.0.0",
"lint-staged": "^12.3.4",
"prettier": "^2.5.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"rollup": "^2.68.0",
"rollup-plugin-esbuild": "^4.8.2",
"lint-staged": "^12.3.7",
"prettier": "^2.6.2",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"rollup": "^2.70.1",
"rollup-plugin-esbuild": "^4.9.1",
"rollup-plugin-terser": "^7.0.2",
"shx": "^0.3.4",
"typescript": "^4.5.5"
},
"resolutions": {
"date-fns": "2.27.0"
"typescript": "^4.6.3"
},
"peerDependencies": {
"react": ">=16.8"
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ const unsub5 = useStore.subscribe(state => state.paw, console.log, { fireImmedia
<summary>How to type store with `subscribeWithSelector` in TypeScript</summary>
```ts
import create, { GetState, SetState } from 'zustand'
import { StoreApiWithSubscribeWithSelector, subscribeWithSelector } from 'zustand/middleware'
import create, { Mutate, GetState, SetState, StoreApi } from 'zustand'
import { subscribeWithSelector } from 'zustand/middleware'

type BearState = {
paw: boolean
Expand All @@ -229,7 +229,7 @@ const useStore = create<
BearState,
SetState<BearState>,
GetState<BearState>,
StoreApiWithSubscribeWithSelector<BearState>
Mutate<StoreApi<BearState>, [["zustand/subscribeWithSelector", never]]>
>(subscribeWithSelector(() => ({ paw: true, snout: true, fur: true })))
```
Expand Down
5 changes: 4 additions & 1 deletion src/middleware/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ export const persist =
}
})
.then((migratedState) => {
stateFromStorage = options.merge(migratedState as S, configResult)
stateFromStorage = options.merge(
migratedState as S,
get() ?? configResult
)

set(stateFromStorage as S, true)
return setItem()
Expand Down
53 changes: 53 additions & 0 deletions tests/persistAsync.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect } from 'react'
import { act, render, waitFor } from '@testing-library/react'
import create from 'zustand'
import { persist } from 'zustand/middleware'
Expand Down Expand Up @@ -217,6 +218,58 @@ describe('persist middleware with async configuration', () => {
expect(onRehydrateStorageSpy).toBeCalledWith({ count: 99 }, undefined)
})

it('can merge partial persisted state', async () => {
const storage = {
getItem: async () =>
JSON.stringify({
state: { count: 42 },
}),
setItem: () => {},
removeItem: () => {},
}

const useStore = create(
persist(
(set) => ({
count: 0,
name: 'unknown',
setName: (name: string) => {
set({ name })
},
}),
{
name: 'test-storage',
getStorage: () => storage,
}
)
)

function Component() {
const { count, setName, name } = useStore()
useEffect(() => {
setName('test')
}, [setName])
return (
<div>
<div>count: {count}</div>
<div>name: {name}</div>
</div>
)
}

const { findByText } = render(<Component />)

await findByText('count: 42')
await findByText('name: test')

expect(useStore.getState()).toEqual(
expect.objectContaining({
count: 42,
name: 'test',
})
)
})

it('can correclty handle a missing migrate function', async () => {
console.error = jest.fn()
const onRehydrateStorageSpy = jest.fn()
Expand Down
Loading

0 comments on commit 2cb29bb

Please sign in to comment.