@elliemae/ds-zustand-helpers 3.14.0 → 3.14.2

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.
Files changed (33) hide show
  1. package/dist/cjs/{AutoUpdater.js → AutoUpdaterComponent.js} +8 -14
  2. package/dist/cjs/AutoUpdaterComponent.js.map +7 -0
  3. package/dist/cjs/builders.js +76 -0
  4. package/dist/cjs/builders.js.map +7 -0
  5. package/dist/cjs/createInternalAndPropsContext.js.map +2 -2
  6. package/dist/cjs/types.js.map +1 -1
  7. package/dist/cjs/useInternalStoreCreator.js +31 -37
  8. package/dist/cjs/useInternalStoreCreator.js.map +2 -2
  9. package/dist/cjs/usePropsStoreCreator.js +10 -6
  10. package/dist/cjs/usePropsStoreCreator.js.map +2 -2
  11. package/dist/cjs/withZustand.js +7 -8
  12. package/dist/cjs/withZustand.js.map +2 -2
  13. package/dist/esm/AutoUpdaterComponent.js +15 -0
  14. package/dist/esm/AutoUpdaterComponent.js.map +7 -0
  15. package/dist/esm/builders.js +50 -0
  16. package/dist/esm/builders.js.map +7 -0
  17. package/dist/esm/createInternalAndPropsContext.js.map +2 -2
  18. package/dist/esm/useInternalStoreCreator.js +31 -37
  19. package/dist/esm/useInternalStoreCreator.js.map +2 -2
  20. package/dist/esm/usePropsStoreCreator.js +10 -6
  21. package/dist/esm/usePropsStoreCreator.js.map +2 -2
  22. package/dist/esm/withZustand.js +6 -7
  23. package/dist/esm/withZustand.js.map +2 -2
  24. package/dist/types/builders.d.ts +4 -0
  25. package/dist/types/createInternalAndPropsContext.d.ts +8 -8
  26. package/dist/types/types.d.ts +46 -26
  27. package/dist/types/useInternalStoreCreator.d.ts +11 -1
  28. package/dist/types/usePropsStoreCreator.d.ts +3 -1
  29. package/package.json +3 -2
  30. package/dist/cjs/AutoUpdater.js.map +0 -7
  31. package/dist/esm/AutoUpdater.js +0 -21
  32. package/dist/esm/AutoUpdater.js.map +0 -7
  33. /package/dist/types/{AutoUpdater.d.ts → AutoUpdaterComponent.d.ts} +0 -0
@@ -22,26 +22,20 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
  mod
23
23
  ));
24
24
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
- var AutoUpdater_exports = {};
26
- __export(AutoUpdater_exports, {
25
+ var AutoUpdaterComponent_exports = {};
26
+ __export(AutoUpdaterComponent_exports, {
27
27
  AutoUpdaterComponent: () => AutoUpdaterComponent
28
28
  });
29
- module.exports = __toCommonJS(AutoUpdater_exports);
29
+ module.exports = __toCommonJS(AutoUpdaterComponent_exports);
30
30
  var React = __toESM(require("react"));
31
31
  var import_jsx_runtime = require("react/jsx-runtime");
32
32
  var import_react = require("react");
33
- const getPropsSetter = (state) => state.set;
34
- const getInternalSetter = (state) => state.set;
35
33
  const AutoUpdaterComponent = (props) => {
36
- const { usePropsStore, useInternalStore, autoCalculatedData, propsWithDefault, Component } = props;
37
- const setProps = usePropsStore(getPropsSetter);
38
- const setInternal = useInternalStore(getInternalSetter);
34
+ const { usePropsStore, autoCalculatedData, propsWithDefault, Component } = props;
35
+ const setProps = usePropsStore((0, import_react.useCallback)((state) => state.set, []));
39
36
  (0, import_react.useEffect)(() => {
40
- setProps({ ...propsWithDefault });
41
- }, [propsWithDefault, setProps]);
42
- (0, import_react.useEffect)(() => {
43
- setInternal(autoCalculatedData);
44
- }, [setInternal, autoCalculatedData]);
37
+ setProps({ ...propsWithDefault, ...autoCalculatedData });
38
+ }, [propsWithDefault, setProps, autoCalculatedData]);
45
39
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, {});
46
40
  };
