define-zustand 0.1.0 → 1.0.1

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/dist/index.d.ts CHANGED
@@ -8,16 +8,16 @@ export interface insideActionsType<S> {
8
8
  }
9
9
  export type actionsType<S, OptionAction, InsideActions = unknown> = (getState: () => S, actions: insideActionsType<S> & InsideActions, store: StoreApi<S>) => OptionAction;
10
10
  export type defGetterStateType<S> = Record<string, (state: S) => any>;
11
+ export type ExtraGetterState<S, G extends defGetterStateType<S>> = {
12
+ [key in keyof G]: ReturnType<G[key]>;
13
+ };
11
14
  export interface optionsType<S, G, OptionAction> {
12
15
  state: () => S;
13
16
  getter: G;
14
17
  actions: actionsType<S, OptionAction>;
15
18
  }
16
- export type ExtraGetterStateType<S, G extends defGetterStateType<S>> = {
17
- [key in keyof G]: ReturnType<G[key]>;
18
- };
19
- export default function defineZustand<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): import("zustand").UseBoundStore<ZStoreApi<Omit<S & ExtraGetterStateType<S, G>, "reset" | "setState" | "subscribe" | keyof OptionActions> & {
19
+ export declare function define<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): import("zustand").UseBoundStore<ZStoreApi<Omit<S & ExtraGetterState<S, G>, "reset" | "setState" | "subscribe" | keyof OptionActions> & {
20
20
  reset: () => void;
21
21
  setState: (updater: Partial<S> | ((state: S) => void), replace?: boolean) => void;
22
- subscribe: (listener: (state: S & ExtraGetterStateType<S, G>, prevState: S & ExtraGetterStateType<S, G>) => void) => () => void;
22
+ subscribe: (listener: (state: S & ExtraGetterState<S, G>, prevState: S & ExtraGetterState<S, G>) => void) => () => void;
23
23
  } & OptionActions>>;
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { create } from 'zustand';
2
2
  import { combine } from 'zustand/middleware';
3
3
  import { produce } from 'immer';
4
- import { forEach, isEqual, size } from 'lodash-es';
4
+ import { keys, isEqualWith, omit, some, forEach } from 'lodash-es';
5
5
 
6
- function defineZustand(options) {
6
+ function define(options) {
7
7
  function createDefState() {
8
8
  const state = options.state();
9
9
  forEach(options.getter, (getter, k) => {
@@ -15,17 +15,13 @@ function defineZustand(options) {
15
15
  // getterListener
16
16
  // ----------------------------------------------------------------------
17
17
  store.subscribe((state, prevState) => {
18
- let equalledLen = 0;
19
- const newGetterState = {};
20
- forEach(options.getter, (getter, k) => {
21
- const value = getter(state);
22
- if (isEqual(value, prevState[k])) {
23
- equalledLen += 1;
24
- } else {
18
+ const getterKeys = keys(options.getter);
19
+ const isUpdate = isEqualWith(omit(state, getterKeys), omit(prevState, getterKeys), (current, prev) => some(current, (v, k) => v !== prev[k]));
20
+ if (isUpdate) {
21
+ const newGetterState = {};
22
+ forEach(options.getter, (getter, k) => {
25
23
  newGetterState[k] = getter(state);
26
- }
27
- });
28
- if (size(options.getter) > equalledLen) {
24
+ });
29
25
  set(newGetterState);
30
26
  }
31
27
  });
@@ -50,4 +46,4 @@ function defineZustand(options) {
50
46
  }));
51
47
  }
52
48
 
53
- export { defineZustand as default };
49
+ export { define };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "define-zustand",
3
- "version": "0.1.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",