define-zustand 3.2.1 → 3.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # define-zustand
2
2
 
3
- > Quickly define [zustand](https://github.com/pmndrs/zustand) state
3
+ > Quickly define [zustand](https://github.com/pmndrs/zustand) state. Supports defining computed properties.
4
4
 
5
5
  ## Install
6
6
 
@@ -0,0 +1,10 @@
1
+ import { FunctionComponent, ReactNode } from 'react';
2
+ import { gettersStateType, 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<import("./types").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: import("./types").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
@@ -1,2 +1,3 @@
1
- export * from './utils';
2
1
  export * from './types';
2
+ export * from './store-middleware';
3
+ export * from './define';
package/dist/index.js CHANGED
@@ -1 +1,3 @@
1
- export { creator, defineContext, defineStateFactory, defineStore } from './utils.js';
1
+ export * from './types';
2
+ export * from './store-middleware';
3
+ export * from './define';
@@ -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(get, { reset, setState: store.setState }, store)
43
+ };
44
+ };
45
+ }
@@ -0,0 +1,5 @@
1
+ import type { Meta, StoryObj } from '@storybook/react';
2
+ declare const meta: Meta;
3
+ export default meta;
4
+ type Story = StoryObj;
5
+ export declare const Primary: Story;
@@ -0,0 +1,45 @@
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: get => ({
19
+ test: () => {
20
+ console.log('1', 1);
21
+ },
22
+ t: () => {
23
+ get();
24
+ }
25
+ })
26
+ });
27
+ // More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args
28
+ export const Primary = {
29
+ render: () => {
30
+ const a = useStore(state => state.a);
31
+ const b = useStore(state => state.b);
32
+ const setState = useStore(state => state.setState);
33
+ console.log('a', a);
34
+ console.log('b', b);
35
+ return (_jsxs(_Fragment, { children: [_jsx("button", { onClick: () => {
36
+ setState(state => {
37
+ state.count += 1;
38
+ state.count2 += 1;
39
+ });
40
+ setState(state => {
41
+ state.count2 += 1;
42
+ });
43
+ }, children: "+" }), _jsx("div", { children: a })] }));
44
+ }
45
+ };
package/dist/types.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { StoreApi as ZStoreApi, ExtractState as ZExtractState } from 'zustand';
2
- export type defGetterStateType<S> = Record<string, (state: S) => any>;
3
- export type ExtraGetterState<S, G extends defGetterStateType<S>> = {
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 defGetterStateType<S>> = S & ExtraGetterState<S, G>;
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 defGetterStateType<S>, OptionAction> = (getState: () => stateType<S, G>, actions: Omit<insideActionsType<S>, 'subscribe'>, store: StoreApi<S>) => OptionAction;
17
- export interface optionsType<S, G extends defGetterStateType<S>, Actions> {
16
+ export type actionsType<S, G extends gettersStateType<any>, OptionAction> = (getState: () => stateType<S, G> & OptionAction, actions: Omit<insideActionsType<S>, 'subscribe'>, store: StoreApi<S>) => OptionAction;
17
+ export interface optionsType<S, G extends gettersStateType<any>, Actions> {
18
18
  state: () => S;
19
- getter: G;
19
+ getters: G;
20
20
  actions: actionsType<S, G, Actions>;
21
21
  }
22
- export type modelStateType<S, G extends defGetterStateType<S>, Actions> = stateType<S, G> & Actions & insideActionsType<S>;
22
+ export type modelStateType<S, G extends gettersStateType<any>, Actions> = stateType<S, G> & Actions & insideActionsType<S>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "define-zustand",
3
- "version": "3.2.1",
3
+ "version": "3.3.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
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 };