cogsbox-state 0.5.467 → 0.5.469

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/src/CogsState.tsx CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  import {
4
4
  createElement,
5
- memo,
6
5
  startTransition,
7
6
  useCallback,
8
7
  useEffect,
@@ -23,6 +22,7 @@ import {
23
22
  } from './utility.js';
24
23
  import {
25
24
  FormElementWrapper,
25
+ IsolatedComponentWrapper,
26
26
  MemoizedCogsItemWrapper,
27
27
  ValidationWrapper,
28
28
  } from './Components.js';
@@ -34,6 +34,7 @@ import {
34
34
  formRefStore,
35
35
  getGlobalStore,
36
36
  ValidationError,
37
+ ValidationSeverity,
37
38
  ValidationStatus,
38
39
  type ComponentsType,
39
40
  } from './store.js';
@@ -55,15 +56,7 @@ export type VirtualViewOptions = {
55
56
 
56
57
  // The result now returns a real StateObject
57
58
  export type VirtualStateObjectResult<T extends any[]> = {
58
- /**
59
- * A new, fully-functional StateObject that represents the virtualized slice.
60
- * You can use `.get()`, `.stateMap()`, `.insert()`, `.cut()` etc. on this object.
61
- */
62
-
63
59
  virtualState: StateObject<T>;
64
- /**
65
- * Props to be spread onto your DOM elements to enable virtualization.
66
- */
67
60
  virtualizerProps: {
68
61
  outer: { ref: RefObject<HTMLDivElement>; style: CSSProperties };
69
62
  inner: { style: CSSProperties };
@@ -83,7 +76,9 @@ export type FormElementParams<T> = StateObject<T> & {
83
76
  ref?: React.RefObject<any>;
84
77
  value?: T extends boolean ? never : T;
85
78
  onChange?: (
86
- event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
79
+ event: React.ChangeEvent<
80
+ HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement
81
+ >
87
82
  ) => void;
88
83
  onBlur?: () => void;
89
84
  };
@@ -149,78 +144,75 @@ export type StreamHandle<T> = {
149
144
  pause: () => void;
150
145
  resume: () => void;
151
146
  };
147
+
152
148
  export type ArrayEndType<TShape extends unknown> = {
153
- stream: <T = Prettify<InferArrayElement<TShape>>, R = T>(
149
+ $stream: <T = Prettify<InferArrayElement<TShape>>, R = T>(
154
150
  options?: StreamOptions<T, R>
155
151
  ) => StreamHandle<T>;
156
- findWith: findWithFuncType<Prettify<InferArrayElement<TShape>>>;
157
- index: (index: number) => StateObject<Prettify<InferArrayElement<TShape>>> & {
158
- insert: InsertTypeObj<Prettify<InferArrayElement<TShape>>>;
159
- cut: CutFunctionType<TShape>;
160
- _index: number;
152
+ $findWith: findWithFuncType<Prettify<InferArrayElement<TShape>>>;
153
+ $index: (index: number) => StateObject<
154
+ Prettify<InferArrayElement<TShape>>
155
+ > & {
156
+ $insert: InsertTypeObj<Prettify<InferArrayElement<TShape>>>;
157
+ $cut: CutFunctionType<TShape>;
158
+ $_index: number;
161
159
  } & EndType<Prettify<InferArrayElement<TShape>>>;
162
- insert: InsertType<Prettify<InferArrayElement<TShape>>>;
163
- cut: CutFunctionType<TShape>;
164
- cutSelected: () => void;
165
- cutByValue: (value: string | number | boolean) => void;
166
- toggleByValue: (value: string | number | boolean) => void;
167
- stateSort: (
160
+ $insert: InsertType<Prettify<InferArrayElement<TShape>>>;
161
+ $cut: CutFunctionType<TShape>;
162
+ $cutSelected: () => void;
163
+ $cutByValue: (value: string | number | boolean) => void;
164
+ $toggleByValue: (value: string | number | boolean) => void;
165
+ $stateSort: (
168
166
  compareFn: (
169
167
  a: Prettify<InferArrayElement<TShape>>,
170
168
  b: Prettify<InferArrayElement<TShape>>
171
169
  ) => number
172
170
  ) => ArrayEndType<TShape>;
173
- useVirtualView: (
171
+ $useVirtualView: (
174
172
  options: VirtualViewOptions
175
173
  ) => VirtualStateObjectResult<Prettify<InferArrayElement<TShape>>[]>;
176
174
 
177
- stateList: (
175
+ $stateList: (
178
176
  callbackfn: (
179
177
  setter: StateObject<Prettify<InferArrayElement<TShape>>>,
180
178
  index: number,
181
179
  arraySetter: StateObject<TShape>
182
180
  ) => void
183
181
  ) => any;
184
- stateMap: <U>(
182
+ $stateMap: <U>(
185
183
  callbackfn: (
186
184
  setter: StateObject<Prettify<InferArrayElement<TShape>>>,
187
185
  index: number,
188
186
  arraySetter: StateObject<TShape>
189
187
  ) => U
190
188
  ) => U[];
191
- $stateMap: (
192
- callbackfn: (
193
- setter: StateObject<Prettify<InferArrayElement<TShape>>>,
194
- index: number,
195
- arraySetter: StateObject<TShape>
196
- ) => void
197
- ) => any;
198
- stateFlattenOn: <K extends keyof Prettify<InferArrayElement<TShape>>>(
189
+
190
+ $stateFlattenOn: <K extends keyof Prettify<InferArrayElement<TShape>>>(
199
191
  field: K
200
192
  ) => StateObject<InferArrayElement<Prettify<InferArrayElement<TShape>>[K]>[]>;
201
- uniqueInsert: (
193
+ $uniqueInsert: (
202
194
  payload: InsertParams<Prettify<InferArrayElement<TShape>>>,
203
195
  fields?: (keyof Prettify<InferArrayElement<TShape>>)[],
204
196
  onMatch?: (existingItem: any) => any
205
197
  ) => void;
206
- stateFind: (
198
+ $stateFind: (
207
199
  callbackfn: (
208
200
  value: Prettify<InferArrayElement<TShape>>,
209
201
  index: number
210
202
  ) => boolean
211
203
  ) => StateObject<Prettify<InferArrayElement<TShape>>> | undefined;
212
- stateFilter: (
204
+ $stateFilter: (
213
205
  callbackfn: (
214
206
  value: Prettify<InferArrayElement<TShape>>,
215
207
  index: number
216
208
  ) => void
217
209
  ) => ArrayEndType<TShape>;
218
- getSelected: () =>
210
+ $getSelected: () =>
219
211
  | StateObject<Prettify<InferArrayElement<TShape>>>
220
212
  | undefined;
221
- clearSelected: () => void;
222
- getSelectedIndex: () => number;
223
- last: () => StateObject<Prettify<InferArrayElement<TShape>>> | undefined;
213
+ $clearSelected: () => void;
214
+ $getSelectedIndex: () => number;
215
+ $last: () => StateObject<Prettify<InferArrayElement<TShape>>> | undefined;
224
216
  } & EndType<TShape>;
225
217
 
226
218
  export type FormOptsType = {
@@ -251,40 +243,40 @@ export type InsertTypeObj<T> = (payload: InsertParams<T>) => void;
251
243
 
252
244
  type EffectFunction<T, R> = (state: T, deps: any[]) => R;
253
245
  export type EndType<T, IsArrayElement = false> = {
254
- addZodValidation: (errors: ValidationError[]) => void;
255
- clearZodValidation: (paths?: string[]) => void;
256
- applyJsonPatch: (patches: any[]) => void;
257
- update: UpdateType<T>;
258
- _path: string[];
259
- _stateKey: string;
260
- formElement: (control: FormControl<T>, opts?: FormOptsType) => JSX.Element;
261
- get: () => T;
262
-
246
+ $addZodValidation: (errors: ValidationError[]) => void;
247
+ $clearZodValidation: (paths?: string[]) => void;
248
+ $applyJsonPatch: (patches: any[]) => void;
249
+ $update: UpdateType<T>;
250
+ $_path: string[];
251
+ $_stateKey: string;
252
+ $isolate: (
253
+ renderFn: (state: StateObject<T>) => React.ReactNode
254
+ ) => JSX.Element;
255
+ $formElement: (control: FormControl<T>, opts?: FormOptsType) => JSX.Element;
263
256
  $get: () => T;
264
- $derive: <R>(fn: EffectFunction<T, R>) => R;
265
-
266
- _status: 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
267
- getStatus: () => 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
268
-
269
- showValidationErrors: () => string[];
270
- setValidation: (ctx: string) => void;
271
- removeValidation: (ctx: string) => void;
272
- ignoreFields: (fields: string[]) => StateObject<T>;
273
- isSelected: boolean;
274
- setSelected: (value: boolean) => void;
275
- toggleSelected: () => void;
276
- getFormRef: () => React.RefObject<any> | undefined;
277
- removeStorage: () => void;
278
- sync: () => void;
279
- validationWrapper: ({
257
+ $$get: () => T;
258
+ $$derive: <R>(fn: EffectFunction<T, R>) => R;
259
+ $_status: 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
260
+ $getStatus: () => 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
261
+ $showValidationErrors: () => string[];
262
+ $setValidation: (ctx: string) => void;
263
+ $removeValidation: (ctx: string) => void;
264
+ $ignoreFields: (fields: string[]) => StateObject<T>;
265
+ $isSelected: boolean;
266
+ $setSelected: (value: boolean) => void;
267
+ $toggleSelected: () => void;
268
+ $getFormRef: () => React.RefObject<any> | undefined;
269
+ $removeStorage: () => void;
270
+ $sync: () => void;
271
+ $validationWrapper: ({
280
272
  children,
281
273
  hideMessage,
282
274
  }: {
283
275
  children: React.ReactNode;
284
276
  hideMessage?: boolean;
285
277
  }) => JSX.Element;
286
- lastSynced?: SyncInfo;
287
- } & (IsArrayElement extends true ? { cutThis: () => void } : {});
278
+ $lastSynced?: SyncInfo;
279
+ } & (IsArrayElement extends true ? { $cutThis: () => void } : {});
288
280
 
289
281
  export type StateObject<T> = (T extends any[]
290
282
  ? ArrayEndType<T>
@@ -294,20 +286,20 @@ export type StateObject<T> = (T extends any[]
294
286
  ? EndType<T, true>
295
287
  : never) &
296
288
  EndType<T, true> & {
297
- toggle: T extends boolean ? () => void : never;
298
- getAllFormRefs: () => Map<string, React.RefObject<any>>;
299
- _componentId: string | null;
300
- getComponents: () => ComponentsType;
289
+ $toggle: T extends boolean ? () => void : never;
290
+ $getAllFormRefs: () => Map<string, React.RefObject<any>>;
291
+ $_componentId: string | null;
292
+ $getComponents: () => ComponentsType;
301
293
 
302
- _initialState: T;
303
- updateInitialState: (newState: T | null) => {
294
+ $_initialState: T;
295
+ $updateInitialState: (newState: T | null) => {
304
296
  fetchId: (field: keyof T) => string | number;
305
297
  };
306
- _isLoading: boolean;
307
- _serverState: T;
308
- revertToInitialState: (obj?: { validationKey?: string }) => T;
298
+ $_isLoading: boolean;
299
+ $_serverState: T;
300
+ $revertToInitialState: (obj?: { validationKey?: string }) => T;
309
301
 
310
- middleware: (
302
+ $middleware: (
311
303
  middles: ({
312
304
  updateLog,
313
305
  update,
@@ -317,7 +309,7 @@ export type StateObject<T> = (T extends any[]
317
309
  }) => void
318
310
  ) => void;
319
311
 
320
- getLocalStorage: (key: string) => LocalStorageData<T> | null;
312
+ $getLocalStorage: (key: string) => LocalStorageData<T> | null;
321
313
  };
322
314
 
323
315
  export type CogsUpdate<T extends unknown> = UpdateType<T>;
@@ -448,8 +440,8 @@ export type SyncRenderOptions<T extends unknown = unknown> = {
448
440
  type FormsElementsType<T> = {
449
441
  validation?: (options: {
450
442
  children: React.ReactNode;
451
- status: ValidationStatus; // Instead of 'active' boolean
452
-
443
+ status: ValidationStatus;
444
+ severity: ValidationSeverity;
453
445
  hasErrors: boolean;
454
446
  hasWarnings: boolean;
455
447
  allErrors: ValidationError[];
@@ -483,7 +475,6 @@ export type TransformedStateType<T> = {
483
475
  const {
484
476
  getInitialOptions,
485
477
  updateInitialStateGlobal,
486
- // ALIAS THE NEW FUNCTIONS TO THE OLD NAMES
487
478
  getShadowMetadata,
488
479
  setShadowMetadata,
489
480
  getShadowValue,
@@ -492,12 +483,9 @@ const {
492
483
  insertShadowArrayElement,
493
484
  insertManyShadowArrayElements,
494
485
  removeShadowArrayElement,
495
- getSelectedIndex,
496
486
  setInitialStateOptions,
497
487
  setServerStateUpdate,
498
488
  markAsDirty,
499
- registerComponent,
500
- unregisterComponent,
501
489
  addPathComponent,
502
490
  clearSelectedIndexesForState,
503
491
  addStateLog,
@@ -505,7 +493,6 @@ const {
505
493
  clearSelectedIndex,
506
494
  getSyncInfo,
507
495
  notifyPathSubscribers,
508
- subscribeToPath,
509
496
  // Note: The old functions are no longer imported under their original names
510
497
  } = getGlobalStore.getState();
511
498
  function getArrayData(stateKey: string, path: string[], meta?: MetaData) {
@@ -1821,64 +1808,12 @@ function getScopedData(stateKey: string, path: string[], meta?: MetaData) {
1821
1808
  function createProxyHandler<T>(
1822
1809
  stateKey: string,
1823
1810
  effectiveSetState: EffectiveSetState<T>,
1824
- componentId: string,
1811
+ outerComponentId: string,
1825
1812
  sessionId?: string
1826
1813
  ): StateObject<T> {
1827
1814
  const proxyCache = new Map<string, any>();
1828
1815
  let stateVersion = 0;
1829
1816
 
1830
- const methodNames = new Set([
1831
- 'sync',
1832
- 'getStatus',
1833
- 'removeStorage',
1834
- 'showValidationErrors',
1835
- 'getSelected',
1836
- 'getSelectedIndex',
1837
- 'clearSelected',
1838
- 'useVirtualView',
1839
- 'stateMap',
1840
- '$stateMap',
1841
- 'stateFind',
1842
- 'stateFilter',
1843
- 'stateSort',
1844
- 'stream',
1845
- 'stateList',
1846
- 'stateFlattenOn',
1847
- 'index',
1848
- 'last',
1849
- 'insert',
1850
- 'uniqueInsert',
1851
- 'cut',
1852
- 'cutSelected',
1853
- 'cutByValue',
1854
- 'toggleByValue',
1855
- 'findWith',
1856
- 'cutThis',
1857
- 'get',
1858
- 'getState',
1859
- '$derive',
1860
- '$get',
1861
- 'lastSynced',
1862
- 'getLocalStorage',
1863
- 'isSelected',
1864
- 'setSelected',
1865
- 'toggleSelected',
1866
- '_componentId',
1867
- 'addZodValidation',
1868
- 'clearZodValidation',
1869
- 'applyJsonPatch',
1870
- 'getComponents',
1871
- 'getAllFormRefs',
1872
- 'getFormRef',
1873
- 'validationWrapper',
1874
- '_stateKey',
1875
- '_path',
1876
- 'update',
1877
- 'toggle',
1878
- 'formElement',
1879
- // Add ANY other method names here
1880
- ]);
1881
-
1882
1817
  function rebuildStateShape({
1883
1818
  path = [],
1884
1819
  meta,
@@ -1891,7 +1826,8 @@ function createProxyHandler<T>(
1891
1826
  const derivationSignature = meta
1892
1827
  ? JSON.stringify(meta.arrayViews || meta.transforms)
1893
1828
  : '';
1894
- const cacheKey = path.join('.') + ':' + derivationSignature;
1829
+ const cacheKey =
1830
+ path.join('.') + ':' + componentId + ':' + derivationSignature;
1895
1831
  if (proxyCache.has(cacheKey)) {
1896
1832
  return proxyCache.get(cacheKey);
1897
1833
  }
@@ -1905,7 +1841,7 @@ function createProxyHandler<T>(
1905
1841
  if (path.length === 0 && prop in rootLevelMethods) {
1906
1842
  return rootLevelMethods[prop as keyof typeof rootLevelMethods];
1907
1843
  }
1908
- if (!methodNames.has(prop)) {
1844
+ if (!prop.startsWith('$')) {
1909
1845
  const nextPath = [...path, prop];
1910
1846
  return rebuildStateShape({
1911
1847
  path: nextPath,
@@ -1913,11 +1849,11 @@ function createProxyHandler<T>(
1913
1849
  meta,
1914
1850
  });
1915
1851
  }
1916
- if (prop === '_rebuildStateShape') {
1852
+ if (prop === '$_rebuildStateShape') {
1917
1853
  return rebuildStateShape;
1918
1854
  }
1919
1855
 
1920
- if (prop === 'sync' && path.length === 0) {
1856
+ if (prop === '$sync' && path.length === 0) {
1921
1857
  return async function () {
1922
1858
  const options = getGlobalStore
1923
1859
  .getState()
@@ -1979,7 +1915,7 @@ function createProxyHandler<T>(
1979
1915
  };
1980
1916
  }
1981
1917
  // Fixed getStatus function in createProxyHandler
1982
- if (prop === '_status' || prop === 'getStatus') {
1918
+ if (prop === '$_status' || prop === '$getStatus') {
1983
1919
  const getStatusFunc = () => {
1984
1920
  // ✅ Use the optimized helper to get all data in one efficient call
1985
1921
  const { shadowMeta, value } = getScopedData(stateKey, path, meta);
@@ -2021,9 +1957,9 @@ function createProxyHandler<T>(
2021
1957
  };
2022
1958
 
2023
1959
  // This part remains the same
2024
- return prop === '_status' ? getStatusFunc() : getStatusFunc;
1960
+ return prop === '$_status' ? getStatusFunc() : getStatusFunc;
2025
1961
  }
2026
- if (prop === 'removeStorage') {
1962
+ if (prop === '$removeStorage') {
2027
1963
  return () => {
2028
1964
  const initialState =
2029
1965
  getGlobalStore.getState().initialStateGlobal[stateKey];
@@ -2035,7 +1971,7 @@ function createProxyHandler<T>(
2035
1971
  if (storageKey) localStorage.removeItem(storageKey);
2036
1972
  };
2037
1973
  }
2038
- if (prop === 'showValidationErrors') {
1974
+ if (prop === '$showValidationErrors') {
2039
1975
  return () => {
2040
1976
  const { shadowMeta } = getScopedData(stateKey, path, meta);
2041
1977
  if (
@@ -2051,7 +1987,7 @@ function createProxyHandler<T>(
2051
1987
  };
2052
1988
  }
2053
1989
 
2054
- if (prop === 'getSelected') {
1990
+ if (prop === '$getSelected') {
2055
1991
  return () => {
2056
1992
  const arrayKey = [stateKey, ...path].join('.');
2057
1993
  registerComponentDependency(stateKey, componentId, [
@@ -2090,7 +2026,7 @@ function createProxyHandler<T>(
2090
2026
  });
2091
2027
  };
2092
2028
  }
2093
- if (prop === 'getSelectedIndex') {
2029
+ if (prop === '$getSelectedIndex') {
2094
2030
  return () => {
2095
2031
  // Key for the array in the global selection map (e.g., "myState.products")
2096
2032
  const arrayKey = stateKey + '.' + path.join('.');
@@ -2120,7 +2056,7 @@ function createProxyHandler<T>(
2120
2056
  return (viewIds as string[]).indexOf(selectedId as string);
2121
2057
  };
2122
2058
  }
2123
- if (prop === 'clearSelected') {
2059
+ if (prop === '$clearSelected') {
2124
2060
  notifySelectionComponents(stateKey, path);
2125
2061
  return () => {
2126
2062
  clearSelectedIndex({
@@ -2129,7 +2065,7 @@ function createProxyHandler<T>(
2129
2065
  };
2130
2066
  }
2131
2067
 
2132
- if (prop === 'useVirtualView') {
2068
+ if (prop === '$useVirtualView') {
2133
2069
  return (
2134
2070
  options: VirtualViewOptions
2135
2071
  ): VirtualStateObjectResult<any[]> => {
@@ -2479,7 +2415,7 @@ function createProxyHandler<T>(
2479
2415
  };
2480
2416
  };
2481
2417
  }
2482
- if (prop === 'stateMap') {
2418
+ if (prop === '$stateMap') {
2483
2419
  return (
2484
2420
  callbackfn: (setter: any, index: number, arraySetter: any) => void
2485
2421
  ) => {
@@ -2519,7 +2455,7 @@ function createProxyHandler<T>(
2519
2455
  };
2520
2456
  }
2521
2457
 
2522
- if (prop === 'stateFilter') {
2458
+ if (prop === '$stateFilter') {
2523
2459
  return (callbackfn: (value: any, index: number) => boolean) => {
2524
2460
  const arrayPathKey = path.length > 0 ? path.join('.') : 'root';
2525
2461
 
@@ -2564,7 +2500,7 @@ function createProxyHandler<T>(
2564
2500
  });
2565
2501
  };
2566
2502
  }
2567
- if (prop === 'stateSort') {
2503
+ if (prop === '$stateSort') {
2568
2504
  return (compareFn: (a: any, b: any) => number) => {
2569
2505
  const arrayPathKey = path.length > 0 ? path.join('.') : 'root';
2570
2506
 
@@ -2605,7 +2541,7 @@ function createProxyHandler<T>(
2605
2541
  };
2606
2542
  }
2607
2543
  // In createProxyHandler, inside the get trap where you have other array methods:
2608
- if (prop === 'stream') {
2544
+ if (prop === '$stream') {
2609
2545
  return function <U = InferArrayElement<T>, R = U>(
2610
2546
  options: StreamOptions<U, R> = {}
2611
2547
  ): StreamHandle<U> {
@@ -2707,7 +2643,7 @@ function createProxyHandler<T>(
2707
2643
  };
2708
2644
  }
2709
2645
 
2710
- if (prop === 'stateList') {
2646
+ if (prop === '$stateList') {
2711
2647
  return (
2712
2648
  callbackfn: (
2713
2649
  setter: any,
@@ -2828,7 +2764,7 @@ function createProxyHandler<T>(
2828
2764
  return <StateListWrapper />;
2829
2765
  };
2830
2766
  }
2831
- if (prop === 'stateFlattenOn') {
2767
+ if (prop === '$stateFlattenOn') {
2832
2768
  return (fieldName: string) => {
2833
2769
  // FIX: Get the definitive list of IDs for the current view from meta.arrayViews.
2834
2770
  const arrayPathKey = path.length > 0 ? path.join('.') : 'root';
@@ -2849,7 +2785,7 @@ function createProxyHandler<T>(
2849
2785
  });
2850
2786
  };
2851
2787
  }
2852
- if (prop === 'index') {
2788
+ if (prop === '$index') {
2853
2789
  return (index: number) => {
2854
2790
  const arrayPathKey = path.length > 0 ? path.join('.') : 'root';
2855
2791
  const viewIds = meta?.arrayViews?.[arrayPathKey];
@@ -2878,7 +2814,7 @@ function createProxyHandler<T>(
2878
2814
  });
2879
2815
  };
2880
2816
  }
2881
- if (prop === 'last') {
2817
+ if (prop === '$last') {
2882
2818
  return () => {
2883
2819
  const { keys: currentViewIds } = getArrayData(stateKey, path, meta);
2884
2820
  if (!currentViewIds || currentViewIds.length === 0) {
@@ -2898,7 +2834,7 @@ function createProxyHandler<T>(
2898
2834
  });
2899
2835
  };
2900
2836
  }
2901
- if (prop === 'insert') {
2837
+ if (prop === '$insert') {
2902
2838
  return (
2903
2839
  payload: InsertParams<InferArrayElement<T>>,
2904
2840
  index?: number
@@ -2906,7 +2842,7 @@ function createProxyHandler<T>(
2906
2842
  effectiveSetState(payload as any, path, { updateType: 'insert' });
2907
2843
  };
2908
2844
  }
2909
- if (prop === 'uniqueInsert') {
2845
+ if (prop === '$uniqueInsert') {
2910
2846
  return (
2911
2847
  payload: UpdateArg<T>,
2912
2848
  fields?: (keyof InferArrayElement<T>)[],
@@ -2948,7 +2884,7 @@ function createProxyHandler<T>(
2948
2884
  }
2949
2885
  };
2950
2886
  }
2951
- if (prop === 'cut') {
2887
+ if (prop === '$cut') {
2952
2888
  return (index?: number, options?: { waitForSync?: boolean }) => {
2953
2889
  const shadowMeta = getShadowMetadata(stateKey, path);
2954
2890
  if (!shadowMeta?.arrayKeys || shadowMeta.arrayKeys.length === 0)
@@ -2969,7 +2905,7 @@ function createProxyHandler<T>(
2969
2905
  });
2970
2906
  };
2971
2907
  }
2972
- if (prop === 'cutSelected') {
2908
+ if (prop === '$cutSelected') {
2973
2909
  return () => {
2974
2910
  const arrayKey = [stateKey, ...path].join('.');
2975
2911
 
@@ -3000,7 +2936,7 @@ function createProxyHandler<T>(
3000
2936
  });
3001
2937
  };
3002
2938
  }
3003
- if (prop === 'cutByValue') {
2939
+ if (prop === '$cutByValue') {
3004
2940
  return (value: string | number | boolean) => {
3005
2941
  const {
3006
2942
  isArray,
@@ -3019,7 +2955,7 @@ function createProxyHandler<T>(
3019
2955
  };
3020
2956
  }
3021
2957
 
3022
- if (prop === 'toggleByValue') {
2958
+ if (prop === '$toggleByValue') {
3023
2959
  return (value: string | number | boolean) => {
3024
2960
  const {
3025
2961
  isArray,
@@ -3042,7 +2978,7 @@ function createProxyHandler<T>(
3042
2978
  }
3043
2979
  };
3044
2980
  }
3045
- if (prop === 'findWith') {
2981
+ if (prop === '$findWith') {
3046
2982
  return (searchKey: string, searchValue: any) => {
3047
2983
  const { isArray, value, keys } = getArrayData(stateKey, path, meta);
3048
2984
 
@@ -3071,7 +3007,7 @@ function createProxyHandler<T>(
3071
3007
  });
3072
3008
  };
3073
3009
  }
3074
- if (prop === 'cutThis') {
3010
+ if (prop === '$cutThis') {
3075
3011
  const { value: shadowValue } = getScopedData(stateKey, path, meta);
3076
3012
  const parentPath = path.slice(0, -1);
3077
3013
  notifySelectionComponents(stateKey, parentPath);
@@ -3080,7 +3016,7 @@ function createProxyHandler<T>(
3080
3016
  };
3081
3017
  }
3082
3018
 
3083
- if (prop === 'get') {
3019
+ if (prop === '$get') {
3084
3020
  return () => {
3085
3021
  registerComponentDependency(stateKey, componentId, path);
3086
3022
  const { value } = getScopedData(stateKey, path, meta);
@@ -3088,7 +3024,7 @@ function createProxyHandler<T>(
3088
3024
  };
3089
3025
  }
3090
3026
 
3091
- if (prop === '$derive') {
3027
+ if (prop === '$$derive') {
3092
3028
  return (fn: any) =>
3093
3029
  $cogsSignal({
3094
3030
  _stateKey: stateKey,
@@ -3098,11 +3034,11 @@ function createProxyHandler<T>(
3098
3034
  });
3099
3035
  }
3100
3036
 
3101
- if (prop === '$get') {
3037
+ if (prop === '$$get') {
3102
3038
  return () =>
3103
3039
  $cogsSignal({ _stateKey: stateKey, _path: path, _meta: meta });
3104
3040
  }
3105
- if (prop === 'lastSynced') {
3041
+ if (prop === '$lastSynced') {
3106
3042
  const syncKey = `${stateKey}:${path.join('.')}`;
3107
3043
  return getSyncInfo(syncKey);
3108
3044
  }
@@ -3110,7 +3046,7 @@ function createProxyHandler<T>(
3110
3046
  return (key: string) =>
3111
3047
  loadFromLocalStorage(sessionId + '-' + stateKey + '-' + key);
3112
3048
  }
3113
- if (prop === 'isSelected') {
3049
+ if (prop === '$isSelected') {
3114
3050
  const parentPathArray = path.slice(0, -1);
3115
3051
  const parentMeta = getShadowMetadata(stateKey, parentPathArray);
3116
3052
 
@@ -3127,7 +3063,7 @@ function createProxyHandler<T>(
3127
3063
  return undefined;
3128
3064
  }
3129
3065
 
3130
- if (prop === 'setSelected') {
3066
+ if (prop === '$setSelected') {
3131
3067
  return (value: boolean) => {
3132
3068
  const parentPath = path.slice(0, -1);
3133
3069
  const fullParentKey = stateKey + '.' + parentPath.join('.');
@@ -3147,7 +3083,7 @@ function createProxyHandler<T>(
3147
3083
  };
3148
3084
  }
3149
3085
 
3150
- if (prop === 'toggleSelected') {
3086
+ if (prop === '$toggleSelected') {
3151
3087
  return () => {
3152
3088
  const parentPath = path.slice(0, -1);
3153
3089
  const fullParentKey = stateKey + '.' + parentPath.join('.');
@@ -3169,11 +3105,11 @@ function createProxyHandler<T>(
3169
3105
  notifySelectionComponents(stateKey, parentPath);
3170
3106
  };
3171
3107
  }
3172
- if (prop === '_componentId') {
3108
+ if (prop === '$_componentId') {
3173
3109
  return componentId;
3174
3110
  }
3175
3111
  if (path.length == 0) {
3176
- if (prop === 'addZodValidation') {
3112
+ if (prop === '$addZodValidation') {
3177
3113
  return (zodErrors: any[]) => {
3178
3114
  zodErrors.forEach((error) => {
3179
3115
  const currentMeta =
@@ -3202,7 +3138,7 @@ function createProxyHandler<T>(
3202
3138
  });
3203
3139
  };
3204
3140
  }
3205
- if (prop === 'clearZodValidation') {
3141
+ if (prop === '$clearZodValidation') {
3206
3142
  return (path?: string[]) => {
3207
3143
  if (!path) {
3208
3144
  throw new Error('clearZodValidation requires a path');
@@ -3220,7 +3156,7 @@ function createProxyHandler<T>(
3220
3156
  });
3221
3157
  };
3222
3158
  }
3223
- if (prop === 'applyJsonPatch') {
3159
+ if (prop === '$applyJsonPatch') {
3224
3160
  return (patches: Operation[]) => {
3225
3161
  const store = getGlobalStore.getState();
3226
3162
  const rootMeta = store.getShadowMetadata(stateKey, []);
@@ -3312,17 +3248,17 @@ function createProxyHandler<T>(
3312
3248
  };
3313
3249
  }
3314
3250
 
3315
- if (prop === 'getComponents')
3251
+ if (prop === '$getComponents')
3316
3252
  return () => getShadowMetadata(stateKey, [])?.components;
3317
- if (prop === 'getAllFormRefs')
3253
+ if (prop === '$getAllFormRefs')
3318
3254
  return () =>
3319
3255
  formRefStore.getState().getFormRefsByStateKey(stateKey);
3320
3256
  }
3321
- if (prop === 'getFormRef') {
3257
+ if (prop === '$getFormRef') {
3322
3258
  return () =>
3323
3259
  formRefStore.getState().getFormRef(stateKey + '.' + path.join('.'));
3324
3260
  }
3325
- if (prop === 'validationWrapper') {
3261
+ if (prop === '$validationWrapper') {
3326
3262
  return ({
3327
3263
  children,
3328
3264
  hideMessage,
@@ -3341,9 +3277,9 @@ function createProxyHandler<T>(
3341
3277
  </ValidationWrapper>
3342
3278
  );
3343
3279
  }
3344
- if (prop === '_stateKey') return stateKey;
3345
- if (prop === '_path') return path;
3346
- if (prop === 'update') {
3280
+ if (prop === '$_stateKey') return stateKey;
3281
+ if (prop === '$_path') return path;
3282
+ if (prop === '$update') {
3347
3283
  return (payload: UpdateArg<T>) => {
3348
3284
  effectiveSetState(payload as any, path, { updateType: 'update' });
3349
3285
 
@@ -3371,7 +3307,7 @@ function createProxyHandler<T>(
3371
3307
  };
3372
3308
  };
3373
3309
  }
3374
- if (prop === 'toggle') {
3310
+ if (prop === '$toggle') {
3375
3311
  const { value: currentValueAtPath } = getScopedData(
3376
3312
  stateKey,
3377
3313
  path,
@@ -3387,7 +3323,19 @@ function createProxyHandler<T>(
3387
3323
  });
3388
3324
  };
3389
3325
  }
3390
- if (prop === 'formElement') {
3326
+ if (prop === '$isolate') {
3327
+ return (renderFn: (state: any) => React.ReactNode) => {
3328
+ return (
3329
+ <IsolatedComponentWrapper
3330
+ stateKey={stateKey}
3331
+ path={path}
3332
+ rebuildStateShape={rebuildStateShape}
3333
+ renderFn={renderFn}
3334
+ />
3335
+ );
3336
+ };
3337
+ }
3338
+ if (prop === '$formElement') {
3391
3339
  return (child: FormControl<T>, formOpts?: FormOptsType) => {
3392
3340
  return (
3393
3341
  <FormElementWrapper
@@ -3402,9 +3350,6 @@ function createProxyHandler<T>(
3402
3350
  };
3403
3351
  }
3404
3352
  const nextPath = [...path, prop];
3405
- const nextValue = getGlobalStore
3406
- .getState()
3407
- .getShadowValue(stateKey, nextPath);
3408
3353
  return rebuildStateShape({
3409
3354
  path: nextPath,
3410
3355
  componentId: componentId!,
@@ -3420,59 +3365,55 @@ function createProxyHandler<T>(
3420
3365
  }
3421
3366
 
3422
3367
  const rootLevelMethods = {
3423
- revertToInitialState: (obj?: { validationKey?: string }) => {
3368
+ $revertToInitialState: (obj?: { validationKey?: string }) => {
3424
3369
  const shadowMeta = getGlobalStore
3425
3370
  .getState()
3426
3371
  .getShadowMetadata(stateKey, []);
3427
3372
  let revertState;
3428
3373
 
3374
+ // Determine the correct state to revert to (same logic as before)
3429
3375
  if (shadowMeta?.stateSource === 'server' && shadowMeta.baseServerState) {
3430
- // Revert to last known server state
3431
3376
  revertState = shadowMeta.baseServerState;
3432
3377
  } else {
3433
- // Revert to initial/default state
3434
3378
  revertState = getGlobalStore.getState().initialStateGlobal[stateKey];
3435
3379
  }
3436
- const initialState =
3437
- getGlobalStore.getState().initialStateGlobal[stateKey];
3438
3380
 
3381
+ // Perform necessary cleanup
3439
3382
  clearSelectedIndexesForState(stateKey);
3440
3383
 
3441
- stateVersion++;
3442
- initializeShadowState(stateKey, initialState);
3384
+ // FIX 1: Use the IMMEDIATE, SYNCHRONOUS state reset function.
3385
+ // This is what your tests expect for a clean slate.
3386
+ initializeShadowState(stateKey, revertState);
3387
+
3388
+ // Rebuild the proxy's internal shape after the reset
3443
3389
  rebuildStateShape({
3444
3390
  path: [],
3445
- componentId: componentId!,
3391
+ componentId: outerComponentId!,
3446
3392
  });
3393
+
3394
+ // Handle localStorage side-effects
3447
3395
  const initalOptionsGet = getInitialOptions(stateKey as string);
3448
3396
  const localKey = isFunction(initalOptionsGet?.localStorage?.key)
3449
- ? initalOptionsGet?.localStorage?.key(initialState)
3397
+ ? initalOptionsGet?.localStorage?.key(revertState)
3450
3398
  : initalOptionsGet?.localStorage?.key;
3451
-
3452
3399
  const storageKey = `${sessionId}-${stateKey}-${localKey}`;
3453
-
3454
3400
  if (storageKey) {
3455
3401
  localStorage.removeItem(storageKey);
3456
3402
  }
3457
3403
 
3458
- const stateEntry = getGlobalStore
3459
- .getState()
3460
- .getShadowMetadata(stateKey, []);
3461
- if (stateEntry) {
3462
- stateEntry?.components?.forEach((component) => {
3463
- component.forceUpdate();
3464
- });
3465
- }
3404
+ // FIX 2: Use the library's BATCHED notification system instead of a manual forceUpdate loop.
3405
+ // This fixes the original infinite loop bug safely.
3406
+ notifyComponents(stateKey);
3466
3407
 
3467
- return initialState;
3408
+ return revertState;
3468
3409
  },
3469
- updateInitialState: (newState: T) => {
3410
+ $updateInitialState: (newState: T) => {
3470
3411
  stateVersion++;
3471
3412
 
3472
3413
  const newUpdaterState = createProxyHandler(
3473
3414
  stateKey,
3474
3415
  effectiveSetState,
3475
- componentId,
3416
+ outerComponentId,
3476
3417
  sessionId
3477
3418
  );
3478
3419
  const initialState =
@@ -3504,13 +3445,13 @@ function createProxyHandler<T>(
3504
3445
  });
3505
3446
 
3506
3447
  return {
3507
- fetchId: (field: keyof T) => (newUpdaterState.get() as any)[field],
3448
+ fetchId: (field: keyof T) => (newUpdaterState.$get() as any)[field],
3508
3449
  };
3509
3450
  },
3510
3451
  };
3511
3452
 
3512
3453
  const returnShape = rebuildStateShape({
3513
- componentId,
3454
+ componentId: outerComponentId,
3514
3455
  path: [],
3515
3456
  });
3516
3457