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

Upgrade Zustand #57

Merged
merged 6 commits into from
Jul 8, 2023
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
9 changes: 9 additions & 0 deletions .changeset/funny-trains-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@udecode/zustood": major
---

Upgraded `zustand` to version 4, bringing in several new features and improvements.
- Deprecated types in `zustand` v4+ were addressed by including them in the package itself.
- Upgraded `immer` to the latest version.
- The upgrade to `zustand` v4 and `immer` introduces enhancements, bug fixes, and performance optimizations to your application.
- Please make sure to review the official documentation of `zustand` v4 and `immer` for any additional changes and updates.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ jobs:
- 'test'
steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup node
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 12
node-version: 16

- name: Run lint
- name: Run ${{ matrix.command }}
run: yarn && yarn build && yarn ${{ matrix.command }}
env:
CI: true
8 changes: 4 additions & 4 deletions .github/workflows/comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
startsWith(github.event.comment.body, '/rebase')
steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

Expand All @@ -33,7 +33,7 @@ jobs:
startsWith(github.event.comment.body, '/release:next')
steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

Expand All @@ -43,9 +43,9 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Setup node
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 12.x
node-version: 16
registry-url: https://registry.npmjs.org

- name: Install dependencies
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ jobs:
- latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup node
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 12.x
node-version: 16
registry-url: https://registry.npmjs.org

- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@
"ts-jest": "^26.5.2",
"typedoc": "^0.22.5",
"typescript": "^4.4.3",
"zustand": "^3.6.4"
"zustand": "^4.3.9"
}
}
8 changes: 4 additions & 4 deletions packages/zustood/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@udecode/zustood",
"version": "1.1.3",
"version": "1.1.4",
"description": "A modular store factory using zustand",
"keywords": [
"zustand"
Expand Down Expand Up @@ -30,11 +30,11 @@
"test": "jest"
},
"dependencies": {
"immer": "^9.0.6",
"react-tracked": "^1.7.9"
"immer": "^10.0.2",
"react-tracked": "^1.7.11"
},
"peerDependencies": {
"zustand": ">=3.5.10"
"zustand": ">=4.3.9"
},
"publishConfig": {
"access": "public"
Expand Down
7 changes: 5 additions & 2 deletions packages/zustood/src/createStore.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { setAutoFreeze, enableMapSet } from 'immer';
import { createTrackedSelector } from 'react-tracked';
import create, { State, StateCreator } from 'zustand';
import { create } from 'zustand';
import type { StateCreator } from 'zustand';
import {
devtools as devtoolsMiddleware,
persist as persistMiddleware,
} from 'zustand/middleware';
import createVanillaStore from 'zustand/vanilla';
import { createStore as createVanillaStore } from 'zustand/vanilla';
import {
ImmerStoreApi,
MergeState,
Expand All @@ -14,6 +15,7 @@ import {
StateGetters,
StoreApi,
UseImmerStore,
State,
} from './types';
import { generateStateActions } from './utils/generateStateActions';
import { storeFactory } from './utils/storeFactory';
Expand Down Expand Up @@ -63,6 +65,7 @@ export const createStore =

middlewares.push(createVanillaStore);

// @ts-ignore
const createStore = (createState: StateCreator<T, SetImmerState<T>>) =>
pipe(createState as any, ...middlewares) as ImmerStoreApi<T>;

Expand Down
12 changes: 8 additions & 4 deletions packages/zustood/src/middlewares/immer.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import produce from 'immer';
import { GetState, State } from 'zustand';
import { SetImmerState, StateCreatorWithDevtools } from '../types';
import { produce } from 'immer';
import { StoreApi } from 'zustand';
import { SetImmerState, StateCreatorWithDevtools, State } from '../types';

export const immerMiddleware =
<T extends State>(
config: StateCreatorWithDevtools<T, SetImmerState<T>, GetState<T>>
config: StateCreatorWithDevtools<
T,
SetImmerState<T>,
StoreApi<T>['getState']
>
): StateCreatorWithDevtools<T> =>
(set, get, api) => {
const setState: SetImmerState<T> = (fn, actionName) =>
Expand Down
9 changes: 6 additions & 3 deletions packages/zustood/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Draft } from 'immer';
import { State, StoreApi as RawStoreApi, UseStore } from 'zustand';
import { EqualityChecker, GetState, StateSelector } from 'zustand/vanilla';
import { StoreApi as RawStoreApi, UseBoundStore } from 'zustand';
import { GetState, StateSelector } from 'zustand/vanilla';
import { NamedSet } from 'zustand/middleware';

export type StoreApiGet<
Expand Down Expand Up @@ -60,6 +60,9 @@ export type StoreApi<
// >;
};

export type State = unknown;
export type EqualityChecker<T> = (state: T, newState: T) => boolean;

export type MergeState<T extends State> = (
state: Partial<T>,
actionName?: string
Expand Down Expand Up @@ -115,7 +118,7 @@ export interface ImmerStoreApi<T extends State>
}

export interface UseImmerStore<T extends State>
extends Omit<UseStore<T>, 'setState'> {
extends Omit<UseBoundStore<RawStoreApi<T>>, 'setState'> {
(): T;

<U>(selector: StateSelector<T, U>, equalityFn?: EqualityChecker<U>): U;
Expand Down
4 changes: 2 additions & 2 deletions packages/zustood/src/types/CreateStoreOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { State } from 'zustand';
import { PersistOptions } from './PersistOptions';
import { DevtoolsOptions } from './DevtoolsOptions';
import { ImmerOptions } from './ImmerOptions';
import { State } from '../types';
import { DevtoolsOptions } from 'zustand/middleware';

export interface CreateStoreOptions<T extends State> {
/**
Expand Down
18 changes: 0 additions & 18 deletions packages/zustood/src/types/DevtoolsOptions.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/zustood/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
*/

export * from './CreateStoreOptions';
export * from './DevtoolsOptions';
export * from './ImmerOptions';
export * from './PersistOptions';
3 changes: 1 addition & 2 deletions packages/zustood/src/utils/extendActions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { State } from 'zustand';
import { ActionBuilder, StateActions, StoreApi } from '../types';
import { ActionBuilder, State, StateActions, StoreApi } from '../types';

export const extendActions = <
AB extends ActionBuilder<TName, T, StateActions<T> & TActions, TSelectors>,
Expand Down
2 changes: 1 addition & 1 deletion packages/zustood/src/utils/extendSelectors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { State } from 'zustand';
import {
SelectorBuilder,
State,
StateActions,
StoreApi,
StoreApiGet,
Expand Down
5 changes: 2 additions & 3 deletions packages/zustood/src/utils/generateStateActions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { State } from 'zustand';
import { SetRecord, UseImmerStore } from '../types';
import { SetRecord, State, UseImmerStore } from '../types';

export const generateStateActions = <T extends State>(
store: UseImmerStore<T>,
storeName: string
) => {
const actions: SetRecord<T> = {} as any;

Object.keys(store.getState()).forEach((key) => {
Object.keys((store as any).getState()).forEach((key) => {
actions[key] = (value: keyof T) => {
const prevValue = store.getState()[key];
if (prevValue === value) return;
Expand Down
5 changes: 2 additions & 3 deletions packages/zustood/src/utils/generateStateGetSelectors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { State } from 'zustand';
import { GetRecord, UseImmerStore } from '../types';
import { GetRecord, State, UseImmerStore } from '../types';

export const generateStateGetSelectors = <T extends State>(
store: UseImmerStore<T>
) => {
const selectors: GetRecord<T> = {} as any;

Object.keys(store.getState()).forEach((key) => {
Object.keys((store as any).getState()).forEach((key) => {
// selectors[`get${capitalize(key)}`] = () => store.getState()[key as keyof T];
selectors[key] = () => store.getState()[key as keyof T];
});
Expand Down
5 changes: 2 additions & 3 deletions packages/zustood/src/utils/generateStateHookSelectors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { EqualityChecker, State } from 'zustand';
import { GetRecord, UseImmerStore } from '../types';
import { GetRecord, UseImmerStore, EqualityChecker, State } from '../types';

export const generateStateHookSelectors = <T extends State>(
store: UseImmerStore<T>
) => {
const selectors: GetRecord<T> = {} as any;

Object.keys(store.getState()).forEach((key) => {
Object.keys((store as any).getState()).forEach((key) => {
// selectors[`use${capitalize(key)}`] = () =>
selectors[key] = (equalityFn?: EqualityChecker<T[keyof T]>) => {
return store((state: T) => state[key as keyof T], equalityFn);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { State } from 'zustand';
import { GetRecord, UseImmerStore } from '../types';
import { GetRecord, UseImmerStore, State } from '../types';

export const generateStateTrackedHooksSelectors = <T extends State>(
store: UseImmerStore<T>,
trackedStore: () => T
) => {
const selectors: GetRecord<T> = {} as any;

Object.keys(store.getState()).forEach((key) => {
Object.keys((store as any).getState()).forEach((key) => {
selectors[key] = () => {
return trackedStore()[key as keyof T];
};
Expand Down
1 change: 1 addition & 0 deletions packages/zustood/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './extendSelectors';
export * from './generateStateActions';
export * from './generateStateGetSelectors';
export * from './generateStateHookSelectors';
export * from './generateStateTrackedHooksSelectors';
export * from './mapValuesKey';
export * from './pipe';
export * from './storeFactory';
2 changes: 1 addition & 1 deletion packages/zustood/src/utils/storeFactory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { State } from 'zustand';
import {
ActionBuilder,
SelectorBuilder,
StateActions,
StoreApi,
State,
} from '../types';
import { extendActions } from './extendActions';
import { extendSelectors } from './extendSelectors';
Expand Down
Loading