define-zustand 3.2.1 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/define.d.ts +10 -0
- package/dist/define.js +26 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/dist/store-middleware.d.ts +3 -0
- package/dist/store-middleware.js +45 -0
- package/dist/test.stories.d.ts +5 -0
- package/dist/test.stories.js +42 -0
- package/dist/types.d.ts +7 -7
- package/package.json +1 -1
- package/dist/utils.d.ts +0 -11
- package/dist/utils.js +0 -83
package/README.md
CHANGED
package/dist/define.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FunctionComponent, ReactNode } from 'react';
|
|
2
|
+
import { gettersStateType, modelStateType, optionsType } from './types';
|
|
3
|
+
export declare function defineStateFactory<S extends Record<string, any>, G extends gettersStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): () => optionsType<S, G, Actions>;
|
|
4
|
+
export declare function defineStore<S extends Record<string, any>, G extends gettersStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): import("zustand").UseBoundStore<import("zustand").StoreApi<modelStateType<S, G, Actions>>>;
|
|
5
|
+
export declare function defineContext<S extends Record<string, any>, G extends gettersStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): {
|
|
6
|
+
Provider: FunctionComponent<{
|
|
7
|
+
children?: ReactNode;
|
|
8
|
+
}>;
|
|
9
|
+
useContext: <T>(selector: (state: modelStateType<S, G, Actions>) => T) => T;
|
|
10
|
+
};
|
package/dist/define.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useRef, useContext } from 'react';
|
|
3
|
+
import { create, createStore, useStore } from 'zustand';
|
|
4
|
+
import { storeMiddleware } from './store-middleware';
|
|
5
|
+
export function defineStateFactory(options) {
|
|
6
|
+
return () => options;
|
|
7
|
+
}
|
|
8
|
+
export function defineStore(options) {
|
|
9
|
+
return create(storeMiddleware(options));
|
|
10
|
+
}
|
|
11
|
+
export function defineContext(options) {
|
|
12
|
+
const creatorResult = storeMiddleware(options);
|
|
13
|
+
const $createStore = () => createStore()(creatorResult);
|
|
14
|
+
const Context = createContext(null);
|
|
15
|
+
const Provider = ({ children }) => {
|
|
16
|
+
const storeRef = useRef($createStore());
|
|
17
|
+
return _jsx(Context.Provider, { value: storeRef.current, children: children });
|
|
18
|
+
};
|
|
19
|
+
function $useContext(selector) {
|
|
20
|
+
const store = useContext(Context);
|
|
21
|
+
if (!store)
|
|
22
|
+
throw new Error('Missing Provider in the tree');
|
|
23
|
+
return useStore(store, selector);
|
|
24
|
+
}
|
|
25
|
+
return { Provider, useContext: $useContext };
|
|
26
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { StateCreator } from 'zustand';
|
|
2
|
+
import { gettersStateType, optionsType, modelStateType } from './types';
|
|
3
|
+
export declare function storeMiddleware<S extends Record<string, any>, G extends gettersStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): StateCreator<modelStateType<S, G, Actions>>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { produce } from 'immer';
|
|
2
|
+
import { forOwn, isEqual, isEqualWith, keys, pick, some } from 'lodash-es';
|
|
3
|
+
export function storeMiddleware(options) {
|
|
4
|
+
return (set, get, store) => {
|
|
5
|
+
const state = options.state();
|
|
6
|
+
forOwn(options.getters, (getter, k) => {
|
|
7
|
+
state[k] = getter(state);
|
|
8
|
+
});
|
|
9
|
+
// getterListener
|
|
10
|
+
// ----------------------------------------------------------------------
|
|
11
|
+
store.subscribe((state, prevState) => {
|
|
12
|
+
const getterKeys = keys(options.getters);
|
|
13
|
+
const normalStateKeys = keys(options.state());
|
|
14
|
+
const normalState = pick(state, normalStateKeys);
|
|
15
|
+
const isUpdate = isEqualWith(normalState, pick(prevState, normalStateKeys), (current, prev) => some(current, (v, k) => v !== prev[k]));
|
|
16
|
+
if (isUpdate) {
|
|
17
|
+
const newGetterState = pick(state, getterKeys);
|
|
18
|
+
forOwn(options.getters, (getter, k) => {
|
|
19
|
+
const current = getter(normalState);
|
|
20
|
+
if (isEqual(newGetterState[k], current)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
newGetterState[k] = current;
|
|
24
|
+
});
|
|
25
|
+
set(newGetterState);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
// actions
|
|
29
|
+
// ----------------------------------------------------------------------
|
|
30
|
+
store.setState = (updater, replace) => {
|
|
31
|
+
const nextState = typeof updater === 'function' ? produce(updater) : updater;
|
|
32
|
+
return set(nextState, replace);
|
|
33
|
+
};
|
|
34
|
+
const reset = () => {
|
|
35
|
+
set(() => options.state());
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
...state,
|
|
39
|
+
reset,
|
|
40
|
+
setState: store.setState,
|
|
41
|
+
subscribe: store.subscribe,
|
|
42
|
+
...options.actions(store.setState, get, store)
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { defineStore } from '.';
|
|
3
|
+
// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction
|
|
4
|
+
const meta = {
|
|
5
|
+
title: 'define-zustand',
|
|
6
|
+
component: () => null
|
|
7
|
+
};
|
|
8
|
+
export default meta;
|
|
9
|
+
const useStore = defineStore({
|
|
10
|
+
state: () => ({
|
|
11
|
+
count: 0,
|
|
12
|
+
count2: 0
|
|
13
|
+
}),
|
|
14
|
+
getters: {
|
|
15
|
+
a: state => state.count + 1,
|
|
16
|
+
b: state => state.count2 + 2
|
|
17
|
+
},
|
|
18
|
+
actions: (setState, getState, store) => ({
|
|
19
|
+
test: () => getState()
|
|
20
|
+
})
|
|
21
|
+
});
|
|
22
|
+
// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args
|
|
23
|
+
export const Demo = {
|
|
24
|
+
render: () => {
|
|
25
|
+
const a = useStore(state => state.a);
|
|
26
|
+
const b = useStore(state => state.b);
|
|
27
|
+
const setState = useStore(state => state.setState);
|
|
28
|
+
const test = useStore(state => state.test);
|
|
29
|
+
console.log('a', a);
|
|
30
|
+
console.log('b', b);
|
|
31
|
+
console.log('test', test());
|
|
32
|
+
return (_jsxs(_Fragment, { children: [_jsx("button", { onClick: () => {
|
|
33
|
+
setState(state => {
|
|
34
|
+
state.count += 1;
|
|
35
|
+
state.count2 += 1;
|
|
36
|
+
});
|
|
37
|
+
setState(state => {
|
|
38
|
+
state.count2 += 1;
|
|
39
|
+
});
|
|
40
|
+
}, children: "+" }), _jsx("div", { children: a })] }));
|
|
41
|
+
}
|
|
42
|
+
};
|
package/dist/types.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { StoreApi as ZStoreApi, ExtractState as ZExtractState } from 'zustand';
|
|
2
|
-
export type
|
|
3
|
-
export type
|
|
2
|
+
export type gettersStateType<S> = Record<string, (state: S) => any>;
|
|
3
|
+
export type ExtraGettersState<G extends gettersStateType<any>> = {
|
|
4
4
|
[key in keyof G]: ReturnType<G[key]>;
|
|
5
5
|
};
|
|
6
|
-
export type stateType<S, G extends
|
|
6
|
+
export type stateType<S, G extends gettersStateType<any>> = S & ExtraGettersState<G>;
|
|
7
7
|
export type StoreApi<S> = Omit<ZStoreApi<S>, 'setState'> & {
|
|
8
8
|
setState: (updater: ((state: S) => void) | Partial<S>, replace?: boolean) => void;
|
|
9
9
|
};
|
|
@@ -13,10 +13,10 @@ export interface insideActionsType<S> {
|
|
|
13
13
|
setState: StoreApi<S>['setState'];
|
|
14
14
|
subscribe: StoreApi<S>['subscribe'];
|
|
15
15
|
}
|
|
16
|
-
export type actionsType<S, G extends
|
|
17
|
-
export interface optionsType<S, G extends
|
|
16
|
+
export type actionsType<S, G extends gettersStateType<any>, OptionAction> = (setState: insideActionsType<S>['setState'], getState: () => stateType<S, G> & insideActionsType<S>, store: StoreApi<S>) => OptionAction;
|
|
17
|
+
export interface optionsType<S, G extends gettersStateType<any>, Actions> {
|
|
18
18
|
state: () => S;
|
|
19
|
-
|
|
19
|
+
getters: G;
|
|
20
20
|
actions: actionsType<S, G, Actions>;
|
|
21
21
|
}
|
|
22
|
-
export type modelStateType<S, G extends
|
|
22
|
+
export type modelStateType<S, G extends gettersStateType<any>, Actions> = stateType<S, G> & Actions & insideActionsType<S>;
|
package/package.json
CHANGED
package/dist/utils.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { FunctionComponent, ReactNode } from 'react';
|
|
2
|
-
import { defGetterStateType, optionsType, modelStateType } from './types';
|
|
3
|
-
export declare function defineStateFactory<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): () => optionsType<S, G, OptionActions>;
|
|
4
|
-
export declare function creator<S extends Record<string, any>, G extends defGetterStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): (set: any, get: any, store: any) => modelStateType<S, G, Actions>;
|
|
5
|
-
export declare function defineStore<S extends Record<string, any>, G extends defGetterStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): import("zustand").UseBoundStore<import("zustand").StoreApi<modelStateType<S, G, Actions>>>;
|
|
6
|
-
export declare function defineContext<S extends Record<string, any>, G extends defGetterStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): {
|
|
7
|
-
Provider: FunctionComponent<{
|
|
8
|
-
children?: ReactNode;
|
|
9
|
-
}>;
|
|
10
|
-
useContext: <T>(selector: (state: modelStateType<S, G, Actions>) => T) => T;
|
|
11
|
-
};
|
package/dist/utils.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { useRef, useContext, createContext } from 'react';
|
|
2
|
-
import { create, useStore, createStore } from 'zustand';
|
|
3
|
-
import { produce } from 'immer';
|
|
4
|
-
import { keys, isEqualWith, omit, some, pick, forOwn, isEqual } from 'lodash-es';
|
|
5
|
-
import { jsx } from 'react/jsx-runtime';
|
|
6
|
-
|
|
7
|
-
function defineStateFactory(options) {
|
|
8
|
-
return () => options;
|
|
9
|
-
}
|
|
10
|
-
function creator(options) {
|
|
11
|
-
function createDefState() {
|
|
12
|
-
const state = options.state();
|
|
13
|
-
forOwn(options.getter, (getter, k) => {
|
|
14
|
-
state[k] = getter(state);
|
|
15
|
-
});
|
|
16
|
-
return state;
|
|
17
|
-
}
|
|
18
|
-
return (set, get, store) => {
|
|
19
|
-
// getterListener
|
|
20
|
-
// ----------------------------------------------------------------------
|
|
21
|
-
store.subscribe((state, prevState) => {
|
|
22
|
-
const getterKeys = keys(options.getter);
|
|
23
|
-
const isUpdate = isEqualWith(omit(state, getterKeys), omit(prevState, getterKeys), (current, prev) => some(current, (v, k) => v !== prev[k]));
|
|
24
|
-
if (isUpdate) {
|
|
25
|
-
const newGetterState = pick(state, getterKeys);
|
|
26
|
-
forOwn(options.getter, (getter, k) => {
|
|
27
|
-
if (isEqual(newGetterState[k], getter(state))) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
newGetterState[k] = getter(state);
|
|
31
|
-
});
|
|
32
|
-
set(newGetterState);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
// actions
|
|
36
|
-
// ----------------------------------------------------------------------
|
|
37
|
-
store.setState = (updater, replace) => {
|
|
38
|
-
const nextState = typeof updater === 'function' ? produce(updater) : updater;
|
|
39
|
-
return set(nextState, replace);
|
|
40
|
-
};
|
|
41
|
-
const reset = () => {
|
|
42
|
-
set(() => options.state());
|
|
43
|
-
};
|
|
44
|
-
return {
|
|
45
|
-
...createDefState(),
|
|
46
|
-
reset,
|
|
47
|
-
setState: store.setState,
|
|
48
|
-
subscribe: store.subscribe,
|
|
49
|
-
...options.actions(get, {
|
|
50
|
-
reset,
|
|
51
|
-
setState: store.setState
|
|
52
|
-
}, store)
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
function defineStore(options) {
|
|
57
|
-
return create(creator(options));
|
|
58
|
-
}
|
|
59
|
-
function defineContext(options) {
|
|
60
|
-
const creatorResult = creator(options);
|
|
61
|
-
const $createStore = () => createStore()(creatorResult);
|
|
62
|
-
const Context = /*#__PURE__*/createContext(null);
|
|
63
|
-
const Provider = ({
|
|
64
|
-
children
|
|
65
|
-
}) => {
|
|
66
|
-
const storeRef = useRef($createStore());
|
|
67
|
-
return /*#__PURE__*/jsx(Context.Provider, {
|
|
68
|
-
value: storeRef.current,
|
|
69
|
-
children: children
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
function $useContext(selector) {
|
|
73
|
-
const store = useContext(Context);
|
|
74
|
-
if (!store) throw new Error('Missing Provider in the tree');
|
|
75
|
-
return useStore(store, selector);
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
Provider,
|
|
79
|
-
useContext: $useContext
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export { creator, defineContext, defineStateFactory, defineStore };
|