Skip to content

Commit

Permalink
fix: fixed deprecation warnings, use subscribeWithSelector middleware
Browse files Browse the repository at this point in the history
BREAKING CHANGE: require minimum `zustand` version `3.6.0`
  • Loading branch information
BowlingX committed Feb 13, 2022
1 parent a8102f5 commit 55c6996
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"immer": ">=7",
"react": ">=16.8",
"react-dom": ">=16.8",
"zustand": "^3"
"zustand": ">=3.6.0"
},
"devDependencies": {
"@babel/core": "^7.7.4",
Expand Down Expand Up @@ -103,8 +103,8 @@
"tslint-config-prettier": "^1.18.0",
"tslint-immutable": "^6.0.1",
"typedoc": "^0.15.4",
"typescript": "^4.1.3",
"zustand": "^3.4.1"
"typescript": "^4.5.5",
"zustand": "^3.7.0"
},
"resolutions": {
"lodash": "4.17.19",
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('<Geschichte />', () => {
} = useQuery()
const { values: secondValues } = secondNamespaceUseQuery()

const { batchPushState } = useBatchQuery()
// tslint:disable-next-line:readonly-keyword
const { batchPushState } = useBatchQuery<{ someParameter: string }>()
return (
<>
<p>{someParameter}</p>
Expand Down
63 changes: 43 additions & 20 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ import {
useMemo,
useState
} from 'react'
import create, { State, StateCreator, UseStore } from 'zustand'
import create, {
GetState,
Mutate,
SetState,
State,
StateCreator,
StoreApi,
UseBoundStore
} from 'zustand'
// tslint:disable-next-line:no-submodule-imports
import { subscribeWithSelector } from 'zustand/middleware'

// tslint:disable-next-line:no-submodule-imports
import shallow from 'zustand/shallow'
import {
Expand All @@ -29,9 +40,9 @@ import {
enablePatches()

export const DEFAULT_NAMESPACE = 'default'
export const StoreContext = createContext<UseStore<StoreState<any>> | null>(
null
)
export const StoreContext = createContext<UseBoundStore<
StoreState<any>
> | null>(null)

export interface Parameter {
readonly name: string
Expand Down Expand Up @@ -68,20 +79,29 @@ export const useGeschichte = <T extends State>(
const middleware = (immerWithPatches<T>(
storeWithHistory
) as unknown) as StateCreator<StoreState<T>>

if (process.env.NODE_ENV === 'development' && typeof window !== 'undefined') {
// tslint:disable-next-line:no-submodule-imports
const { devtools } = require('zustand/middleware')
return create(devtools(middleware, 'geschichte')) as UseStore<
StoreState<State>
>
return create<
StoreState<T>,
SetState<StoreState<T>>,
GetState<StoreState<T>>,
StoreApi<StoreState<T>>
>(subscribeWithSelector(devtools(middleware, 'geschichte')))
}
return create(middleware)
return create<
StoreState<T>,
SetState<StoreState<T>>,
GetState<StoreState<T>>,
StoreApi<StoreState<T>>
>(subscribeWithSelector(middleware))
}

type InitialValuesProvider<T> = T | (() => T)

export const useStore = <T>() => {
return useContext(StoreContext) as UseStore<StoreState<T>>
return useContext(StoreContext) as UseBoundStore<StoreState<T>>
}

export const useBatchQuery = <T extends State>() => {
Expand Down Expand Up @@ -132,7 +152,13 @@ export const factoryParameters = <T>(
const memCreateInitialValues = memoizeOne(createInitialValues)

const useQuery = () => {
const useStore = useContext(StoreContext) as UseStore<StoreState<T>>
const useStore = useContext(StoreContext) as UseBoundStore<
StoreState<T>,
Mutate<
StoreApi<StoreState<T>>,
[['zustand/subscribeWithSelector', never]]
>
>

const {
register,
Expand Down Expand Up @@ -194,21 +220,18 @@ export const factoryParameters = <T>(
) {
setCurrentState({ values, initialValues })
}
const unsubscribe = useStore.subscribe<{
readonly values: T
readonly initialValues: T
}>(
state => {
if (state) {
setCurrentState(state)
}
},
const unsubscribe = useStore.subscribe(
state =>
state.namespaces[ns] && {
initialValues: state.namespaces[ns].initialValues,
values: state.namespaces[ns].values
},
shallow
state => {
if (state) {
setCurrentState(state)
}
},
{ equalityFn: shallow }
)

return () => {
Expand Down
33 changes: 25 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,13 @@
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.2.tgz#690a1475b84f2a884fd07cd797c00f5f31356ea8"
integrity sha512-ce5d3q03Ex0sy4R14722Rmt6MT07Ua+k4FwDfdcToYJcMKNtRVQvJ6JCAPdAmAnbRb6CsX6aYb9m96NGod9uTw==

"@types/ramda@^0.27.64":
version "0.27.64"
resolved "https://registry.yarnpkg.com/@types/ramda/-/ramda-0.27.64.tgz#7e23f2bf7b9729bcbd59223f100460ead6829008"
integrity sha512-EDf++ss/JoMiDpvT1MuA8oi88OwpvmqVE+o8Ojm5v/5bdJEPZ6eIQd/XYAeQ0imlwG6Tf0Npfq4Z9w3hAKBk9Q==
dependencies:
ts-toolbelt "^6.15.1"

"@types/reach__router@^1.2.3":
version "1.3.4"
resolved "https://registry.yarnpkg.com/@types/reach__router/-/reach__router-1.3.4.tgz#98ef393d06f59d296b5c021ba94b94e5fc463245"
Expand Down Expand Up @@ -12414,6 +12421,11 @@ ramda@^0.21.0:
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.21.0.tgz#a001abedb3ff61077d4ff1d577d44de77e8d0a35"
integrity sha1-oAGr7bP/YQd9T/HVd9RN536NCjU=

ramda@^0.28.0:
version "0.28.0"
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.28.0.tgz#acd785690100337e8b063cab3470019be427cc97"
integrity sha512-9QnLuG/kPVgWvMQ4aODhsBUFKOUmnbUnsSXACv+NCQZcHbeb+v8Lodp8OVxtRULN1/xOyYLLaL6npE6dMq5QTA==

[email protected]:
version "0.4.6"
resolved "https://registry.yarnpkg.com/randexp/-/randexp-0.4.6.tgz#e986ad5e5e31dae13ddd6f7b3019aa7c87f60ca3"
Expand Down Expand Up @@ -14985,6 +14997,11 @@ ts-pnp@^1.1.2:
resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.2.0.tgz#a500ad084b0798f1c3071af391e65912c86bca92"
integrity sha512-csd+vJOb/gkzvcCHgTGSChYpy5f1/XKNsmvBGO4JXS+z1v2HobugDz4s1IeFXM3wZB44uczs+eazB5Q/ccdhQw==

ts-toolbelt@^6.15.1:
version "6.15.5"
resolved "https://registry.yarnpkg.com/ts-toolbelt/-/ts-toolbelt-6.15.5.tgz#cb3b43ed725cb63644782c64fbcad7d8f28c0a83"
integrity sha512-FZIXf1ksVyLcfr7M317jbB67XFJhOO1YqdTcuGaq9q5jLUoTikukZ+98TPjKiP2jC5CgmYdWWYs0s2nLSU0/1A==

tslib@^1.10.0, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3:
version "1.11.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.11.1.tgz#eb15d128827fbee2841549e171f45ed338ac7e35"
Expand Down Expand Up @@ -15156,10 +15173,10 @@ [email protected]:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae"
integrity sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==

typescript@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==
typescript@^4.5.5:
version "4.5.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==

uglify-js@^2.6.1:
version "2.8.29"
Expand Down Expand Up @@ -16291,10 +16308,10 @@ yargs@~3.10.0:
decamelize "^1.0.0"
window-size "0.1.0"

zustand@^3.4.1:
version "3.4.1"
resolved "https://registry.yarnpkg.com/zustand/-/zustand-3.4.1.tgz#ff1bc8480facda3a4329510b8164aa5b821be133"
integrity sha512-Kb91vSjy5vwBQ/PQ1a5GE6naS3gCxCgpkujT9zqZSO85+gnvmzgqraMW3ao1I0jR4PwHBXtLTf26r9j7EXoUiQ==
zustand@^3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/zustand/-/zustand-3.7.0.tgz#a5c68fb06bdee9c63ad829de2432635be6d0ce69"
integrity sha512-USzVzLGrvZ8VK1/sEsOAmeqa8N7D3OBdZskVaL7DL89Q4QLTYD053iIlZ5KDidyZ+Od80Dttin/f8ZulOLFFDQ==

zwitch@^1.0.0:
version "1.0.5"
Expand Down

0 comments on commit 55c6996

Please sign in to comment.