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