cogsbox-state 0.5.281 → 0.5.282

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-state",
3
- "version": "0.5.281",
3
+ "version": "0.5.282",
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
@@ -1039,6 +1039,8 @@ export function useCogsStateFn<TStateObject extends unknown>(
1039
1039
  const pathKey = `${thisKey}-${path.join(".")}`;
1040
1040
  componentUpdatesRef.current.add(pathKey);
1041
1041
  }
1042
+ const store = getGlobalStore.getState();
1043
+
1042
1044
  setState(thisKey, (prevValue: TStateObject) => {
1043
1045
  const payload = isFunction<TStateObject>(newStateOrFunction)
1044
1046
  ? newStateOrFunction(prevValue as TStateObject)
@@ -1047,9 +1049,7 @@ export function useCogsStateFn<TStateObject extends unknown>(
1047
1049
  const signalId = `${thisKey}-${path.join(".")}`;
1048
1050
  if (signalId) {
1049
1051
  let isArrayOperation = false;
1050
- let elements = getGlobalStore
1051
- .getState()
1052
- .signalDomElements.get(signalId);
1052
+ let elements = store.signalDomElements.get(signalId);
1053
1053
 
1054
1054
  if (
1055
1055
  (!elements || elements.size === 0) &&
@@ -1062,9 +1062,7 @@ export function useCogsStateFn<TStateObject extends unknown>(
1062
1062
  if (Array.isArray(arrayValue)) {
1063
1063
  isArrayOperation = true;
1064
1064
  const arraySignalId = `${thisKey}-${arrayPath.join(".")}`;
1065
- elements = getGlobalStore
1066
- .getState()
1067
- .signalDomElements.get(arraySignalId);
1065
+ elements = store.signalDomElements.get(arraySignalId);
1068
1066
  }
1069
1067
  }
1070
1068
 
@@ -1089,32 +1087,7 @@ export function useCogsStateFn<TStateObject extends unknown>(
1089
1087
  }
1090
1088
  }
1091
1089
 
1092
- const shadowUpdate = () => {
1093
- const store = getGlobalStore.getState();
1094
-
1095
- switch (updateObj.updateType) {
1096
- case "update":
1097
- // For updates, just mirror the structure at the path
1098
- store.updateShadowAtPath(thisKey, path, payload);
1099
- break;
1100
-
1101
- case "insert":
1102
- // For array insert, add empty element to shadow array
1103
- const parentPath = path.slice(0, -1);
1104
- store.insertShadowArrayElement(thisKey, parentPath);
1105
- break;
1106
-
1107
- case "cut":
1108
- // For array cut, remove element from shadow array
1109
- const arrayPath = path.slice(0, -1);
1110
- const index = parseInt(path[path.length - 1]!);
1111
- store.removeShadowArrayElement(thisKey, arrayPath, index);
1112
- break;
1113
- }
1114
- };
1115
-
1116
- shadowUpdate();
1117
- console.log("shadowState", getGlobalStore.getState().shadowStateStore);
1090
+ console.log("shadowState", store.shadowStateStore);
1118
1091
  if (
1119
1092
  updateObj.updateType === "update" &&
1120
1093
  (validationKey || latestInitialOptionsRef.current?.validation?.key) &&
@@ -1164,7 +1137,7 @@ export function useCogsStateFn<TStateObject extends unknown>(
1164
1137
  });
1165
1138
  }
1166
1139
 
1167
- const stateEntry = getGlobalStore.getState().stateComponents.get(thisKey);
1140
+ const stateEntry = store.stateComponents.get(thisKey);
1168
1141
  console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", stateEntry);
1169
1142
  if (stateEntry) {
1170
1143
  const changedPaths = getDifferences(prevValue, payload);
@@ -1282,6 +1255,26 @@ export function useCogsStateFn<TStateObject extends unknown>(
1282
1255
  newValue,
1283
1256
  } satisfies UpdateTypeDetail;
1284
1257
 
1258
+ switch (updateObj.updateType) {
1259
+ case "update":
1260
+ // For updates, just mirror the structure at the path
1261
+ store.updateShadowAtPath(thisKey, path, payload);
1262
+ break;
1263
+
1264
+ case "insert":
1265
+ // For array insert, add empty element to shadow array
1266
+ const parentPath = path.slice(0, -1);
1267
+ store.insertShadowArrayElement(thisKey, parentPath);
1268
+ break;
1269
+
1270
+ case "cut":
1271
+ // For array cut, remove element from shadow array
1272
+ const arrayPath = path.slice(0, -1);
1273
+ const index = parseInt(path[path.length - 1]!);
1274
+ store.removeShadowArrayElement(thisKey, arrayPath, index);
1275
+ break;
1276
+ }
1277
+
1285
1278
  setStateLog(thisKey, (prevLogs) => {
1286
1279
  const logs = [...(prevLogs ?? []), newUpdate];
1287
1280
 
@@ -1322,7 +1315,7 @@ export function useCogsStateFn<TStateObject extends unknown>(
1322
1315
  });
1323
1316
  }
1324
1317
  if (latestInitialOptionsRef.current?.serverSync) {
1325
- const serverStateStore = getGlobalStore.getState().serverState[thisKey];
1318
+ const serverStateStore = store.serverState[thisKey];
1326
1319
  const serverSync = latestInitialOptionsRef.current?.serverSync;
1327
1320
  setServerSyncActions(thisKey, {
1328
1321
  syncKey: