cogsbox-state 0.5.226 → 0.5.227

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/CogsState.tsx +73 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-state",
3
- "version": "0.5.226",
3
+ "version": "0.5.227",
4
4
  "description": "React state management library with form controls and server sync",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/CogsState.tsx CHANGED
@@ -1798,7 +1798,77 @@ function createProxyHandler<T>(
1798
1798
  };
1799
1799
  }
1800
1800
 
1801
- if (prop === "stateMap" || prop === "stateMapNoRender") {
1801
+ // 1. The new logic, ONLY for `stateMap`
1802
+ if (prop === "stateMap") {
1803
+ return (
1804
+ callbackfn: (
1805
+ value: InferArrayElement<T>,
1806
+ setter: StateObject<InferArrayElement<T>>,
1807
+ info: {
1808
+ register: () => void;
1809
+ index: number;
1810
+ originalIndex: number;
1811
+ }
1812
+ ) => any
1813
+ ) => {
1814
+ const arrayToMap = currentState as any[];
1815
+ return arrayToMap.map((item, index) => {
1816
+ let originalIndex: number;
1817
+ if (
1818
+ meta?.validIndices &&
1819
+ meta.validIndices[index] !== undefined
1820
+ ) {
1821
+ originalIndex = meta.validIndices[index]!;
1822
+ } else {
1823
+ originalIndex = index;
1824
+ }
1825
+
1826
+ const finalPath = [...path, originalIndex.toString()];
1827
+ const setter = rebuildStateShape(item, finalPath, meta);
1828
+
1829
+ const register = () => {
1830
+ const [, forceUpdate] = useState({});
1831
+ const itemComponentId = `${componentId}-${path.join(".")}-${originalIndex}`;
1832
+
1833
+ useLayoutEffect(() => {
1834
+ const fullComponentId = `${stateKey}////${itemComponentId}`;
1835
+ const stateEntry = getGlobalStore
1836
+ .getState()
1837
+ .stateComponents.get(stateKey) || {
1838
+ components: new Map(),
1839
+ };
1840
+
1841
+ stateEntry.components.set(fullComponentId, {
1842
+ forceUpdate: () => forceUpdate({}),
1843
+ paths: new Set([""]),
1844
+ });
1845
+
1846
+ getGlobalStore
1847
+ .getState()
1848
+ .stateComponents.set(stateKey, stateEntry);
1849
+
1850
+ return () => {
1851
+ const currentEntry = getGlobalStore
1852
+ .getState()
1853
+ .stateComponents.get(stateKey);
1854
+ if (currentEntry) {
1855
+ currentEntry.components.delete(fullComponentId);
1856
+ }
1857
+ };
1858
+ }, [stateKey, itemComponentId]);
1859
+ };
1860
+
1861
+ return callbackfn(item, setter, {
1862
+ register,
1863
+ index,
1864
+ originalIndex,
1865
+ });
1866
+ });
1867
+ };
1868
+ }
1869
+
1870
+ // 2. The previous, simple logic, ONLY for `stateMapNoRender`
1871
+ if (prop === "stateMapNoRender") {
1802
1872
  return (
1803
1873
  callbackfn: (
1804
1874
  value: InferArrayElement<T>,
@@ -1811,7 +1881,6 @@ function createProxyHandler<T>(
1811
1881
  const arrayToMap = currentState as any[];
1812
1882
  return arrayToMap.map((item, index) => {
1813
1883
  let originalIndex: number;
1814
- // We READ from the meta object using the CORRECT property name: `validIndices`.
1815
1884
  if (
1816
1885
  meta?.validIndices &&
1817
1886
  meta.validIndices[index] !== undefined
@@ -1822,7 +1891,8 @@ function createProxyHandler<T>(
1822
1891
  }
1823
1892
  const finalPath = [...path, originalIndex.toString()];
1824
1893
 
1825
- const setter = rebuildStateShape(item, finalPath, meta); // Pass meta through
1894
+ const setter = rebuildStateShape(item, finalPath, meta);
1895
+
1826
1896
  return callbackfn(
1827
1897
  item,
1828
1898
  setter,
@@ -2032,14 +2102,6 @@ function createProxyHandler<T>(
2032
2102
  if (prop === "get") {
2033
2103
  return () => getGlobalStore.getState().getNestedState(stateKey, path);
2034
2104
  }
2035
- if (prop === "$derive") {
2036
- return (fn: any) =>
2037
- $cogsSignal({
2038
- _stateKey: stateKey,
2039
- _path: path,
2040
- _effect: fn.toString(),
2041
- });
2042
- }
2043
2105
 
2044
2106
  if (prop === "$derive") {
2045
2107
  return (fn: any) =>