47
- //# sourceMappingURL=AutoUpdater.js.map
41
+ //# sourceMappingURL=AutoUpdaterComponent.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/AutoUpdaterComponent.tsx", "../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["import React, { useCallback, useEffect } from 'react';\nimport type { ZustandT } from './types';\n\nexport const AutoUpdaterComponent: ZustandT.AutoUpdaterComponentT = (props) => {\n const { usePropsStore, autoCalculatedData, propsWithDefault, Component } = props;\n const setProps = usePropsStore(useCallback((state) => state.set, []));\n\n useEffect(() => {\n setProps({ ...propsWithDefault, ...autoCalculatedData });\n }, [propsWithDefault, setProps, autoCalculatedData]);\n\n return <Component />;\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADWd;AAXT,mBAA8C;AAGvC,MAAM,uBAAuD,CAAC,UAAU;AAC7E,QAAM,EAAE,eAAe,oBAAoB,kBAAkB,UAAU,IAAI;AAC3E,QAAM,WAAW,kBAAc,0BAAY,CAAC,UAAU,MAAM,KAAK,CAAC,CAAC,CAAC;AAEpE,8BAAU,MAAM;AACd,aAAS,EAAE,GAAG,kBAAkB,GAAG,mBAAmB,CAAC;AAAA,EACzD,GAAG,CAAC,kBAAkB,UAAU,kBAAkB,CAAC;AAEnD,SAAO,4CAAC,aAAU;AACpB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var builders_exports = {};
26
+ __export(builders_exports, {
27
+ buildGetters: () => buildGetters,
28
+ buildRefGetterAndSetter: () => buildRefGetterAndSetter,
29
+ buildSetters: () => buildSetters
30
+ });
31
+ module.exports = __toCommonJS(builders_exports);
32
+ var React = __toESM(require("react"));
33
+ const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
34
+ const getKeys = (obj) => Object.keys(obj);
35
+ const buildGetters = (internalAtomDefaultValues, get) => {
36
+ const internalAtomGetters = {};
37
+ getKeys(internalAtomDefaultValues).forEach((key) => {
38
+ const keyForGetter = `get${capitalize(key)}`;
39
+ const getter = () => get()[key];
40
+ internalAtomGetters[keyForGetter] = getter;
41
+ });
42
+ return internalAtomGetters;
43
+ };
44
+ const buildSetters = (internalAtomDefaultValues, get, set) => {
45
+ const internalAtomSetters = {};
46
+ getKeys(internalAtomDefaultValues).forEach((key) => {
47
+ const keyForSetter = `set${capitalize(key)}`;
48
+ const setter = (newVal) => {
49
+ if (typeof newVal === "function") {
50
+ set({ [key]: newVal(get()[key], get()) });
51
+ } else {
52
+ set({ [key]: newVal });
53
+ }
54
+ };
55
+ internalAtomSetters[keyForSetter] = setter;
56
+ });
57
+ return internalAtomSetters;
58
+ };
59
+ const buildRefGetterAndSetter = (get, set) => {
60
+ const getZustandRef = (key) => get().zustandRefs[key.join(",")];
61
+ const setZustandRef = (key, value) => {
62
+ set((state) => {
63
+ if (state instanceof Map || state instanceof Set)
64
+ return;
65
+ const zustandRefs = state.zustandRefs;
66
+ return {
67
+ zustandRefs: {
68
+ ...zustandRefs,
69
+ [key.join(",")]: value
70
+ }
71
+ };
72
+ });
73
+ };
74
+ return [getZustandRef, setZustandRef];
75
+ };
76
+ //# sourceMappingURL=builders.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/builders.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["import type { ZustandT } from './types';\n\nconst capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);\n\nconst getKeys = <T extends object>(obj: T) => Object.keys(obj) as (keyof T)[];\n\nexport const buildGetters = <A extends object, S extends object, R extends object>(\n internalAtomDefaultValues: A,\n get: ZustandT.Getter<ZustandT.InternalStore<A, S, R>>,\n) => {\n const internalAtomGetters: ZustandT.AtomGetters<A> = {} as ZustandT.AtomGetters<A>;\n\n getKeys(internalAtomDefaultValues).forEach((key) => {\n const keyForGetter = `get${capitalize(key as string)}` as keyof ZustandT.AtomGetters<A>;\n\n const getter = (() => get()[key]) as ZustandT.AtomGetters<A>[keyof ZustandT.AtomGetters<A>];\n\n internalAtomGetters[keyForGetter] = getter;\n });\n\n return internalAtomGetters;\n};\n\nexport const buildSetters = <A extends object, S extends object, R extends object>(\n internalAtomDefaultValues: A,\n get: ZustandT.Getter<ZustandT.InternalStore<A, S, R>>,\n set: ZustandT.Setter<ZustandT.InternalStore<A, S, R>>,\n) => {\n const internalAtomSetters: ZustandT.AtomSetters<A, S, R> = {} as ZustandT.AtomSetters<A, S, R>;\n\n getKeys(internalAtomDefaultValues).forEach((key) => {\n const keyForSetter = `set${capitalize(key as string)}` as keyof ZustandT.AtomSetters<A, S, R>;\n\n const setter = ((newVal: A[typeof key]) => {\n if (typeof newVal === 'function') {\n // If it's a function, we call it with the current store value, and the full store. We try to mimic react-setState's behavior\n set({ [key]: newVal(get()[key], get()) as A[typeof key] } as Partial<ZustandT.InternalStore<A, S, R>>);\n } else {\n // We just received the new value, set it\n set({ [key]: newVal } as Partial<ZustandT.InternalStore<A, S, R>>);\n }\n }) as ZustandT.AtomSetters<A, S, R>[keyof ZustandT.AtomSetters<A, S, R>];\n\n // We add the setter to the internalAtomSetters object\n internalAtomSetters[keyForSetter] = setter;\n });\n\n return internalAtomSetters;\n};\n\nexport const buildRefGetterAndSetter = <A extends object, S extends object, R extends object>(\n get: ZustandT.Getter<ZustandT.InternalStore<A, S, R>>,\n set: ZustandT.Setter<ZustandT.InternalStore<A, S, R>>,\n) => {\n const getZustandRef = (key: string[]): HTMLElement | null => get().zustandRefs[key.join(',')];\n\n const setZustandRef = (key: string[], value: HTMLElement | null) => {\n set((state) => {\n if (state instanceof Map || state instanceof Set) return;\n const zustandRefs = state.zustandRefs;\n return {\n zustandRefs: {\n ...zustandRefs,\n [key.join(',')]: value,\n },\n };\n });\n };\n\n return [getZustandRef, setZustandRef] as const;\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADEvB,MAAM,aAAa,CAAC,QAAgB,IAAI,OAAO,CAAC,EAAE,YAAY,IAAI,IAAI,MAAM,CAAC;AAE7E,MAAM,UAAU,CAAmB,QAAW,OAAO,KAAK,GAAG;AAEtD,MAAM,eAAe,CAC1B,2BACA,QACG;AACH,QAAM,sBAA+C,CAAC;AAEtD,UAAQ,yBAAyB,EAAE,QAAQ,CAAC,QAAQ;AAClD,UAAM,eAAe,MAAM,WAAW,GAAa;AAEnD,UAAM,SAAU,MAAM,IAAI,EAAE;AAE5B,wBAAoB,gBAAgB;AAAA,EACtC,CAAC;AAED,SAAO;AACT;AAEO,MAAM,eAAe,CAC1B,2BACA,KACA,QACG;AACH,QAAM,sBAAqD,CAAC;AAE5D,UAAQ,yBAAyB,EAAE,QAAQ,CAAC,QAAQ;AAClD,UAAM,eAAe,MAAM,WAAW,GAAa;AAEnD,UAAM,SAAU,CAAC,WAA0B;AACzC,UAAI,OAAO,WAAW,YAAY;AAEhC,YAAI,EAAE,CAAC,MAAM,OAAO,IAAI,EAAE,MAAM,IAAI,CAAC,EAAmB,CAA6C;AAAA,MACvG,OAAO;AAEL,YAAI,EAAE,CAAC,MAAM,OAAO,CAA6C;AAAA,MACnE;AAAA,IACF;AAGA,wBAAoB,gBAAgB;AAAA,EACtC,CAAC;AAED,SAAO;AACT;AAEO,MAAM,0BAA0B,CACrC,KACA,QACG;AACH,QAAM,gBAAgB,CAAC,QAAsC,IAAI,EAAE,YAAY,IAAI,KAAK,GAAG;AAE3F,QAAM,gBAAgB,CAAC,KAAe,UAA8B;AAClE,QAAI,CAAC,UAAU;AACb,UAAI,iBAAiB,OAAO,iBAAiB;AAAK;AAClD,YAAM,cAAc,MAAM;AAC1B,aAAO;AAAA,QACL,aAAa;AAAA,UACX,GAAG;AAAA,UACH,CAAC,IAAI,KAAK,GAAG,IAAI;AAAA,QACnB;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,CAAC,eAAe,aAAa;AACtC;",
6
+ "names": []
7
+ }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/createInternalAndPropsContext.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
- "sourcesContent": ["import createContext from 'zustand/context';\nimport type { ZustandT } from './types';\n\nexport const createInternalAndPropsContext = <\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n>() => {\n const { Provider: PropsProvider, useStore: usePropsStore } =\n createContext<ZustandT.ZustandStore<ZustandT.PropsStore<P>>>();\n const { Provider: InternalProvider, useStore: useInternalStore } =\n createContext<ZustandT.ZustandStore<ZustandT.InternalStore<A, C, S, R>>>();\n\n return { PropsProvider, usePropsStore, InternalProvider, useInternalStore };\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADAvB,qBAA0B;AAGnB,MAAM,gCAAgC,MAMtC;AACL,QAAM,EAAE,UAAU,eAAe,UAAU,cAAc,QACvD,eAAAA,SAA6D;AAC/D,QAAM,EAAE,UAAU,kBAAkB,UAAU,iBAAiB,QAC7D,eAAAA,SAAyE;AAE3E,SAAO,EAAE,eAAe,eAAe,kBAAkB,iBAAiB;AAC5E;",
4
+ "sourcesContent": ["import createContext from 'zustand/context';\nimport type { ZustandT } from './types';\n\nexport const createInternalAndPropsContext = <\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n>() => {\n const { Provider: PropsProvider, useStore: usePropsStore } =\n createContext<ZustandT.ZustandStore<ZustandT.PropsStore<P & C>>>();\n\n const { Provider: InternalProvider, useStore: useInternalStore } =\n createContext<ZustandT.ZustandStore<ZustandT.InternalStore<A, S, R>>>();\n\n return { PropsProvider, usePropsStore, InternalProvider, useInternalStore };\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADAvB,qBAA0B;AAGnB,MAAM,gCAAgC,MAMtC;AACL,QAAM,EAAE,UAAU,eAAe,UAAU,cAAc,QACvD,eAAAA,SAAiE;AAEnE,QAAM,EAAE,UAAU,kBAAkB,UAAU,iBAAiB,QAC7D,eAAAA,SAAsE;AAExE,SAAO,EAAE,eAAe,eAAe,kBAAkB,iBAAiB;AAC5E;",
6
6
  "names": ["createContext"]
7
7
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/types.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
- "sourcesContent": ["/* eslint-disable no-use-before-define */\nimport type { StoreApi, UseBoundStore } from 'zustand';\nimport type createContext from 'zustand/context';\n\n// U is a generic assigned to the user props for clarity\n// P is a generic assigned to the internal props for clarity\n// A is a generic assigned to the atoms for clarity\n// C is a generic assigned to the auto-calculated values for clarity\n// S is a generic assigned to selectors for clarity\n// R is a generic assigned to reducers for clarity\n\nexport namespace ZustandT {\n export type ZustandStore<T> = UseBoundStore<StoreApi<T>>;\n\n export type Getter<T> = StoreApi<T>['getState'];\n export type Setter<T> = StoreApi<T>['setState'];\n\n export type InternalStore<A extends object, C extends object, S extends object, R extends object> = A &\n AtomGetters<A> &\n AtomSetters<A, C, S, R> &\n C &\n S &\n R & {\n set: Setter<InternalStore<A, C, S, R>>;\n };\n\n export type PropsStore<T> = T & {\n set: Setter<PropsStore<T>>;\n };\n\n export type AtomGetters<A extends object> = {\n [key in keyof A as `get${Capitalize<string & key>}`]: () => A[key];\n };\n\n export type AtomSetters<A extends object, C extends object, S extends object, R extends object> = {\n [key in keyof A as `set${Capitalize<string & key>}`]: (\n newVal: A[key] | ((arg: A[key], store: InternalStore<A, C, S, R>) => A[key]),\n ) => void;\n };\n\n export type SelectorObject<T, S extends object> = (get: Getter<T>) => S;\n export type ReducerObject<T, R extends object> = (get: Getter<T>, set: Setter<T>) => R;\n\n export type InternalParameters<A extends object, C extends object, S extends object, R extends object> = {\n internalAtomDefaultValues: A;\n autoCalculatedData: C;\n selectors: SelectorObject<InternalStore<A, C, S, R>, S>;\n reducers: ReducerObject<InternalStore<A, C, S, R>, R>;\n };\n\n export type InternalAndPropsContext<\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n > = {\n PropsProvider: ReturnType<typeof createContext<ZustandStore<PropsStore<P>>>>['Provider'];\n usePropsStore: ReturnType<typeof createContext<ZustandStore<PropsStore<P>>>>['useStore'];\n InternalProvider: ReturnType<typeof createContext<ZustandStore<InternalStore<A, C, S, R>>>>['Provider'];\n useInternalStore: ReturnType<typeof createContext<ZustandStore<InternalStore<A, C, S, R>>>>['useStore'];\n };\n\n export type Config<P extends object, A extends object, C extends object, S extends object, R extends object> = {\n defaultProps: P;\n internalAtomDefaultValues: A;\n useAutoCalculated: (props: P) => C;\n selectors?: SelectorObject<InternalStore<A, C, S, R>, S>;\n reducers?: ReducerObject<InternalStore<A, C, S, R>, R>;\n } & InternalAndPropsContext<P, A, C, S, R>;\n\n export type AutoUpdaterComponentT = <\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n >(\n props: Pick<Config<P, A, C, S, R>, 'usePropsStore' | 'useInternalStore'> & { autoCalculatedData: C } & {\n propsWithDefault: P;\n } & { Component: React.ComponentType<Record<string, never>> },\n ) => JSX.Element;\n}\n", "import * as React from 'react';\nexport { React };\n"],
4
+ "sourcesContent": ["/* eslint-disable no-use-before-define */\nimport type { Draft } from 'immer';\nimport type { StoreApi, UseBoundStore } from 'zustand';\nimport type createContext from 'zustand/context';\n\n// U is a generic assigned to the user props for clarity\n// P is a generic assigned to the internal props for clarity\n// A is a generic assigned to the atoms for clarity\n// C is a generic assigned to the auto-calculated values for clarity\n// S is a generic assigned to selectors for clarity\n// R is a generic assigned to reducers for clarity\n\nexport namespace ZustandT {\n type Write<T, U> = Omit<T, keyof U> & U;\n\n type ImmerSetter<T extends object> = {\n setState: (\n nextStateOrUpdater: T | Partial<T> | ((state: Draft<T>) => void),\n shouldReplace?: boolean | undefined,\n ) => void;\n };\n\n type StoreSubscribeWithSelector<T> = {\n subscribe: {\n (listener: (selectedState: T, previousSelectedState: T) => void): () => void;\n <U>(\n selector: (state: T) => U,\n listener: (selectedState: U, previousSelectedState: U) => void,\n options?: {\n equalityFn?: (a: U, b: U) => boolean;\n fireImmediately?: boolean;\n },\n ): () => void;\n };\n };\n\n // We add the immer setter method type\n type StoreWithImmer<T extends object> = Write<StoreApi<T>, ImmerSetter<T>>;\n\n // We add the subscribe with selector method type\n type StoreWithImmerAndSubscriber<T extends object> = Write<StoreWithImmer<T>, StoreSubscribeWithSelector<T>>;\n\n export type ZustandStore<T extends object> = UseBoundStore<StoreWithImmerAndSubscriber<T>>;\n\n export type Getter<T extends object> = StoreWithImmerAndSubscriber<T>['getState'];\n export type Setter<T extends object> = StoreWithImmerAndSubscriber<T>['setState'];\n\n export type InternalStore<A extends object, S extends object, R extends object> = A &\n AtomGetters<A> &\n AtomSetters<A, S, R> &\n S &\n R & {\n get: Getter<InternalStore<A, S, R>>;\n set: Setter<InternalStore<A, S, R>>;\n zustandRefs: Record<string, HTMLElement | null>;\n getZustandRef: (key: string[]) => HTMLElement | null;\n setZustandRef: (key: string[], value: HTMLElement | null) => void;\n };\n\n export type PropsStore<T> = T & {\n set: Setter<PropsStore<T>>;\n };\n\n export type AtomGetters<A extends object> = {\n [key in keyof A as `get${Capitalize<string & key>}`]: () => A[key];\n };\n\n export type AtomSetters<A extends object, S extends object, R extends object> = {\n [key in keyof A as `set${Capitalize<string & key>}`]: (\n newVal: A[key] | ((arg: A[key], store: InternalStore<A, S, R>) => A[key]),\n ) => void;\n };\n\n export type SelectorObject<T extends object, S extends object> = (get: Getter<T>) => S;\n export type ReducerObject<T extends object, R extends object> = (get: Getter<T>, set: Setter<T>) => R;\n export type Subscribers<T extends object> = (api: StoreWithImmerAndSubscriber<T>) => void;\n\n export type InternalParameters<A extends object, S extends object, R extends object> = {\n internalAtomDefaultValues: A;\n selectors: SelectorObject<InternalStore<A, S, R>, S>;\n reducers: ReducerObject<InternalStore<A, S, R>, R>;\n subscribers: Subscribers<InternalStore<A, S, R>>;\n };\n\n export type Config<P extends object, A extends object, C extends object, S extends object, R extends object> = {\n defaultProps: P;\n internalAtomDefaultValues: A;\n useAutoCalculated: (props: P) => C;\n selectors?: SelectorObject<InternalStore<A, S, R>, S>;\n reducers?: ReducerObject<InternalStore<A, S, R>, R>;\n subscribers?: Subscribers<InternalStore<A, S, R>>;\n PropsProvider: ReturnType<typeof createContext<ZustandStore<PropsStore<P & C>>>>['Provider'];\n InternalProvider: ReturnType<typeof createContext<ZustandStore<InternalStore<A, S, R>>>>['Provider'];\n usePropsStore: ReturnType<typeof createContext<ZustandStore<PropsStore<P & C>>>>['useStore'];\n };\n\n export type AutoUpdaterComponentT = <\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n >(\n props: Pick<Config<P, A, C, S, R>, 'usePropsStore'> & { autoCalculatedData: C } & {\n propsWithDefault: P;\n } & { Component: React.ComponentType<Record<string, never>> },\n ) => JSX.Element;\n}\n", "import * as React from 'react';\nexport { React };\n"],
5
5
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;ACAA,YAAuB;",
6
6
  "names": []
7
7
  }
@@ -30,54 +30,48 @@ module.exports = __toCommonJS(useInternalStoreCreator_exports);
30
30
  var React = __toESM(require("react"));
31
31
  var import_react = require("react");
32
32
  var import_zustand = __toESM(require("zustand"));
33
- const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
34
- const getKeys = (obj) => Object.keys(obj);
33
+ var import_middleware = require("zustand/middleware");
34
+ var import_immer = require("zustand/middleware/immer");
35
+ var import_builders = require("./builders");
35
36
  const createInternalStore = ({
36
37
  internalAtomDefaultValues,
37
- autoCalculatedData,
38
38
  selectors,
39
- reducers
39
+ reducers,
40
+ subscribers
40
41
  }) => {
41
- const store = (0, import_zustand.default)((set, get) => {
42
- const internalAtomGetters = {};
43
- getKeys(internalAtomDefaultValues).forEach((key) => {
44
- const keyForGetter = `get${capitalize(key)}`;
45
- const getter = () => get()[key];
46
- internalAtomGetters[keyForGetter] = getter;
47
- });
48
- const internalAtomSetters = {};
49
- getKeys(internalAtomDefaultValues).forEach((key) => {
50
- const keyForSetter = `set${capitalize(key)}`;
51
- const setter = (newVal) => {
52
- if (typeof newVal === "function") {
53
- set({ [key]: newVal(get()[key], get()) });
54
- } else {
55
- set({ [key]: newVal });
56
- }
57
- };
58
- internalAtomSetters[keyForSetter] = setter;
59
- });
60
- return {
61
- ...internalAtomDefaultValues,
62
- ...internalAtomGetters,
63
- ...internalAtomSetters,
64
- ...autoCalculatedData,
65
- ...selectors(get),
66
- ...reducers(get, set),
67
- set
68
- };
69
- });
42
+ const store = (0, import_zustand.default)(
43
+ (0, import_middleware.subscribeWithSelector)(
44
+ (0, import_immer.immer)((set, get) => {
45
+ const internalAtomGetters = (0, import_builders.buildGetters)(internalAtomDefaultValues, get);
46
+ const internalAtomSetters = (0, import_builders.buildSetters)(internalAtomDefaultValues, get, set);
47
+ const [getZustandRef, setZustandRef] = (0, import_builders.buildRefGetterAndSetter)(get, set);
48
+ return {
49
+ ...internalAtomDefaultValues,
50
+ ...internalAtomGetters,
51
+ ...internalAtomSetters,
52
+ ...selectors(get),
53
+ ...reducers(get, set),
54
+ zustandRefs: {},
55
+ getZustandRef,
56
+ setZustandRef,
57
+ get,
58
+ set
59
+ };
60
+ })
61
+ )
62
+ );
63
+ subscribers(store);
70
64
  return store;
71
65
  };
72
66
  const useInternalStoreCreator = ({
73
67
  internalAtomDefaultValues,
74
- autoCalculatedData,
75
68
  selectors,
76
- reducers
69
+ reducers,
70
+ subscribers
77
71
  }) => {
78
72
  const storeCreator = (0, import_react.useCallback)(
79
- () => createInternalStore({ internalAtomDefaultValues, autoCalculatedData, selectors, reducers }),
80
- [internalAtomDefaultValues, autoCalculatedData, selectors, reducers]
73
+ () => createInternalStore({ internalAtomDefaultValues, selectors, reducers, subscribers }),
74
+ [internalAtomDefaultValues, selectors, reducers, subscribers]
81
75
  );
82
76
  return storeCreator;
83
77
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/useInternalStoreCreator.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
- "sourcesContent": ["import { useCallback } from 'react';\nimport create from 'zustand';\nimport type { ZustandT } from './types';\n\nconst capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);\n\nconst getKeys = <T extends object>(obj: T) => Object.keys(obj) as (keyof T)[];\n\n// Function that will create the store, putting:\n// 1) the atoms\n// 2) the setters for the atoms (which we auto-calculate)\n// 3) the auto-calculated values\n// 4) the set function\n// The store is fully typed, and the setters are typed as well\nconst createInternalStore = <A extends object, C extends object, S extends object, R extends object>({\n internalAtomDefaultValues,\n autoCalculatedData,\n selectors,\n reducers,\n}: ZustandT.InternalParameters<A, C, S, R>) => {\n const store = create<ZustandT.InternalStore<A, C, S, R>>((set, get) => {\n // We build the getters\n const internalAtomGetters: ZustandT.AtomGetters<A> = {} as ZustandT.AtomGetters<A>;\n\n getKeys(internalAtomDefaultValues).forEach((key) => {\n const keyForGetter = `get${capitalize(key as string)}` as keyof ZustandT.AtomGetters<A>;\n\n const getter = (() => get()[key]) as ZustandT.AtomGetters<A>[keyof ZustandT.AtomGetters<A>];\n\n internalAtomGetters[keyForGetter] = getter;\n });\n\n // We build the setters\n const internalAtomSetters: ZustandT.AtomSetters<A, C, S, R> = {} as ZustandT.AtomSetters<A, C, S, R>;\n\n getKeys(internalAtomDefaultValues).forEach((key) => {\n const keyForSetter = `set${capitalize(key as string)}` as keyof ZustandT.AtomSetters<A, C, S, R>;\n\n const setter = ((newVal: A[typeof key]) => {\n if (typeof newVal === 'function') {\n // If it's a function, we call it with the current store value, and the full store. We try to mimic react-setState's behavior\n set({ [key]: newVal(get()[key], get()) as A[typeof key] } as Partial<ZustandT.InternalStore<A, C, S, R>>);\n } else {\n // We just received the new value, set it\n set({ [key]: newVal } as Partial<ZustandT.InternalStore<A, C, S, R>>);\n }\n }) as ZustandT.AtomSetters<A, C, S, R>[keyof ZustandT.AtomSetters<A, C, S, R>];\n\n // We add the setter to the internalAtomSetters object\n internalAtomSetters[keyForSetter] = setter;\n });\n\n return {\n ...internalAtomDefaultValues,\n ...internalAtomGetters,\n ...internalAtomSetters,\n ...autoCalculatedData,\n ...selectors(get),\n ...reducers(get, set),\n set,\n };\n });\n\n return store;\n};\n\nexport const useInternalStoreCreator = <A extends object, C extends object, S extends object, R extends object>({\n internalAtomDefaultValues,\n autoCalculatedData,\n selectors,\n reducers,\n}: ZustandT.InternalParameters<A, C, S, R>) => {\n const storeCreator = useCallback(\n () => createInternalStore({ internalAtomDefaultValues, autoCalculatedData, selectors, reducers }),\n [internalAtomDefaultValues, autoCalculatedData, selectors, reducers],\n );\n\n return storeCreator;\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADAvB,mBAA4B;AAC5B,qBAAmB;AAGnB,MAAM,aAAa,CAAC,QAAgB,IAAI,OAAO,CAAC,EAAE,YAAY,IAAI,IAAI,MAAM,CAAC;AAE7E,MAAM,UAAU,CAAmB,QAAW,OAAO,KAAK,GAAG;AAQ7D,MAAM,sBAAsB,CAAyE;AAAA,EACnG;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA+C;AAC7C,QAAM,YAAQ,eAAAA,SAA2C,CAAC,KAAK,QAAQ;AAErE,UAAM,sBAA+C,CAAC;AAEtD,YAAQ,yBAAyB,EAAE,QAAQ,CAAC,QAAQ;AAClD,YAAM,eAAe,MAAM,WAAW,GAAa;AAEnD,YAAM,SAAU,MAAM,IAAI,EAAE;AAE5B,0BAAoB,gBAAgB;AAAA,IACtC,CAAC;AAGD,UAAM,sBAAwD,CAAC;AAE/D,YAAQ,yBAAyB,EAAE,QAAQ,CAAC,QAAQ;AAClD,YAAM,eAAe,MAAM,WAAW,GAAa;AAEnD,YAAM,SAAU,CAAC,WAA0B;AACzC,YAAI,OAAO,WAAW,YAAY;AAEhC,cAAI,EAAE,CAAC,MAAM,OAAO,IAAI,EAAE,MAAM,IAAI,CAAC,EAAmB,CAAgD;AAAA,QAC1G,OAAO;AAEL,cAAI,EAAE,CAAC,MAAM,OAAO,CAAgD;AAAA,QACtE;AAAA,MACF;AAGA,0BAAoB,gBAAgB;AAAA,IACtC,CAAC;AAED,WAAO;AAAA,MACL,GAAG;AAAA,MACH,GAAG;AAAA,MACH,GAAG;AAAA,MACH,GAAG;AAAA,MACH,GAAG,UAAU,GAAG;AAAA,MAChB,GAAG,SAAS,KAAK,GAAG;AAAA,MACpB;AAAA,IACF;AAAA,EACF,CAAC;AAED,SAAO;AACT;AAEO,MAAM,0BAA0B,CAAyE;AAAA,EAC9G;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA+C;AAC7C,QAAM,mBAAe;AAAA,IACnB,MAAM,oBAAoB,EAAE,2BAA2B,oBAAoB,WAAW,SAAS,CAAC;AAAA,IAChG,CAAC,2BAA2B,oBAAoB,WAAW,QAAQ;AAAA,EACrE;AAEA,SAAO;AACT;",
4
+ "sourcesContent": ["import { useCallback } from 'react';\nimport create from 'zustand';\nimport { subscribeWithSelector } from 'zustand/middleware';\nimport { immer } from 'zustand/middleware/immer';\nimport { buildGetters, buildRefGetterAndSetter, buildSetters } from './builders';\nimport type { ZustandT } from './types';\n\n// Function that will create the store, putting:\n// 1) the atoms\n// 2) the getters for the atoms (which we auto-calculate and are mutable)\n// 3) the setters for the atoms (which we auto-calculate)\n// 4) the auto-calculated values\n// 5) selectors\n// 6) reducers\n// 7) ref object, getters and setters\n// 8) the get function\n// 9) the set function\n// After that, we configure the subscribers\n// The store is fully typed, and the setters are typed as well\nconst createInternalStore = <A extends object, S extends object, R extends object>({\n internalAtomDefaultValues,\n selectors,\n reducers,\n subscribers,\n}: ZustandT.InternalParameters<A, S, R>) => {\n const store = create(\n subscribeWithSelector(\n immer<ZustandT.InternalStore<A, S, R>>((set, get) => {\n // We build the atom getters\n const internalAtomGetters = buildGetters(internalAtomDefaultValues, get);\n\n // We build the atom setters\n const internalAtomSetters = buildSetters(internalAtomDefaultValues, get, set);\n\n // We build an object to store references to elements\n const [getZustandRef, setZustandRef] = buildRefGetterAndSetter(get, set);\n\n return {\n ...internalAtomDefaultValues,\n ...internalAtomGetters,\n ...internalAtomSetters,\n ...selectors(get),\n ...reducers(get, set),\n zustandRefs: {},\n getZustandRef,\n setZustandRef,\n get,\n set,\n };\n }),\n ),\n );\n\n // We setup all subscribers\n subscribers(store);\n\n return store;\n};\n\nexport const useInternalStoreCreator = <A extends object, S extends object, R extends object>({\n internalAtomDefaultValues,\n selectors,\n reducers,\n subscribers,\n}: ZustandT.InternalParameters<A, S, R>) => {\n const storeCreator = useCallback(\n () => createInternalStore({ internalAtomDefaultValues, selectors, reducers, subscribers }),\n [internalAtomDefaultValues, selectors, reducers, subscribers],\n );\n\n return storeCreator;\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADAvB,mBAA4B;AAC5B,qBAAmB;AACnB,wBAAsC;AACtC,mBAAsB;AACtB,sBAAoE;AAepE,MAAM,sBAAsB,CAAuD;AAAA,EACjF;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA4C;AAC1C,QAAM,YAAQ,eAAAA;AAAA,QACZ;AAAA,UACE,oBAAuC,CAAC,KAAK,QAAQ;AAEnD,cAAM,0BAAsB,8BAAa,2BAA2B,GAAG;AAGvE,cAAM,0BAAsB,8BAAa,2BAA2B,KAAK,GAAG;AAG5E,cAAM,CAAC,eAAe,aAAa,QAAI,yCAAwB,KAAK,GAAG;AAEvE,eAAO;AAAA,UACL,GAAG;AAAA,UACH,GAAG;AAAA,UACH,GAAG;AAAA,UACH,GAAG,UAAU,GAAG;AAAA,UAChB,GAAG,SAAS,KAAK,GAAG;AAAA,UACpB,aAAa,CAAC;AAAA,UACd;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAGA,cAAY,KAAK;AAEjB,SAAO;AACT;AAEO,MAAM,0BAA0B,CAAuD;AAAA,EAC5F;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA4C;AAC1C,QAAM,mBAAe;AAAA,IACnB,MAAM,oBAAoB,EAAE,2BAA2B,WAAW,UAAU,YAAY,CAAC;AAAA,IACzF,CAAC,2BAA2B,WAAW,UAAU,WAAW;AAAA,EAC9D;AAEA,SAAO;AACT;",
6
6
  "names": ["create"]
7
7
  }
@@ -30,12 +30,16 @@ module.exports = __toCommonJS(usePropsStoreCreator_exports);
30
30
  var React = __toESM(require("react"));
31
31
  var import_react = require("react");
32
32
  var import_zustand = __toESM(require("zustand"));
33
- const createPropsStore = (propsWithDefault) => (0, import_zustand.default)((set) => ({
34
- set,
35
- ...propsWithDefault
36
- }));
37
- const usePropsStoreCreator = (props) => {
38
- const storeCreator = (0, import_react.useCallback)(() => createPropsStore(props), []);
33
+ var import_immer = require("zustand/middleware/immer");
34
+ const createPropsStore = (propsWithDefault, autoCalculatedData) => (0, import_zustand.default)(
35
+ (0, import_immer.immer)((set) => ({
36
+ set,
37
+ ...propsWithDefault,
38
+ ...autoCalculatedData
39
+ }))
40
+ );
41
+ const usePropsStoreCreator = (props, autoCalculatedData) => {
42
+ const storeCreator = (0, import_react.useCallback)(() => createPropsStore(props, autoCalculatedData), []);
39
43
  return storeCreator;
40
44
  };
41
45
  //# sourceMappingURL=usePropsStoreCreator.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/usePropsStoreCreator.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
- "sourcesContent": ["import { useCallback } from 'react';\nimport create from 'zustand';\nimport type { ZustandT } from './types';\n\nconst createPropsStore = <T extends object>(propsWithDefault: T) =>\n create<ZustandT.PropsStore<T>>((set) => ({\n set,\n ...propsWithDefault,\n }));\n\nexport const usePropsStoreCreator = <P extends object>(props: P) => {\n // we want to memoize the creation of the store,\n // it doesn't make sense to create a new store every time the props change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const storeCreator = useCallback(() => createPropsStore(props), []);\n\n return storeCreator;\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADAvB,mBAA4B;AAC5B,qBAAmB;AAGnB,MAAM,mBAAmB,CAAmB,yBAC1C,eAAAA,SAA+B,CAAC,SAAS;AAAA,EACvC;AAAA,EACA,GAAG;AACL,EAAE;AAEG,MAAM,uBAAuB,CAAmB,UAAa;AAIlE,QAAM,mBAAe,0BAAY,MAAM,iBAAiB,KAAK,GAAG,CAAC,CAAC;AAElE,SAAO;AACT;",
4
+ "sourcesContent": ["import { useCallback } from 'react';\nimport create from 'zustand';\nimport { immer } from 'zustand/middleware/immer';\nimport type { ZustandT } from './types';\n\nconst createPropsStore = <P extends object, C extends object>(propsWithDefault: P, autoCalculatedData: C) =>\n create(\n immer<ZustandT.PropsStore<P & C>>((set) => ({\n set,\n ...propsWithDefault,\n ...autoCalculatedData,\n })),\n );\n\nexport const usePropsStoreCreator = <P extends object, C extends object>(props: P, autoCalculatedData: C) => {\n // we want to memoize the creation of the store,\n // it doesn't make sense to create a new store every time the props change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const storeCreator = useCallback(() => createPropsStore(props, autoCalculatedData), []);\n\n return storeCreator;\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADAvB,mBAA4B;AAC5B,qBAAmB;AACnB,mBAAsB;AAGtB,MAAM,mBAAmB,CAAqC,kBAAqB,2BACjF,eAAAA;AAAA,MACE,oBAAkC,CAAC,SAAS;AAAA,IAC1C;AAAA,IACA,GAAG;AAAA,IACH,GAAG;AAAA,EACL,EAAE;AACJ;AAEK,MAAM,uBAAuB,CAAqC,OAAU,uBAA0B;AAI3G,QAAM,mBAAe,0BAAY,MAAM,iBAAiB,OAAO,kBAAkB,GAAG,CAAC,CAAC;AAEtF,SAAO;AACT;",
6
6
  "names": ["create"]
7
7
  }
@@ -31,8 +31,8 @@ var React = __toESM(require("react"));
31
31
  var import_jsx_runtime = require("react/jsx-runtime");
32
32
  var import_ds_utilities = require("@elliemae/ds-utilities");
33
33
  var import_useInternalStoreCreator = require("./useInternalStoreCreator");
34
+ var import_AutoUpdaterComponent = require("./AutoUpdaterComponent");
34
35
  var import_usePropsStoreCreator = require("./usePropsStoreCreator");
35
- var import_AutoUpdater = require("./AutoUpdater");
36
36
  const noopish = () => ({});
37
37
  const withZustand = (Component, config) => {
38
38
  const {
@@ -41,27 +41,26 @@ const withZustand = (Component, config) => {
41
41
  useAutoCalculated,
42
42
  selectors = noopish,
43
43
  reducers = noopish,
44
+ subscribers = noopish,
44
45
  PropsProvider,
45
46
  InternalProvider,
46
- usePropsStore,
47
- useInternalStore
47
+ usePropsStore
48
48
  } = config;
49
49
  const ComponentWithStore = (props) => {
50
50
  const propsWithDefault = (0, import_ds_utilities.useMemoMergePropsWithDefault)(props, defaultProps);
51
51
  const autoCalculatedData = useAutoCalculated(propsWithDefault);
52
52
  const internalStoreCreator = (0, import_useInternalStoreCreator.useInternalStoreCreator)({
53
53
  internalAtomDefaultValues,
54
- autoCalculatedData,
55
54
  selectors,
56
- reducers
55
+ reducers,
56
+ subscribers
57
57
  });
58
- const propsStoreCreator = (0, import_usePropsStoreCreator.usePropsStoreCreator)(propsWithDefault);
58
+ const propsStoreCreator = (0, import_usePropsStoreCreator.usePropsStoreCreator)(propsWithDefault, autoCalculatedData);
59
59
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PropsProvider, { createStore: propsStoreCreator, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(InternalProvider, { createStore: internalStoreCreator, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
60
- import_AutoUpdater.AutoUpdaterComponent,
60
+ import_AutoUpdaterComponent.AutoUpdaterComponent,
61
61
  {
62
62
  Component,
63
63
  usePropsStore,
64
- useInternalStore,
65
64
  autoCalculatedData,
66
65
  propsWithDefault
67
66
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/withZustand.tsx", "../../../../scripts/build/transpile/react-shim.js"],
4
- "sourcesContent": ["/* eslint-disable indent */\nimport { useMemoMergePropsWithDefault } from '@elliemae/ds-utilities';\nimport { useInternalStoreCreator } from './useInternalStoreCreator';\nimport { usePropsStoreCreator } from './usePropsStoreCreator';\nimport { AutoUpdaterComponent } from './AutoUpdater';\nimport type { ZustandT } from './types';\n\nconst noopish = <T extends object>() => ({} as T);\n\nexport const withZustand = <\n U extends object,\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n>(\n Component: React.ComponentType<Record<string, never>>,\n config: ZustandT.Config<P, A, C, S, R>,\n) => {\n const {\n defaultProps,\n internalAtomDefaultValues,\n useAutoCalculated,\n selectors = noopish,\n reducers = noopish,\n PropsProvider,\n InternalProvider,\n usePropsStore,\n useInternalStore,\n } = config;\n\n const ComponentWithStore = (props: U) => {\n const propsWithDefault = useMemoMergePropsWithDefault<P>(props, defaultProps);\n\n const autoCalculatedData = useAutoCalculated(propsWithDefault);\n\n const internalStoreCreator = useInternalStoreCreator({\n internalAtomDefaultValues,\n autoCalculatedData,\n selectors,\n reducers,\n });\n\n const propsStoreCreator = usePropsStoreCreator(propsWithDefault);\n\n return (\n <PropsProvider createStore={propsStoreCreator}>\n <InternalProvider createStore={internalStoreCreator}>\n <AutoUpdaterComponent\n Component={Component}\n usePropsStore={usePropsStore}\n useInternalStore={useInternalStore}\n autoCalculatedData={autoCalculatedData}\n propsWithDefault={propsWithDefault}\n />\n </InternalProvider>\n </PropsProvider>\n );\n };\n return ComponentWithStore;\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADiDb;AAhDV,0BAA6C;AAC7C,qCAAwC;AACxC,kCAAqC;AACrC,yBAAqC;AAGrC,MAAM,UAAU,OAAyB,CAAC;AAEnC,MAAM,cAAc,CAQzB,WACA,WACG;AACH,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,YAAY;AAAA,IACZ,WAAW;AAAA,IACX;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,qBAAqB,CAAC,UAAa;AACvC,UAAM,uBAAmB,kDAAgC,OAAO,YAAY;AAE5E,UAAM,qBAAqB,kBAAkB,gBAAgB;AAE7D,UAAM,2BAAuB,wDAAwB;AAAA,MACnD;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,UAAM,wBAAoB,kDAAqB,gBAAgB;AAE/D,WACE,4CAAC,iBAAc,aAAa,mBAC1B,sDAAC,oBAAiB,aAAa,sBAC7B;AAAA,MAAC;AAAA;AAAA,QACC;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA;AAAA,IACF,GACF,GACF;AAAA,EAEJ;AACA,SAAO;AACT;",
4
+ "sourcesContent": ["/* eslint-disable indent */\nimport { useMemoMergePropsWithDefault } from '@elliemae/ds-utilities';\nimport { useInternalStoreCreator } from './useInternalStoreCreator';\nimport type { ZustandT } from './types';\nimport { AutoUpdaterComponent } from './AutoUpdaterComponent';\nimport { usePropsStoreCreator } from './usePropsStoreCreator';\n\nconst noopish = <T extends object>() => ({} as T);\n\nexport const withZustand = <\n U extends object,\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n>(\n Component: React.ComponentType<Record<string, never>>,\n config: ZustandT.Config<P, A, C, S, R>,\n) => {\n const {\n defaultProps,\n internalAtomDefaultValues,\n useAutoCalculated,\n selectors = noopish,\n reducers = noopish,\n subscribers = noopish,\n PropsProvider,\n InternalProvider,\n usePropsStore,\n } = config;\n\n const ComponentWithStore = (props: U) => {\n const propsWithDefault = useMemoMergePropsWithDefault<P>(props, defaultProps);\n\n const autoCalculatedData = useAutoCalculated(propsWithDefault);\n\n const internalStoreCreator = useInternalStoreCreator({\n internalAtomDefaultValues,\n selectors,\n reducers,\n subscribers,\n });\n\n const propsStoreCreator = usePropsStoreCreator(propsWithDefault, autoCalculatedData);\n\n return (\n <PropsProvider createStore={propsStoreCreator}>\n <InternalProvider createStore={internalStoreCreator}>\n <AutoUpdaterComponent\n Component={Component}\n usePropsStore={usePropsStore}\n autoCalculatedData={autoCalculatedData}\n propsWithDefault={propsWithDefault}\n />\n </InternalProvider>\n </PropsProvider>\n );\n };\n return ComponentWithStore;\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADiDb;AAhDV,0BAA6C;AAC7C,qCAAwC;AAExC,kCAAqC;AACrC,kCAAqC;AAErC,MAAM,UAAU,OAAyB,CAAC;AAEnC,MAAM,cAAc,CAQzB,WACA,WACG;AACH,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,YAAY;AAAA,IACZ,WAAW;AAAA,IACX,cAAc;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,qBAAqB,CAAC,UAAa;AACvC,UAAM,uBAAmB,kDAAgC,OAAO,YAAY;AAE5E,UAAM,qBAAqB,kBAAkB,gBAAgB;AAE7D,UAAM,2BAAuB,wDAAwB;AAAA,MACnD;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,UAAM,wBAAoB,kDAAqB,kBAAkB,kBAAkB;AAEnF,WACE,4CAAC,iBAAc,aAAa,mBAC1B,sDAAC,oBAAiB,aAAa,sBAC7B;AAAA,MAAC;AAAA;AAAA,QACC;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA;AAAA,IACF,GACF,GACF;AAAA,EAEJ;AACA,SAAO;AACT;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,15 @@
1
+ import * as React from "react";
2
+ import { jsx } from "react/jsx-runtime";
3
+ import { useCallback, useEffect } from "react";
4
+ const AutoUpdaterComponent = (props) => {
5
+ const { usePropsStore, autoCalculatedData, propsWithDefault, Component } = props;
6
+ const setProps = usePropsStore(useCallback((state) => state.set, []));
7
+ useEffect(() => {
8
+ setProps({ ...propsWithDefault, ...autoCalculatedData });
9
+ }, [propsWithDefault, setProps, autoCalculatedData]);
10
+ return /* @__PURE__ */ jsx(Component, {});
11
+ };
12
+ export {
13
+ AutoUpdaterComponent
14
+ };
15
+ //# sourceMappingURL=AutoUpdaterComponent.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/AutoUpdaterComponent.tsx"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import React, { useCallback, useEffect } from 'react';\nimport type { ZustandT } from './types';\n\nexport const AutoUpdaterComponent: ZustandT.AutoUpdaterComponentT = (props) => {\n const { usePropsStore, autoCalculatedData, propsWithDefault, Component } = props;\n const setProps = usePropsStore(useCallback((state) => state.set, []));\n\n useEffect(() => {\n setProps({ ...propsWithDefault, ...autoCalculatedData });\n }, [propsWithDefault, setProps, autoCalculatedData]);\n\n return <Component />;\n};\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACWd;AAXT,SAAgB,aAAa,iBAAiB;AAGvC,MAAM,uBAAuD,CAAC,UAAU;AAC7E,QAAM,EAAE,eAAe,oBAAoB,kBAAkB,UAAU,IAAI;AAC3E,QAAM,WAAW,cAAc,YAAY,CAAC,UAAU,MAAM,KAAK,CAAC,CAAC,CAAC;AAEpE,YAAU,MAAM;AACd,aAAS,EAAE,GAAG,kBAAkB,GAAG,mBAAmB,CAAC;AAAA,EACzD,GAAG,CAAC,kBAAkB,UAAU,kBAAkB,CAAC;AAEnD,SAAO,oBAAC,aAAU;AACpB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,50 @@
1
+ import * as React from "react";
2
+ const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
3
+ const getKeys = (obj) => Object.keys(obj);
4
+ const buildGetters = (internalAtomDefaultValues, get) => {
5
+ const internalAtomGetters = {};
6
+ getKeys(internalAtomDefaultValues).forEach((key) => {
7
+ const keyForGetter = `get${capitalize(key)}`;
8
+ const getter = () => get()[key];
9
+ internalAtomGetters[keyForGetter] = getter;
10
+ });
11
+ return internalAtomGetters;
12
+ };
13
+ const buildSetters = (internalAtomDefaultValues, get, set) => {
14
+ const internalAtomSetters = {};
15
+ getKeys(internalAtomDefaultValues).forEach((key) => {
16
+ const keyForSetter = `set${capitalize(key)}`;
17
+ const setter = (newVal) => {
18
+ if (typeof newVal === "function") {
19
+ set({ [key]: newVal(get()[key], get()) });
20
+ } else {
21
+ set({ [key]: newVal });
22
+ }
23
+ };
24
+ internalAtomSetters[keyForSetter] = setter;
25
+ });
26
+ return internalAtomSetters;
27
+ };
28
+ const buildRefGetterAndSetter = (get, set) => {
29
+ const getZustandRef = (key) => get().zustandRefs[key.join(",")];
30
+ const setZustandRef = (key, value) => {
31
+ set((state) => {
32
+ if (state instanceof Map || state instanceof Set)
33
+ return;
34
+ const zustandRefs = state.zustandRefs;
35
+ return {
36
+ zustandRefs: {
37
+ ...zustandRefs,
38
+ [key.join(",")]: value
39
+ }
40
+ };
41
+ });
42
+ };
43
+ return [getZustandRef, setZustandRef];
44
+ };
45
+ export {
46
+ buildGetters,
47
+ buildRefGetterAndSetter,
48
+ buildSetters
49
+ };
50
+ //# sourceMappingURL=builders.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/builders.ts"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import type { ZustandT } from './types';\n\nconst capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);\n\nconst getKeys = <T extends object>(obj: T) => Object.keys(obj) as (keyof T)[];\n\nexport const buildGetters = <A extends object, S extends object, R extends object>(\n internalAtomDefaultValues: A,\n get: ZustandT.Getter<ZustandT.InternalStore<A, S, R>>,\n) => {\n const internalAtomGetters: ZustandT.AtomGetters<A> = {} as ZustandT.AtomGetters<A>;\n\n getKeys(internalAtomDefaultValues).forEach((key) => {\n const keyForGetter = `get${capitalize(key as string)}` as keyof ZustandT.AtomGetters<A>;\n\n const getter = (() => get()[key]) as ZustandT.AtomGetters<A>[keyof ZustandT.AtomGetters<A>];\n\n internalAtomGetters[keyForGetter] = getter;\n });\n\n return internalAtomGetters;\n};\n\nexport const buildSetters = <A extends object, S extends object, R extends object>(\n internalAtomDefaultValues: A,\n get: ZustandT.Getter<ZustandT.InternalStore<A, S, R>>,\n set: ZustandT.Setter<ZustandT.InternalStore<A, S, R>>,\n) => {\n const internalAtomSetters: ZustandT.AtomSetters<A, S, R> = {} as ZustandT.AtomSetters<A, S, R>;\n\n getKeys(internalAtomDefaultValues).forEach((key) => {\n const keyForSetter = `set${capitalize(key as string)}` as keyof ZustandT.AtomSetters<A, S, R>;\n\n const setter = ((newVal: A[typeof key]) => {\n if (typeof newVal === 'function') {\n // If it's a function, we call it with the current store value, and the full store. We try to mimic react-setState's behavior\n set({ [key]: newVal(get()[key], get()) as A[typeof key] } as Partial<ZustandT.InternalStore<A, S, R>>);\n } else {\n // We just received the new value, set it\n set({ [key]: newVal } as Partial<ZustandT.InternalStore<A, S, R>>);\n }\n }) as ZustandT.AtomSetters<A, S, R>[keyof ZustandT.AtomSetters<A, S, R>];\n\n // We add the setter to the internalAtomSetters object\n internalAtomSetters[keyForSetter] = setter;\n });\n\n return internalAtomSetters;\n};\n\nexport const buildRefGetterAndSetter = <A extends object, S extends object, R extends object>(\n get: ZustandT.Getter<ZustandT.InternalStore<A, S, R>>,\n set: ZustandT.Setter<ZustandT.InternalStore<A, S, R>>,\n) => {\n const getZustandRef = (key: string[]): HTMLElement | null => get().zustandRefs[key.join(',')];\n\n const setZustandRef = (key: string[], value: HTMLElement | null) => {\n set((state) => {\n if (state instanceof Map || state instanceof Set) return;\n const zustandRefs = state.zustandRefs;\n return {\n zustandRefs: {\n ...zustandRefs,\n [key.join(',')]: value,\n },\n };\n });\n };\n\n return [getZustandRef, setZustandRef] as const;\n};\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACEvB,MAAM,aAAa,CAAC,QAAgB,IAAI,OAAO,CAAC,EAAE,YAAY,IAAI,IAAI,MAAM,CAAC;AAE7E,MAAM,UAAU,CAAmB,QAAW,OAAO,KAAK,GAAG;AAEtD,MAAM,eAAe,CAC1B,2BACA,QACG;AACH,QAAM,sBAA+C,CAAC;AAEtD,UAAQ,yBAAyB,EAAE,QAAQ,CAAC,QAAQ;AAClD,UAAM,eAAe,MAAM,WAAW,GAAa;AAEnD,UAAM,SAAU,MAAM,IAAI,EAAE;AAE5B,wBAAoB,gBAAgB;AAAA,EACtC,CAAC;AAED,SAAO;AACT;AAEO,MAAM,eAAe,CAC1B,2BACA,KACA,QACG;AACH,QAAM,sBAAqD,CAAC;AAE5D,UAAQ,yBAAyB,EAAE,QAAQ,CAAC,QAAQ;AAClD,UAAM,eAAe,MAAM,WAAW,GAAa;AAEnD,UAAM,SAAU,CAAC,WAA0B;AACzC,UAAI,OAAO,WAAW,YAAY;AAEhC,YAAI,EAAE,CAAC,MAAM,OAAO,IAAI,EAAE,MAAM,IAAI,CAAC,EAAmB,CAA6C;AAAA,MACvG,OAAO;AAEL,YAAI,EAAE,CAAC,MAAM,OAAO,CAA6C;AAAA,MACnE;AAAA,IACF;AAGA,wBAAoB,gBAAgB;AAAA,EACtC,CAAC;AAED,SAAO;AACT;AAEO,MAAM,0BAA0B,CACrC,KACA,QACG;AACH,QAAM,gBAAgB,CAAC,QAAsC,IAAI,EAAE,YAAY,IAAI,KAAK,GAAG;AAE3F,QAAM,gBAAgB,CAAC,KAAe,UAA8B;AAClE,QAAI,CAAC,UAAU;AACb,UAAI,iBAAiB,OAAO,iBAAiB;AAAK;AAClD,YAAM,cAAc,MAAM;AAC1B,aAAO;AAAA,QACL,aAAa;AAAA,UACX,GAAG;AAAA,UACH,CAAC,IAAI,KAAK,GAAG,IAAI;AAAA,QACnB;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,CAAC,eAAe,aAAa;AACtC;",
6
+ "names": []
7
+ }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/createInternalAndPropsContext.ts"],
4
- "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import createContext from 'zustand/context';\nimport type { ZustandT } from './types';\n\nexport const createInternalAndPropsContext = <\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n>() => {\n const { Provider: PropsProvider, useStore: usePropsStore } =\n createContext<ZustandT.ZustandStore<ZustandT.PropsStore<P>>>();\n const { Provider: InternalProvider, useStore: useInternalStore } =\n createContext<ZustandT.ZustandStore<ZustandT.InternalStore<A, C, S, R>>>();\n\n return { PropsProvider, usePropsStore, InternalProvider, useInternalStore };\n};\n"],
5
- "mappings": "AAAA,YAAY,WAAW;ACAvB,OAAO,mBAAmB;AAGnB,MAAM,gCAAgC,MAMtC;AACL,QAAM,EAAE,UAAU,eAAe,UAAU,cAAc,IACvD,cAA6D;AAC/D,QAAM,EAAE,UAAU,kBAAkB,UAAU,iBAAiB,IAC7D,cAAyE;AAE3E,SAAO,EAAE,eAAe,eAAe,kBAAkB,iBAAiB;AAC5E;",
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import createContext from 'zustand/context';\nimport type { ZustandT } from './types';\n\nexport const createInternalAndPropsContext = <\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n>() => {\n const { Provider: PropsProvider, useStore: usePropsStore } =\n createContext<ZustandT.ZustandStore<ZustandT.PropsStore<P & C>>>();\n\n const { Provider: InternalProvider, useStore: useInternalStore } =\n createContext<ZustandT.ZustandStore<ZustandT.InternalStore<A, S, R>>>();\n\n return { PropsProvider, usePropsStore, InternalProvider, useInternalStore };\n};\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACAvB,OAAO,mBAAmB;AAGnB,MAAM,gCAAgC,MAMtC;AACL,QAAM,EAAE,UAAU,eAAe,UAAU,cAAc,IACvD,cAAiE;AAEnE,QAAM,EAAE,UAAU,kBAAkB,UAAU,iBAAiB,IAC7D,cAAsE;AAExE,SAAO,EAAE,eAAe,eAAe,kBAAkB,iBAAiB;AAC5E;",
6
6
  "names": []
7
7
  }
@@ -1,54 +1,48 @@
1
1
  import * as React from "react";
2
2
  import { useCallback } from "react";
3
3
  import create from "zustand";
4
- const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
5
- const getKeys = (obj) => Object.keys(obj);
4
+ import { subscribeWithSelector } from "zustand/middleware";
5
+ import { immer } from "zustand/middleware/immer";
6
+ import { buildGetters, buildRefGetterAndSetter, buildSetters } from "./builders";
6
7
  const createInternalStore = ({
7
8
  internalAtomDefaultValues,
8
- autoCalculatedData,
9
9
  selectors,
10
- reducers
10
+ reducers,
11
+ subscribers
11
12
  }) => {
12
- const store = create((set, get) => {
13
- const internalAtomGetters = {};
14
- getKeys(internalAtomDefaultValues).forEach((key) => {
15
- const keyForGetter = `get${capitalize(key)}`;
16
- const getter = () => get()[key];
17
- internalAtomGetters[keyForGetter] = getter;
18
- });
19
- const internalAtomSetters = {};
20
- getKeys(internalAtomDefaultValues).forEach((key) => {
21
- const keyForSetter = `set${capitalize(key)}`;
22
- const setter = (newVal) => {
23
- if (typeof newVal === "function") {
24
- set({ [key]: newVal(get()[key], get()) });
25
- } else {
26
- set({ [key]: newVal });
27
- }
28
- };
29
- internalAtomSetters[keyForSetter] = setter;
30
- });
31
- return {
32
- ...internalAtomDefaultValues,
33
- ...internalAtomGetters,
34
- ...internalAtomSetters,
35
- ...autoCalculatedData,
36
- ...selectors(get),
37
- ...reducers(get, set),
38
- set
39
- };
40
- });
13
+ const store = create(
14
+ subscribeWithSelector(
15
+ immer((set, get) => {
16
+ const internalAtomGetters = buildGetters(internalAtomDefaultValues, get);
17
+ const internalAtomSetters = buildSetters(internalAtomDefaultValues, get, set);
18
+ const [getZustandRef, setZustandRef] = buildRefGetterAndSetter(get, set);
19
+ return {
20
+ ...internalAtomDefaultValues,
21
+ ...internalAtomGetters,
22
+ ...internalAtomSetters,
23
+ ...selectors(get),
24
+ ...reducers(get, set),
25
+ zustandRefs: {},
26
+ getZustandRef,
27
+ setZustandRef,
28
+ get,
29
+ set
30
+ };
31
+ })
32
+ )
33
+ );
34
+ subscribers(store);
41
35
  return store;
42
36
  };
43
37
  const useInternalStoreCreator = ({
44
38
  internalAtomDefaultValues,
45
- autoCalculatedData,
46
39
  selectors,
47
- reducers
40
+ reducers,
41
+ subscribers
48
42
  }) => {
49
43
  const storeCreator = useCallback(
50
- () => createInternalStore({ internalAtomDefaultValues, autoCalculatedData, selectors, reducers }),
51
- [internalAtomDefaultValues, autoCalculatedData, selectors, reducers]
44
+ () => createInternalStore({ internalAtomDefaultValues, selectors, reducers, subscribers }),
45
+ [internalAtomDefaultValues, selectors, reducers, subscribers]
52
46
  );
53
47
  return storeCreator;
54
48
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/useInternalStoreCreator.ts"],
4
- "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import { useCallback } from 'react';\nimport create from 'zustand';\nimport type { ZustandT } from './types';\n\nconst capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);\n\nconst getKeys = <T extends object>(obj: T) => Object.keys(obj) as (keyof T)[];\n\n// Function that will create the store, putting:\n// 1) the atoms\n// 2) the setters for the atoms (which we auto-calculate)\n// 3) the auto-calculated values\n// 4) the set function\n// The store is fully typed, and the setters are typed as well\nconst createInternalStore = <A extends object, C extends object, S extends object, R extends object>({\n internalAtomDefaultValues,\n autoCalculatedData,\n selectors,\n reducers,\n}: ZustandT.InternalParameters<A, C, S, R>) => {\n const store = create<ZustandT.InternalStore<A, C, S, R>>((set, get) => {\n // We build the getters\n const internalAtomGetters: ZustandT.AtomGetters<A> = {} as ZustandT.AtomGetters<A>;\n\n getKeys(internalAtomDefaultValues).forEach((key) => {\n const keyForGetter = `get${capitalize(key as string)}` as keyof ZustandT.AtomGetters<A>;\n\n const getter = (() => get()[key]) as ZustandT.AtomGetters<A>[keyof ZustandT.AtomGetters<A>];\n\n internalAtomGetters[keyForGetter] = getter;\n });\n\n // We build the setters\n const internalAtomSetters: ZustandT.AtomSetters<A, C, S, R> = {} as ZustandT.AtomSetters<A, C, S, R>;\n\n getKeys(internalAtomDefaultValues).forEach((key) => {\n const keyForSetter = `set${capitalize(key as string)}` as keyof ZustandT.AtomSetters<A, C, S, R>;\n\n const setter = ((newVal: A[typeof key]) => {\n if (typeof newVal === 'function') {\n // If it's a function, we call it with the current store value, and the full store. We try to mimic react-setState's behavior\n set({ [key]: newVal(get()[key], get()) as A[typeof key] } as Partial<ZustandT.InternalStore<A, C, S, R>>);\n } else {\n // We just received the new value, set it\n set({ [key]: newVal } as Partial<ZustandT.InternalStore<A, C, S, R>>);\n }\n }) as ZustandT.AtomSetters<A, C, S, R>[keyof ZustandT.AtomSetters<A, C, S, R>];\n\n // We add the setter to the internalAtomSetters object\n internalAtomSetters[keyForSetter] = setter;\n });\n\n return {\n ...internalAtomDefaultValues,\n ...internalAtomGetters,\n ...internalAtomSetters,\n ...autoCalculatedData,\n ...selectors(get),\n ...reducers(get, set),\n set,\n };\n });\n\n return store;\n};\n\nexport const useInternalStoreCreator = <A extends object, C extends object, S extends object, R extends object>({\n internalAtomDefaultValues,\n autoCalculatedData,\n selectors,\n reducers,\n}: ZustandT.InternalParameters<A, C, S, R>) => {\n const storeCreator = useCallback(\n () => createInternalStore({ internalAtomDefaultValues, autoCalculatedData, selectors, reducers }),\n [internalAtomDefaultValues, autoCalculatedData, selectors, reducers],\n );\n\n return storeCreator;\n};\n"],
5
- "mappings": "AAAA,YAAY,WAAW;ACAvB,SAAS,mBAAmB;AAC5B,OAAO,YAAY;AAGnB,MAAM,aAAa,CAAC,QAAgB,IAAI,OAAO,CAAC,EAAE,YAAY,IAAI,IAAI,MAAM,CAAC;AAE7E,MAAM,UAAU,CAAmB,QAAW,OAAO,KAAK,GAAG;AAQ7D,MAAM,sBAAsB,CAAyE;AAAA,EACnG;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA+C;AAC7C,QAAM,QAAQ,OAA2C,CAAC,KAAK,QAAQ;AAErE,UAAM,sBAA+C,CAAC;AAEtD,YAAQ,yBAAyB,EAAE,QAAQ,CAAC,QAAQ;AAClD,YAAM,eAAe,MAAM,WAAW,GAAa;AAEnD,YAAM,SAAU,MAAM,IAAI,EAAE;AAE5B,0BAAoB,gBAAgB;AAAA,IACtC,CAAC;AAGD,UAAM,sBAAwD,CAAC;AAE/D,YAAQ,yBAAyB,EAAE,QAAQ,CAAC,QAAQ;AAClD,YAAM,eAAe,MAAM,WAAW,GAAa;AAEnD,YAAM,SAAU,CAAC,WAA0B;AACzC,YAAI,OAAO,WAAW,YAAY;AAEhC,cAAI,EAAE,CAAC,MAAM,OAAO,IAAI,EAAE,MAAM,IAAI,CAAC,EAAmB,CAAgD;AAAA,QAC1G,OAAO;AAEL,cAAI,EAAE,CAAC,MAAM,OAAO,CAAgD;AAAA,QACtE;AAAA,MACF;AAGA,0BAAoB,gBAAgB;AAAA,IACtC,CAAC;AAED,WAAO;AAAA,MACL,GAAG;AAAA,MACH,GAAG;AAAA,MACH,GAAG;AAAA,MACH,GAAG;AAAA,MACH,GAAG,UAAU,GAAG;AAAA,MAChB,GAAG,SAAS,KAAK,GAAG;AAAA,MACpB;AAAA,IACF;AAAA,EACF,CAAC;AAED,SAAO;AACT;AAEO,MAAM,0BAA0B,CAAyE;AAAA,EAC9G;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA+C;AAC7C,QAAM,eAAe;AAAA,IACnB,MAAM,oBAAoB,EAAE,2BAA2B,oBAAoB,WAAW,SAAS,CAAC;AAAA,IAChG,CAAC,2BAA2B,oBAAoB,WAAW,QAAQ;AAAA,EACrE;AAEA,SAAO;AACT;",
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import { useCallback } from 'react';\nimport create from 'zustand';\nimport { subscribeWithSelector } from 'zustand/middleware';\nimport { immer } from 'zustand/middleware/immer';\nimport { buildGetters, buildRefGetterAndSetter, buildSetters } from './builders';\nimport type { ZustandT } from './types';\n\n// Function that will create the store, putting:\n// 1) the atoms\n// 2) the getters for the atoms (which we auto-calculate and are mutable)\n// 3) the setters for the atoms (which we auto-calculate)\n// 4) the auto-calculated values\n// 5) selectors\n// 6) reducers\n// 7) ref object, getters and setters\n// 8) the get function\n// 9) the set function\n// After that, we configure the subscribers\n// The store is fully typed, and the setters are typed as well\nconst createInternalStore = <A extends object, S extends object, R extends object>({\n internalAtomDefaultValues,\n selectors,\n reducers,\n subscribers,\n}: ZustandT.InternalParameters<A, S, R>) => {\n const store = create(\n subscribeWithSelector(\n immer<ZustandT.InternalStore<A, S, R>>((set, get) => {\n // We build the atom getters\n const internalAtomGetters = buildGetters(internalAtomDefaultValues, get);\n\n // We build the atom setters\n const internalAtomSetters = buildSetters(internalAtomDefaultValues, get, set);\n\n // We build an object to store references to elements\n const [getZustandRef, setZustandRef] = buildRefGetterAndSetter(get, set);\n\n return {\n ...internalAtomDefaultValues,\n ...internalAtomGetters,\n ...internalAtomSetters,\n ...selectors(get),\n ...reducers(get, set),\n zustandRefs: {},\n getZustandRef,\n setZustandRef,\n get,\n set,\n };\n }),\n ),\n );\n\n // We setup all subscribers\n subscribers(store);\n\n return store;\n};\n\nexport const useInternalStoreCreator = <A extends object, S extends object, R extends object>({\n internalAtomDefaultValues,\n selectors,\n reducers,\n subscribers,\n}: ZustandT.InternalParameters<A, S, R>) => {\n const storeCreator = useCallback(\n () => createInternalStore({ internalAtomDefaultValues, selectors, reducers, subscribers }),\n [internalAtomDefaultValues, selectors, reducers, subscribers],\n );\n\n return storeCreator;\n};\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACAvB,SAAS,mBAAmB;AAC5B,OAAO,YAAY;AACnB,SAAS,6BAA6B;AACtC,SAAS,aAAa;AACtB,SAAS,cAAc,yBAAyB,oBAAoB;AAepE,MAAM,sBAAsB,CAAuD;AAAA,EACjF;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA4C;AAC1C,QAAM,QAAQ;AAAA,IACZ;AAAA,MACE,MAAuC,CAAC,KAAK,QAAQ;AAEnD,cAAM,sBAAsB,aAAa,2BAA2B,GAAG;AAGvE,cAAM,sBAAsB,aAAa,2BAA2B,KAAK,GAAG;AAG5E,cAAM,CAAC,eAAe,aAAa,IAAI,wBAAwB,KAAK,GAAG;AAEvE,eAAO;AAAA,UACL,GAAG;AAAA,UACH,GAAG;AAAA,UACH,GAAG;AAAA,UACH,GAAG,UAAU,GAAG;AAAA,UAChB,GAAG,SAAS,KAAK,GAAG;AAAA,UACpB,aAAa,CAAC;AAAA,UACd;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAGA,cAAY,KAAK;AAEjB,SAAO;AACT;AAEO,MAAM,0BAA0B,CAAuD;AAAA,EAC5F;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA4C;AAC1C,QAAM,eAAe;AAAA,IACnB,MAAM,oBAAoB,EAAE,2BAA2B,WAAW,UAAU,YAAY,CAAC;AAAA,IACzF,CAAC,2BAA2B,WAAW,UAAU,WAAW;AAAA,EAC9D;AAEA,SAAO;AACT;",
6
6
  "names": []
7
7
  }
@@ -1,12 +1,16 @@
1
1
  import * as React from "react";
2
2
  import { useCallback } from "react";
3
3
  import create from "zustand";
4
- const createPropsStore = (propsWithDefault) => create((set) => ({
5
- set,
6
- ...propsWithDefault
7
- }));
8
- const usePropsStoreCreator = (props) => {
9
- const storeCreator = useCallback(() => createPropsStore(props), []);
4
+ import { immer } from "zustand/middleware/immer";
5
+ const createPropsStore = (propsWithDefault, autoCalculatedData) => create(
6
+ immer((set) => ({
7
+ set,
8
+ ...propsWithDefault,
9
+ ...autoCalculatedData
10
+ }))
11
+ );
12
+ const usePropsStoreCreator = (props, autoCalculatedData) => {
13
+ const storeCreator = useCallback(() => createPropsStore(props, autoCalculatedData), []);
10
14
  return storeCreator;
11
15
  };
12
16
  export {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/usePropsStoreCreator.ts"],
4
- "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import { useCallback } from 'react';\nimport create from 'zustand';\nimport type { ZustandT } from './types';\n\nconst createPropsStore = <T extends object>(propsWithDefault: T) =>\n create<ZustandT.PropsStore<T>>((set) => ({\n set,\n ...propsWithDefault,\n }));\n\nexport const usePropsStoreCreator = <P extends object>(props: P) => {\n // we want to memoize the creation of the store,\n // it doesn't make sense to create a new store every time the props change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const storeCreator = useCallback(() => createPropsStore(props), []);\n\n return storeCreator;\n};\n"],
5
- "mappings": "AAAA,YAAY,WAAW;ACAvB,SAAS,mBAAmB;AAC5B,OAAO,YAAY;AAGnB,MAAM,mBAAmB,CAAmB,qBAC1C,OAA+B,CAAC,SAAS;AAAA,EACvC;AAAA,EACA,GAAG;AACL,EAAE;AAEG,MAAM,uBAAuB,CAAmB,UAAa;AAIlE,QAAM,eAAe,YAAY,MAAM,iBAAiB,KAAK,GAAG,CAAC,CAAC;AAElE,SAAO;AACT;",
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import { useCallback } from 'react';\nimport create from 'zustand';\nimport { immer } from 'zustand/middleware/immer';\nimport type { ZustandT } from './types';\n\nconst createPropsStore = <P extends object, C extends object>(propsWithDefault: P, autoCalculatedData: C) =>\n create(\n immer<ZustandT.PropsStore<P & C>>((set) => ({\n set,\n ...propsWithDefault,\n ...autoCalculatedData,\n })),\n );\n\nexport const usePropsStoreCreator = <P extends object, C extends object>(props: P, autoCalculatedData: C) => {\n // we want to memoize the creation of the store,\n // it doesn't make sense to create a new store every time the props change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const storeCreator = useCallback(() => createPropsStore(props, autoCalculatedData), []);\n\n return storeCreator;\n};\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACAvB,SAAS,mBAAmB;AAC5B,OAAO,YAAY;AACnB,SAAS,aAAa;AAGtB,MAAM,mBAAmB,CAAqC,kBAAqB,uBACjF;AAAA,EACE,MAAkC,CAAC,SAAS;AAAA,IAC1C;AAAA,IACA,GAAG;AAAA,IACH,GAAG;AAAA,EACL,EAAE;AACJ;AAEK,MAAM,uBAAuB,CAAqC,OAAU,uBAA0B;AAI3G,QAAM,eAAe,YAAY,MAAM,iBAAiB,OAAO,kBAAkB,GAAG,CAAC,CAAC;AAEtF,SAAO;AACT;",
6
6
  "names": []
7
7
  }
@@ -2,8 +2,8 @@ import * as React from "react";
2
2
  import { jsx } from "react/jsx-runtime";
3
3
  import { useMemoMergePropsWithDefault } from "@elliemae/ds-utilities";
4
4
  import { useInternalStoreCreator } from "./useInternalStoreCreator";
5
+ import { AutoUpdaterComponent } from "./AutoUpdaterComponent";
5
6
  import { usePropsStoreCreator } from "./usePropsStoreCreator";
6
- import { AutoUpdaterComponent } from "./AutoUpdater";
7
7
  const noopish = () => ({});
8
8
  const withZustand = (Component, config) => {
9
9
  const {
@@ -12,27 +12,26 @@ const withZustand = (Component, config) => {
12
12
  useAutoCalculated,
13
13
  selectors = noopish,
14
14
  reducers = noopish,
15
+ subscribers = noopish,
15
16
  PropsProvider,
16
17
  InternalProvider,
17
- usePropsStore,
18
- useInternalStore
18
+ usePropsStore
19
19
  } = config;
20
20
  const ComponentWithStore = (props) => {
21
21
  const propsWithDefault = useMemoMergePropsWithDefault(props, defaultProps);
22
22
  const autoCalculatedData = useAutoCalculated(propsWithDefault);
23
23
  const internalStoreCreator = useInternalStoreCreator({
24
24
  internalAtomDefaultValues,
25
- autoCalculatedData,
26
25
  selectors,
27
- reducers
26
+ reducers,
27
+ subscribers
28
28
  });
29
- const propsStoreCreator = usePropsStoreCreator(propsWithDefault);
29
+ const propsStoreCreator = usePropsStoreCreator(propsWithDefault, autoCalculatedData);
30
30
  return /* @__PURE__ */ jsx(PropsProvider, { createStore: propsStoreCreator, children: /* @__PURE__ */ jsx(InternalProvider, { createStore: internalStoreCreator, children: /* @__PURE__ */ jsx(
31
31
  AutoUpdaterComponent,
32
32
  {
33
33
  Component,
34
34
  usePropsStore,
35
- useInternalStore,
36
35
  autoCalculatedData,
37
36
  propsWithDefault
38
37
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/withZustand.tsx"],
4
- "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable indent */\nimport { useMemoMergePropsWithDefault } from '@elliemae/ds-utilities';\nimport { useInternalStoreCreator } from './useInternalStoreCreator';\nimport { usePropsStoreCreator } from './usePropsStoreCreator';\nimport { AutoUpdaterComponent } from './AutoUpdater';\nimport type { ZustandT } from './types';\n\nconst noopish = <T extends object>() => ({} as T);\n\nexport const withZustand = <\n U extends object,\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n>(\n Component: React.ComponentType<Record<string, never>>,\n config: ZustandT.Config<P, A, C, S, R>,\n) => {\n const {\n defaultProps,\n internalAtomDefaultValues,\n useAutoCalculated,\n selectors = noopish,\n reducers = noopish,\n PropsProvider,\n InternalProvider,\n usePropsStore,\n useInternalStore,\n } = config;\n\n const ComponentWithStore = (props: U) => {\n const propsWithDefault = useMemoMergePropsWithDefault<P>(props, defaultProps);\n\n const autoCalculatedData = useAutoCalculated(propsWithDefault);\n\n const internalStoreCreator = useInternalStoreCreator({\n internalAtomDefaultValues,\n autoCalculatedData,\n selectors,\n reducers,\n });\n\n const propsStoreCreator = usePropsStoreCreator(propsWithDefault);\n\n return (\n <PropsProvider createStore={propsStoreCreator}>\n <InternalProvider createStore={internalStoreCreator}>\n <AutoUpdaterComponent\n Component={Component}\n usePropsStore={usePropsStore}\n useInternalStore={useInternalStore}\n autoCalculatedData={autoCalculatedData}\n propsWithDefault={propsWithDefault}\n />\n </InternalProvider>\n </PropsProvider>\n );\n };\n return ComponentWithStore;\n};\n"],
5
- "mappings": "AAAA,YAAY,WAAW;ACiDb;AAhDV,SAAS,oCAAoC;AAC7C,SAAS,+BAA+B;AACxC,SAAS,4BAA4B;AACrC,SAAS,4BAA4B;AAGrC,MAAM,UAAU,OAAyB,CAAC;AAEnC,MAAM,cAAc,CAQzB,WACA,WACG;AACH,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,YAAY;AAAA,IACZ,WAAW;AAAA,IACX;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,qBAAqB,CAAC,UAAa;AACvC,UAAM,mBAAmB,6BAAgC,OAAO,YAAY;AAE5E,UAAM,qBAAqB,kBAAkB,gBAAgB;AAE7D,UAAM,uBAAuB,wBAAwB;AAAA,MACnD;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,UAAM,oBAAoB,qBAAqB,gBAAgB;AAE/D,WACE,oBAAC,iBAAc,aAAa,mBAC1B,8BAAC,oBAAiB,aAAa,sBAC7B;AAAA,MAAC;AAAA;AAAA,QACC;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA;AAAA,IACF,GACF,GACF;AAAA,EAEJ;AACA,SAAO;AACT;",
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable indent */\nimport { useMemoMergePropsWithDefault } from '@elliemae/ds-utilities';\nimport { useInternalStoreCreator } from './useInternalStoreCreator';\nimport type { ZustandT } from './types';\nimport { AutoUpdaterComponent } from './AutoUpdaterComponent';\nimport { usePropsStoreCreator } from './usePropsStoreCreator';\n\nconst noopish = <T extends object>() => ({} as T);\n\nexport const withZustand = <\n U extends object,\n P extends object,\n A extends object,\n C extends object,\n S extends object,\n R extends object,\n>(\n Component: React.ComponentType<Record<string, never>>,\n config: ZustandT.Config<P, A, C, S, R>,\n) => {\n const {\n defaultProps,\n internalAtomDefaultValues,\n useAutoCalculated,\n selectors = noopish,\n reducers = noopish,\n subscribers = noopish,\n PropsProvider,\n InternalProvider,\n usePropsStore,\n } = config;\n\n const ComponentWithStore = (props: U) => {\n const propsWithDefault = useMemoMergePropsWithDefault<P>(props, defaultProps);\n\n const autoCalculatedData = useAutoCalculated(propsWithDefault);\n\n const internalStoreCreator = useInternalStoreCreator({\n internalAtomDefaultValues,\n selectors,\n reducers,\n subscribers,\n });\n\n const propsStoreCreator = usePropsStoreCreator(propsWithDefault, autoCalculatedData);\n\n return (\n <PropsProvider createStore={propsStoreCreator}>\n <InternalProvider createStore={internalStoreCreator}>\n <AutoUpdaterComponent\n Component={Component}\n usePropsStore={usePropsStore}\n autoCalculatedData={autoCalculatedData}\n propsWithDefault={propsWithDefault}\n />\n </InternalProvider>\n </PropsProvider>\n );\n };\n return ComponentWithStore;\n};\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACiDb;AAhDV,SAAS,oCAAoC;AAC7C,SAAS,+BAA+B;AAExC,SAAS,4BAA4B;AACrC,SAAS,4BAA4B;AAErC,MAAM,UAAU,OAAyB,CAAC;AAEnC,MAAM,cAAc,CAQzB,WACA,WACG;AACH,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,YAAY;AAAA,IACZ,WAAW;AAAA,IACX,cAAc;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,qBAAqB,CAAC,UAAa;AACvC,UAAM,mBAAmB,6BAAgC,OAAO,YAAY;AAE5E,UAAM,qBAAqB,kBAAkB,gBAAgB;AAE7D,UAAM,uBAAuB,wBAAwB;AAAA,MACnD;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,UAAM,oBAAoB,qBAAqB,kBAAkB,kBAAkB;AAEnF,WACE,oBAAC,iBAAc,aAAa,mBAC1B,8BAAC,oBAAiB,aAAa,sBAC7B;AAAA,MAAC;AAAA;AAAA,QACC;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA;AAAA,IACF,GACF,GACF;AAAA,EAEJ;AACA,SAAO;AACT;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,4 @@
1
+ import type { ZustandT } from './types';
2
+ export declare const buildGetters: <A extends object, S extends object, R extends object>(internalAtomDefaultValues: A, get: () => ZustandT.InternalStore<A, S, R>) => ZustandT.AtomGetters<A>;
3
+ export declare const buildSetters: <A extends object, S extends object, R extends object>(internalAtomDefaultValues: A, get: () => ZustandT.InternalStore<A, S, R>, set: (nextStateOrUpdater: ZustandT.InternalStore<A, S, R> | Partial<ZustandT.InternalStore<A, S, R>> | ((state: import("immer").Draft<ZustandT.InternalStore<A, S, R>>) => void), shouldReplace?: boolean | undefined) => void) => ZustandT.AtomSetters<A, S, R>;
4
+ export declare const buildRefGetterAndSetter: <A extends object, S extends object, R extends object>(get: () => ZustandT.InternalStore<A, S, R>, set: (nextStateOrUpdater: ZustandT.InternalStore<A, S, R> | Partial<ZustandT.InternalStore<A, S, R>> | ((state: import("immer").Draft<ZustandT.InternalStore<A, S, R>>) => void), shouldReplace?: boolean | undefined) => void) => readonly [(key: string[]) => HTMLElement | null, (key: string[], value: HTMLElement | null) => void];
@@ -2,19 +2,19 @@
2
2
  import type { ZustandT } from './types';
3
3
  export declare const createInternalAndPropsContext: <P extends object, A extends object, C extends object, S extends object, R extends object>() => {
4
4
  PropsProvider: ({ createStore, children, }: {
5
- createStore: () => ZustandT.ZustandStore<ZustandT.PropsStore<P>>;
5
+ createStore: () => ZustandT.ZustandStore<ZustandT.PropsStore<P & C>>;
6
6
  children: import("react").ReactNode;
7
- }) => import("react").FunctionComponentElement<import("react").ProviderProps<ZustandT.ZustandStore<ZustandT.PropsStore<P>> | undefined>>;
7
+ }) => import("react").FunctionComponentElement<import("react").ProviderProps<ZustandT.ZustandStore<ZustandT.PropsStore<P & C>> | undefined>>;
8
8
  usePropsStore: {
9
- (): ZustandT.PropsStore<P>;
10
- <U>(selector: (state: ZustandT.PropsStore<P>) => U, equalityFn?: ((a: U, b: U) => boolean) | undefined): U;
9
+ (): ZustandT.PropsStore<P & C>;
10
+ <U>(selector: (state: ZustandT.PropsStore<P & C>) => U, equalityFn?: ((a: U, b: U) => boolean) | undefined): U;
11
11
  };
12
12
  InternalProvider: ({ createStore, children, }: {
13
- createStore: () => ZustandT.ZustandStore<ZustandT.InternalStore<A, C, S, R>>;
13
+ createStore: () => ZustandT.ZustandStore<ZustandT.InternalStore<A, S, R>>;
14
14
  children: import("react").ReactNode;
15
- }) => import("react").FunctionComponentElement<import("react").ProviderProps<ZustandT.ZustandStore<ZustandT.InternalStore<A, C, S, R>> | undefined>>;
15
+ }) => import("react").FunctionComponentElement<import("react").ProviderProps<ZustandT.ZustandStore<ZustandT.InternalStore<A, S, R>> | undefined>>;
16
16
  useInternalStore: {
17
- (): ZustandT.InternalStore<A, C, S, R>;
18
- <U_1>(selector: (state: ZustandT.InternalStore<A, C, S, R>) => U_1, equalityFn?: ((a: U_1, b: U_1) => boolean) | undefined): U_1;
17
+ (): ZustandT.InternalStore<A, S, R>;
18
+ <U_1>(selector: (state: ZustandT.InternalStore<A, S, R>) => U_1, equalityFn?: ((a: U_1, b: U_1) => boolean) | undefined): U_1;
19
19
  };
20
20
  };
@@ -1,48 +1,68 @@
1
1
  /// <reference types="react" />
2
+ import type { Draft } from 'immer';
2
3
  import type { StoreApi, UseBoundStore } from 'zustand';
3
4
  import type createContext from 'zustand/context';
4
5
  export declare namespace ZustandT {
5
- type ZustandStore<T> = UseBoundStore<StoreApi<T>>;
6
- type Getter<T> = StoreApi<T>['getState'];
7
- type Setter<T> = StoreApi<T>['setState'];
8
- type InternalStore<A extends object, C extends object, S extends object, R extends object> = A & AtomGetters<A> & AtomSetters<A, C, S, R> & C & S & R & {
9
- set: Setter<InternalStore<A, C, S, R>>;
6
+ type Write<T, U> = Omit<T, keyof U> & U;
7
+ type ImmerSetter<T extends object> = {
8
+ setState: (nextStateOrUpdater: T | Partial<T> | ((state: Draft<T>) => void), shouldReplace?: boolean | undefined) => void;
10
9
  };
11
- type PropsStore<T> = T & {
10
+ type StoreSubscribeWithSelector<T> = {
11
+ subscribe: {
12
+ (listener: (selectedState: T, previousSelectedState: T) => void): () => void;
13
+ <U>(selector: (state: T) => U, listener: (selectedState: U, previousSelectedState: U) => void, options?: {
14
+ equalityFn?: (a: U, b: U) => boolean;
15
+ fireImmediately?: boolean;
16
+ }): () => void;
17
+ };
18
+ };
19
+ type StoreWithImmer<T extends object> = Write<StoreApi<T>, ImmerSetter<T>>;
20
+ type StoreWithImmerAndSubscriber<T extends object> = Write<StoreWithImmer<T>, StoreSubscribeWithSelector<T>>;
21
+ export type ZustandStore<T extends object> = UseBoundStore<StoreWithImmerAndSubscriber<T>>;
22
+ export type Getter<T extends object> = StoreWithImmerAndSubscriber<T>['getState'];
23
+ export type Setter<T extends object> = StoreWithImmerAndSubscriber<T>['setState'];
24
+ export type InternalStore<A extends object, S extends object, R extends object> = A & AtomGetters<A> & AtomSetters<A, S, R> & S & R & {
25
+ get: Getter<InternalStore<A, S, R>>;
26
+ set: Setter<InternalStore<A, S, R>>;
27
+ zustandRefs: Record<string, HTMLElement | null>;
28
+ getZustandRef: (key: string[]) => HTMLElement | null;
29
+ setZustandRef: (key: string[], value: HTMLElement | null) => void;
30
+ };
31
+ export type PropsStore<T> = T & {
12
32
  set: Setter<PropsStore<T>>;
13
33
  };
14
- type AtomGetters<A extends object> = {
34
+ export type AtomGetters<A extends object> = {
15
35
  [key in keyof A as `get${Capitalize<string & key>}`]: () => A[key];
16
36
  };
17
- type AtomSetters<A extends object, C extends object, S extends object, R extends object> = {
18
- [key in keyof A as `set${Capitalize<string & key>}`]: (newVal: A[key] | ((arg: A[key], store: InternalStore<A, C, S, R>) => A[key])) => void;
37
+ export type AtomSetters<A extends object, S extends object, R extends object> = {
38
+ [key in keyof A as `set${Capitalize<string & key>}`]: (newVal: A[key] | ((arg: A[key], store: InternalStore<A, S, R>) => A[key])) => void;
19
39
  };
20
- type SelectorObject<T, S extends object> = (get: Getter<T>) => S;
21
- type ReducerObject<T, R extends object> = (get: Getter<T>, set: Setter<T>) => R;
22
- type InternalParameters<A extends object, C extends object, S extends object, R extends object> = {
40
+ export type SelectorObject<T extends object, S extends object> = (get: Getter<T>) => S;
41
+ export type ReducerObject<T extends object, R extends object> = (get: Getter<T>, set: Setter<T>) => R;
42
+ export type Subscribers<T extends object> = (api: StoreWithImmerAndSubscriber<T>) => void;
43
+ export type InternalParameters<A extends object, S extends object, R extends object> = {
23
44
  internalAtomDefaultValues: A;
24
- autoCalculatedData: C;
25
- selectors: SelectorObject<InternalStore<A, C, S, R>, S>;
26
- reducers: ReducerObject<InternalStore<A, C, S, R>, R>;
45
+ selectors: SelectorObject<InternalStore<A, S, R>, S>;
46
+ reducers: ReducerObject<InternalStore<A, S, R>, R>;
47
+ subscribers: Subscribers<InternalStore<A, S, R>>;
27
48
  };
28
- type InternalAndPropsContext<P extends object, A extends object, C extends object, S extends object, R extends object> = {
29
- PropsProvider: ReturnType<typeof createContext<ZustandStore<PropsStore<P>>>>['Provider'];
30
- usePropsStore: ReturnType<typeof createContext<ZustandStore<PropsStore<P>>>>['useStore'];
31
- InternalProvider: ReturnType<typeof createContext<ZustandStore<InternalStore<A, C, S, R>>>>['Provider'];
32
- useInternalStore: ReturnType<typeof createContext<ZustandStore<InternalStore<A, C, S, R>>>>['useStore'];
33
- };
34
- type Config<P extends object, A extends object, C extends object, S extends object, R extends object> = {
49
+ export type Config<P extends object, A extends object, C extends object, S extends object, R extends object> = {
35
50
  defaultProps: P;
36
51
  internalAtomDefaultValues: A;
37
52
  useAutoCalculated: (props: P) => C;
38
- selectors?: SelectorObject<InternalStore<A, C, S, R>, S>;
39
- reducers?: ReducerObject<InternalStore<A, C, S, R>, R>;
40
- } & InternalAndPropsContext<P, A, C, S, R>;
41
- type AutoUpdaterComponentT = <P extends object, A extends object, C extends object, S extends object, R extends object>(props: Pick<Config<P, A, C, S, R>, 'usePropsStore' | 'useInternalStore'> & {
53
+ selectors?: SelectorObject<InternalStore<A, S, R>, S>;
54
+ reducers?: ReducerObject<InternalStore<A, S, R>, R>;
55
+ subscribers?: Subscribers<InternalStore<A, S, R>>;
56
+ PropsProvider: ReturnType<typeof createContext<ZustandStore<PropsStore<P & C>>>>['Provider'];
57
+ InternalProvider: ReturnType<typeof createContext<ZustandStore<InternalStore<A, S, R>>>>['Provider'];
58
+ usePropsStore: ReturnType<typeof createContext<ZustandStore<PropsStore<P & C>>>>['useStore'];
59
+ };
60
+ export type AutoUpdaterComponentT = <P extends object, A extends object, C extends object, S extends object, R extends object>(props: Pick<Config<P, A, C, S, R>, 'usePropsStore'> & {
42
61
  autoCalculatedData: C;
43
62
  } & {
44
63
  propsWithDefault: P;
45
64
  } & {
46
65
  Component: React.ComponentType<Record<string, never>>;
47
66
  }) => JSX.Element;
67
+ export {};
48
68
  }
@@ -1,2 +1,12 @@
1
1
  import type { ZustandT } from './types';
2
- export declare const useInternalStoreCreator: <A extends object, C extends object, S extends object, R extends object>({ internalAtomDefaultValues, autoCalculatedData, selectors, reducers, }: ZustandT.InternalParameters<A, C, S, R>) => () => import("zustand").UseBoundStore<import("zustand").StoreApi<ZustandT.InternalStore<A, C, S, R>>>;
2
+ export declare const useInternalStoreCreator: <A extends object, S extends object, R extends object>({ internalAtomDefaultValues, selectors, reducers, subscribers, }: ZustandT.InternalParameters<A, S, R>) => () => import("zustand").UseBoundStore<Omit<Omit<import("zustand").StoreApi<ZustandT.InternalStore<A, S, R>>, "subscribe"> & {
3
+ subscribe: {
4
+ (listener: (selectedState: ZustandT.InternalStore<A, S, R>, previousSelectedState: ZustandT.InternalStore<A, S, R>) => void): () => void;
5
+ <U>(selector: (state: ZustandT.InternalStore<A, S, R>) => U, listener: (selectedState: U, previousSelectedState: U) => void, options?: {
6
+ equalityFn?: ((a: U, b: U) => boolean) | undefined;
7
+ fireImmediately?: boolean | undefined;
8
+ } | undefined): () => void;
9
+ };
10
+ }, "setState"> & {
11
+ setState(nextStateOrUpdater: ZustandT.InternalStore<A, S, R> | Partial<ZustandT.InternalStore<A, S, R>> | ((state: import("immer").Draft<ZustandT.InternalStore<A, S, R>>) => void), shouldReplace?: boolean | undefined): void;
12
+ }>;
@@ -1,2 +1,4 @@
1
1
  import type { ZustandT } from './types';
2
- export declare const usePropsStoreCreator: <P extends object>(props: P) => () => import("zustand").UseBoundStore<import("zustand").StoreApi<ZustandT.PropsStore<P>>>;
2
+ export declare const usePropsStoreCreator: <P extends object, C extends object>(props: P, autoCalculatedData: C) => () => import("zustand").UseBoundStore<Omit<import("zustand").StoreApi<ZustandT.PropsStore<P & C>>, "setState"> & {
3
+ setState(nextStateOrUpdater: ZustandT.PropsStore<P & C> | Partial<ZustandT.PropsStore<P & C>> | ((state: import("immer").Draft<ZustandT.PropsStore<P & C>>) => void), shouldReplace?: boolean | undefined): void;
4
+ }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elliemae/ds-zustand-helpers",
3
- "version": "3.14.0",
3
+ "version": "3.14.2",
4
4
  "license": "MIT",
5
5
  "description": "ICE MT - Dimsum - Zustand Helpers",
6
6
  "files": [
@@ -39,8 +39,9 @@
39
39
  "typeSafety": false
40
40
  },
41
41
  "dependencies": {
42
+ "immer": "9.0.19",
42
43
  "zustand": "4.1.4",
43
- "@elliemae/ds-utilities": "3.14.0"
44
+ "@elliemae/ds-utilities": "3.14.2"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@testing-library/jest-dom": "~5.16.5",
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/AutoUpdater.tsx", "../../../../scripts/build/transpile/react-shim.js"],
4
- "sourcesContent": ["import React, { useEffect } from 'react';\nimport type { ZustandT } from './types';\n\nconst getPropsSetter = <P extends object>(state: ZustandT.PropsStore<P>) => state.set;\nconst getInternalSetter = <A extends object, C extends object, S extends object, R extends object>(\n state: ZustandT.InternalStore<A, C, S, R>,\n) => state.set;\n\nexport const AutoUpdaterComponent: ZustandT.AutoUpdaterComponentT = (props) => {\n const { usePropsStore, useInternalStore, autoCalculatedData, propsWithDefault, Component } = props;\n const setProps = usePropsStore(getPropsSetter);\n const setInternal = useInternalStore(getInternalSetter);\n\n useEffect(() => {\n setProps({ ...propsWithDefault });\n }, [propsWithDefault, setProps]);\n\n useEffect(() => {\n setInternal(autoCalculatedData);\n }, [setInternal, autoCalculatedData]);\n\n return <Component />;\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADqBd;AArBT,mBAAiC;AAGjC,MAAM,iBAAiB,CAAmB,UAAkC,MAAM;AAClF,MAAM,oBAAoB,CACxB,UACG,MAAM;AAEJ,MAAM,uBAAuD,CAAC,UAAU;AAC7E,QAAM,EAAE,eAAe,kBAAkB,oBAAoB,kBAAkB,UAAU,IAAI;AAC7F,QAAM,WAAW,cAAc,cAAc;AAC7C,QAAM,cAAc,iBAAiB,iBAAiB;AAEtD,8BAAU,MAAM;AACd,aAAS,EAAE,GAAG,iBAAiB,CAAC;AAAA,EAClC,GAAG,CAAC,kBAAkB,QAAQ,CAAC;AAE/B,8BAAU,MAAM;AACd,gBAAY,kBAAkB;AAAA,EAChC,GAAG,CAAC,aAAa,kBAAkB,CAAC;AAEpC,SAAO,4CAAC,aAAU;AACpB;",
6
- "names": []
7
- }
@@ -1,21 +0,0 @@
1
- import * as React from "react";
2
- import { jsx } from "react/jsx-runtime";
3
- import { useEffect } from "react";
4
- const getPropsSetter = (state) => state.set;
5
- const getInternalSetter = (state) => state.set;
6
- const AutoUpdaterComponent = (props) => {
7
- const { usePropsStore, useInternalStore, autoCalculatedData, propsWithDefault, Component } = props;
8
- const setProps = usePropsStore(getPropsSetter);
9
- const setInternal = useInternalStore(getInternalSetter);
10
- useEffect(() => {
11
- setProps({ ...propsWithDefault });
12
- }, [propsWithDefault, setProps]);
13
- useEffect(() => {
14
- setInternal(autoCalculatedData);
15
- }, [setInternal, autoCalculatedData]);
16
- return /* @__PURE__ */ jsx(Component, {});
17
- };
18
- export {
19
- AutoUpdaterComponent
20
- };
21
- //# sourceMappingURL=AutoUpdater.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/AutoUpdater.tsx"],
4
- "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import React, { useEffect } from 'react';\nimport type { ZustandT } from './types';\n\nconst getPropsSetter = <P extends object>(state: ZustandT.PropsStore<P>) => state.set;\nconst getInternalSetter = <A extends object, C extends object, S extends object, R extends object>(\n state: ZustandT.InternalStore<A, C, S, R>,\n) => state.set;\n\nexport const AutoUpdaterComponent: ZustandT.AutoUpdaterComponentT = (props) => {\n const { usePropsStore, useInternalStore, autoCalculatedData, propsWithDefault, Component } = props;\n const setProps = usePropsStore(getPropsSetter);\n const setInternal = useInternalStore(getInternalSetter);\n\n useEffect(() => {\n setProps({ ...propsWithDefault });\n }, [propsWithDefault, setProps]);\n\n useEffect(() => {\n setInternal(autoCalculatedData);\n }, [setInternal, autoCalculatedData]);\n\n return <Component />;\n};\n"],
5
- "mappings": "AAAA,YAAY,WAAW;ACqBd;AArBT,SAAgB,iBAAiB;AAGjC,MAAM,iBAAiB,CAAmB,UAAkC,MAAM;AAClF,MAAM,oBAAoB,CACxB,UACG,MAAM;AAEJ,MAAM,uBAAuD,CAAC,UAAU;AAC7E,QAAM,EAAE,eAAe,kBAAkB,oBAAoB,kBAAkB,UAAU,IAAI;AAC7F,QAAM,WAAW,cAAc,cAAc;AAC7C,QAAM,cAAc,iBAAiB,iBAAiB;AAEtD,YAAU,MAAM;AACd,aAAS,EAAE,GAAG,iBAAiB,CAAC;AAAA,EAClC,GAAG,CAAC,kBAAkB,QAAQ,CAAC;AAE/B,YAAU,MAAM;AACd,gBAAY,kBAAkB;AAAA,EAChC,GAAG,CAAC,aAAa,kBAAkB,CAAC;AAEpC,SAAO,oBAAC,aAAU;AACpB;",
6
- "names": []
7
- }