cogsbox-state 0.5.475-canary.10 → 0.5.475-canary.12

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.475-canary.10",
3
+ "version": "0.5.475-canary.12",
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
@@ -74,11 +74,6 @@ export type FormElementParams<T> = StateObject<T> & {
74
74
 
75
75
  export type StateKeys = string;
76
76
 
77
- type findWithFuncType<U> = (
78
- thisKey: keyof U,
79
- thisValue: U[keyof U]
80
- ) => EndType<U> & StateObject<U>;
81
-
82
77
  type CutFunctionType<T> = (
83
78
  index?: number,
84
79
  options?: { waitForSync?: boolean }
@@ -102,78 +97,24 @@ export type StreamHandle<T> = {
102
97
  pause: () => void;
103
98
  resume: () => void;
104
99
  };
100
+ export type FormControl<T> = (obj: FormElementParams<T>) => JSX.Element;
105
101
 
106
- export type ArrayEndType<
107
- TShape extends unknown,
108
- TPlugins extends readonly CogsPlugin<any, any, any, any, any>[],
109
- > = {
110
- $stream: <T = Prettify<InferArrayElement<TShape>>, R = T>(
111
- options?: StreamOptions<T, R>
112
- ) => StreamHandle<T>;
113
- $findWith: findWithFuncType<Prettify<InferArrayElement<TShape>>>;
114
- $index: (index: number) => StateObject<
115
- Prettify<InferArrayElement<TShape>>
116
- > & {
117
- $insert: InsertTypeObj<Prettify<InferArrayElement<TShape>>>;
118
- $cut: CutFunctionType<TShape>;
119
- $_index: number;
120
- } & EndType<Prettify<InferArrayElement<TShape>>>;
121
- $insert: InsertType<Prettify<InferArrayElement<TShape>>>;
122
- $insertMany: (payload: InferArrayElement<TShape>[]) => void;
123
- $cut: CutFunctionType<TShape>;
124
- $cutSelected: () => void;
125
- $cutByValue: (value: string | number | boolean) => void;
126
- $toggleByValue: (value: string | number | boolean) => void;
127
- $sort: (
128
- compareFn: (
129
- a: Prettify<InferArrayElement<TShape>>,
130
- b: Prettify<InferArrayElement<TShape>>
131
- ) => number
132
- ) => ArrayEndType<TShape, TPlugins>;
133
-
134
- $list: (
135
- callbackfn: (
136
- setter: StateObject<Prettify<InferArrayElement<TShape>>>,
137
- index: number,
138
- arraySetter: StateObject<TShape>
139
- ) => void
140
- ) => any;
141
- $map: <U>(
142
- callbackfn: (
143
- setter: StateObject<Prettify<InferArrayElement<TShape>>>,
144
- index: number,
145
- arraySetter: StateObject<TShape>
146
- ) => U
147
- ) => U[];
102
+ export type UpdateArg<S> = S | ((prevState: S) => S);
103
+ export type InsertParams<S> =
104
+ | S
105
+ | ((prevState: { state: S; uuid: string }) => S);
106
+ export type UpdateType<T> = (payload: UpdateArg<T>) => { synced: () => void };
148
107
 
149
- $stateFlattenOn: <K extends keyof Prettify<InferArrayElement<TShape>>>(
150
- field: K
151
- ) => StateObject<InferArrayElement<Prettify<InferArrayElement<TShape>>[K]>[]>;
152
- $uniqueInsert: (
153
- payload: InsertParams<Prettify<InferArrayElement<TShape>>>,
154
- fields?: (keyof Prettify<InferArrayElement<TShape>>)[],
155
- onMatch?: (existingItem: any) => any
156
- ) => void;
157
- $find: (
158
- callbackfn: (
159
- value: Prettify<InferArrayElement<TShape>>,
160
- index: number
161
- ) => boolean
162
- ) => StateObject<Prettify<InferArrayElement<TShape>>> | undefined;
163
- $filter: (
164
- callbackfn: (
165
- value: Prettify<InferArrayElement<TShape>>,
166
- index: number
167
- ) => void
168
- ) => ArrayEndType<TShape, TPlugins>;
169
- $getSelected: () =>
170
- | StateObject<Prettify<InferArrayElement<TShape>>>
171
- | undefined;
172
- $clearSelected: () => void;
173
- $getSelectedIndex: () => number;
174
- $last: () => StateObject<Prettify<InferArrayElement<TShape>>> | undefined;
175
- } & EndType<TShape>;
108
+ export type InsertType<T> = (payload: InsertParams<T>, index?: number) => void;
109
+ export type InsertTypeObj<T> = (payload: InsertParams<T>) => void;
176
110
 
111
+ type EffectFunction<T, R> = (state: T, deps: any[]) => R;
112
+ export type PerPathFormOptsType<
113
+ TState,
114
+ TPlugins extends readonly CogsPlugin<any, any, any, any, any>[] = [],
115
+ > = Omit<FormOptsType, 'formElements'> & {
116
+ formElements?: FormsElementsType<TState, TPlugins>;
117
+ };
177
118
  export type FormOptsType = {
178
119
  validation?: {
179
120
  hideMessage?: boolean;
@@ -189,34 +130,21 @@ export type FormOptsType = {
189
130
  };
190
131
  };
191
132
 
192
- export type FormControl<T> = (obj: FormElementParams<T>) => JSX.Element;
193
-
194
- export type UpdateArg<S> = S | ((prevState: S) => S);
195
- export type InsertParams<S> =
196
- | S
197
- | ((prevState: { state: S; uuid: string }) => S);
198
- export type UpdateType<T> = (payload: UpdateArg<T>) => { synced: () => void };
199
-
200
- export type InsertType<T> = (payload: InsertParams<T>, index?: number) => void;
201
- export type InsertTypeObj<T> = (payload: InsertParams<T>) => void;
202
-
203
- type EffectFunction<T, R> = (state: T, deps: any[]) => R;
204
- export type PerPathFormOptsType<
205
- TState,
206
- TPlugins extends readonly CogsPlugin<any, any, any, any, any>[] = [],
207
- > = Omit<FormOptsType, 'formElements'> & {
208
- formElements?: FormsElementsType<TState, TPlugins>;
133
+ // Separate type for array-element-specific methods
134
+ export type ArrayElementExtras<TParentArray = unknown> = {
135
+ $cutThis: () => void;
136
+ $_index: number;
209
137
  };
138
+
139
+ // EndType - NO $cutThis here, it's purely contextual
210
140
  export type EndType<
211
141
  T,
212
142
  TPlugins extends readonly CogsPlugin<any, any, any, any, any>[] = [],
213
- IsArrayElement = false,
214
143
  > = {
215
144
  $getPluginMetaData: (pluginName: string) => Record<string, any>;
216
145
  $addPluginMetaData: (key: string, data: Record<string, any>) => void;
217
146
  $removePluginMetaData: (key: string) => void;
218
147
  $setOptions: (options: OptionsType<T>) => void;
219
-
220
148
  $addZodValidation: (
221
149
  errors: ValidationError[],
222
150
  source?: 'client' | 'sync_engine' | 'api'
@@ -231,12 +159,9 @@ export type EndType<
231
159
  $_path: string[];
232
160
  $_stateKey: string;
233
161
  $isolate: {
234
- // Overload 1: Just the render function (Default behavior)
235
162
  (
236
163
  renderFn: (state: StateObject<T, TPlugins>) => React.ReactNode
237
164
  ): JSX.Element;
238
-
239
- // Overload 2: Dependencies array + render function (Optimized behavior)
240
165
  (
241
166
  dependencies: any[],
242
167
  renderFn: (state: StateObject<T, TPlugins>) => React.ReactNode
@@ -252,10 +177,8 @@ export type EndType<
252
177
  $_status: 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
253
178
  $getStatus: () => 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
254
179
  $showValidationErrors: () => string[];
255
-
256
180
  $setValidation: (ctx: string) => void;
257
181
  $removeValidation: (ctx: string) => void;
258
-
259
182
  $isSelected: boolean;
260
183
  $setSelected: (value: boolean) => void;
261
184
  $toggleSelected: () => void;
@@ -277,29 +200,140 @@ export type EndType<
277
200
  hideMessage?: boolean;
278
201
  }) => JSX.Element;
279
202
  $lastSynced?: SyncInfo;
280
- } & (IsArrayElement extends true ? { $cutThis: () => void } : {});
203
+ };
204
+
205
+ // Helper type for element returned from array methods
206
+ export type ArrayElementState<
207
+ TElement,
208
+ TParentArray,
209
+ TPlugins extends readonly CogsPlugin<any, any, any, any, any>[] = [],
210
+ > = StateObject<TElement, TPlugins> & ArrayElementExtras<TParentArray>;
211
+
212
+ export type ArrayEndType<
213
+ TShape extends unknown,
214
+ TPlugins extends readonly CogsPlugin<any, any, any, any, any>[],
215
+ > = {
216
+ (): TShape;
217
+ (newValue: TShape | ((prev: TShape) => TShape)): void;
218
+
219
+ $stream: <T = Prettify<InferArrayElement<TShape>>, R = T>(
220
+ options?: StreamOptions<T, R>
221
+ ) => StreamHandle<T>;
222
+
223
+ $findWith: <K extends keyof Prettify<InferArrayElement<TShape>>>(
224
+ key: K,
225
+ value: Prettify<InferArrayElement<TShape>>[K]
226
+ ) =>
227
+ | ArrayElementState<Prettify<InferArrayElement<TShape>>, TShape, TPlugins>
228
+ | undefined;
229
+
230
+ // Returns element WITH $cutThis
231
+ $index: (
232
+ index: number
233
+ ) => ArrayElementState<Prettify<InferArrayElement<TShape>>, TShape, TPlugins>;
234
+
235
+ $insert: InsertType<Prettify<InferArrayElement<TShape>>>;
236
+ $insertMany: (payload: InferArrayElement<TShape>[]) => void;
237
+ $cut: CutFunctionType<TShape>;
238
+ $cutSelected: () => void;
239
+ $cutByValue: (value: string | number | boolean) => void;
240
+ $toggleByValue: (value: string | number | boolean) => void;
241
+
242
+ // Returns array StateObject (chainable)
243
+ $sort: (
244
+ compareFn: (
245
+ a: Prettify<InferArrayElement<TShape>>,
246
+ b: Prettify<InferArrayElement<TShape>>
247
+ ) => number
248
+ ) => StateObject<TShape, TPlugins>;
249
+
250
+ // Callback receives element WITH $cutThis
251
+ $list: (
252
+ callbackfn: (
253
+ setter: ArrayElementState<
254
+ Prettify<InferArrayElement<TShape>>,
255
+ TShape,
256
+ TPlugins
257
+ >,
258
+ index: number,
259
+ arraySetter: StateObject<TShape, TPlugins>
260
+ ) => void
261
+ ) => any;
262
+
263
+ $map: <U>(
264
+ callbackfn: (
265
+ setter: ArrayElementState<
266
+ Prettify<InferArrayElement<TShape>>,
267
+ TShape,
268
+ TPlugins
269
+ >,
270
+ index: number,
271
+ arraySetter: StateObject<TShape, TPlugins>
272
+ ) => U
273
+ ) => U[];
274
+
275
+ $stateFlattenOn: <K extends keyof Prettify<InferArrayElement<TShape>>>(
276
+ field: K
277
+ ) => StateObject<
278
+ InferArrayElement<Prettify<InferArrayElement<TShape>>[K]>[],
279
+ TPlugins
280
+ >;
281
+
282
+ $uniqueInsert: (
283
+ payload: InsertParams<Prettify<InferArrayElement<TShape>>>,
284
+ fields?: (keyof Prettify<InferArrayElement<TShape>>)[],
285
+ onMatch?: (existingItem: any) => any
286
+ ) => void;
287
+
288
+ // Returns element WITH $cutThis
289
+ $find: (
290
+ callbackfn: (
291
+ value: Prettify<InferArrayElement<TShape>>,
292
+ index: number
293
+ ) => boolean
294
+ ) =>
295
+ | ArrayElementState<Prettify<InferArrayElement<TShape>>, TShape, TPlugins>
296
+ | undefined;
297
+
298
+ // Returns array StateObject (chainable)
299
+ $filter: (
300
+ callbackfn: (
301
+ value: Prettify<InferArrayElement<TShape>>,
302
+ index: number
303
+ ) => boolean
304
+ ) => StateObject<TShape, TPlugins>;
305
+
306
+ // Returns element WITH $cutThis
307
+ $getSelected: () =>
308
+ | ArrayElementState<Prettify<InferArrayElement<TShape>>, TShape, TPlugins>
309
+ | undefined;
310
+
311
+ $clearSelected: () => void;
312
+ $getSelectedIndex: () => number;
313
+
314
+ // Returns element WITH $cutThis
315
+ $last: () =>
316
+ | ArrayElementState<Prettify<InferArrayElement<TShape>>, TShape, TPlugins>
317
+ | undefined;
318
+ } & EndType<TShape, TPlugins>; // NO $cutThis on arrays themselves
281
319
 
282
320
  export type StateObject<
283
321
  T,
284
322
  TPlugins extends readonly CogsPlugin<any, any, any, any, any>[] = [],
285
323
  > = {
286
- // A. Callable Getter: state.count()
287
324
  (): T;
288
- // B. Callable Setter: state.count(5)
289
325
  (newValue: T | ((prev: T) => T)): void;
290
326
  } & (T extends any[]
291
327
  ? ArrayEndType<T, TPlugins>
292
328
  : T extends Record<string, unknown> | object
293
329
  ? { [K in keyof T]-?: StateObject<T[K], TPlugins> }
294
- : T extends string | number | boolean | null
295
- ? EndType<T, TPlugins, true>
296
- : never) &
297
- EndType<T, TPlugins, true> & {
330
+ : EndType<T, TPlugins>) & // primitives just get EndType
331
+ EndType<T, TPlugins> & {
332
+ // NO third param = no $cutThis
298
333
  $toggle: T extends boolean ? () => void : never;
299
334
  $validate: () => { success: boolean; data?: T; error?: any };
300
335
  $_componentId: string | null;
301
336
  $getComponents: () => ComponentsType;
302
-
303
337
  $_initialState: T;
304
338
  $updateInitialState: (newState: T | null) => {
305
339
  fetchId: (field: keyof T) => string | number;
@@ -308,7 +342,6 @@ export type StateObject<
308
342
  $_isLoading: boolean;
309
343
  $_serverState: T;
310
344
  $revertToInitialState: (obj?: { validationKey?: string }) => T;
311
-
312
345
  $middleware: (
313
346
  middles: ({
314
347
  updateLog,
@@ -318,7 +351,6 @@ export type StateObject<
318
351
  update: UpdateTypeDetail;
319
352
  }) => void
320
353
  ) => void;
321
-
322
354
  $getLocalStorage: (key: string) => LocalStorageData<T> | null;
323
355
  };
324
356