cogsbox-state 0.5.432 → 0.5.435
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/README.md +596 -238
- package/dist/CogsState.d.ts +103 -122
- package/dist/CogsState.jsx +1647 -1069
- package/dist/CogsState.jsx.map +1 -1
- package/dist/Functions.d.ts +1 -15
- package/dist/Functions.jsx +40 -188
- package/dist/Functions.jsx.map +1 -1
- package/dist/index.js +18 -19
- package/dist/store.d.ts +94 -92
- package/dist/store.js +232 -295
- package/dist/store.js.map +1 -1
- package/dist/useValidateZodPath.d.ts +1 -1
- package/dist/utility.d.ts +2 -2
- package/dist/utility.js +152 -169
- package/dist/utility.js.map +1 -1
- package/package.json +2 -1
- package/src/CogsState.tsx +3220 -1694
- package/src/Functions.tsx +167 -303
- package/src/store.ts +449 -440
- package/src/utility.ts +76 -95
- package/dist/useValidateZodPath.js +0 -59
- package/dist/useValidateZodPath.js.map +0 -1
package/dist/CogsState.d.ts
CHANGED
|
@@ -3,14 +3,15 @@ import { GenericObject } from './utility.js';
|
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import { ComponentsType } from './store.js';
|
|
5
5
|
|
|
6
|
-
type Prettify<T> = {
|
|
6
|
+
type Prettify<T> = T extends any ? {
|
|
7
7
|
[K in keyof T]: T[K];
|
|
8
|
-
}
|
|
8
|
+
} : never;
|
|
9
9
|
export type VirtualViewOptions = {
|
|
10
10
|
itemHeight?: number;
|
|
11
11
|
overscan?: number;
|
|
12
12
|
stickToBottom?: boolean;
|
|
13
13
|
dependencies?: any[];
|
|
14
|
+
scrollStickTolerance?: number;
|
|
14
15
|
};
|
|
15
16
|
export type VirtualStateObjectResult<T extends any[]> = {
|
|
16
17
|
/**
|
|
@@ -36,27 +37,11 @@ export type VirtualStateObjectResult<T extends any[]> = {
|
|
|
36
37
|
scrollToBottom: (behavior?: ScrollBehavior) => void;
|
|
37
38
|
scrollToIndex: (index: number, behavior?: ScrollBehavior) => void;
|
|
38
39
|
};
|
|
39
|
-
export type ServerSyncStatus = {
|
|
40
|
-
isFresh: boolean;
|
|
41
|
-
isFreshTime: number;
|
|
42
|
-
isStale: boolean;
|
|
43
|
-
isStaleTime: number;
|
|
44
|
-
isSyncing: boolean;
|
|
45
|
-
isSyncingTime: number;
|
|
46
|
-
};
|
|
47
40
|
export type SyncInfo = {
|
|
48
41
|
timeStamp: number;
|
|
49
42
|
userId: number;
|
|
50
43
|
};
|
|
51
|
-
export type FormElementParams<T> = {
|
|
52
|
-
get: () => T;
|
|
53
|
-
set: UpdateType<T>;
|
|
54
|
-
syncStatus: (SyncInfo & {
|
|
55
|
-
date: Date;
|
|
56
|
-
}) | null;
|
|
57
|
-
path: string[];
|
|
58
|
-
validationErrors: () => string[];
|
|
59
|
-
addValidationError: (message?: string) => void;
|
|
44
|
+
export type FormElementParams<T> = StateObject<T> & {
|
|
60
45
|
inputProps: {
|
|
61
46
|
ref?: React.RefObject<any>;
|
|
62
47
|
value?: T extends boolean ? never : T;
|
|
@@ -66,39 +51,51 @@ export type FormElementParams<T> = {
|
|
|
66
51
|
};
|
|
67
52
|
export type StateKeys = string;
|
|
68
53
|
type findWithFuncType<U> = (thisKey: keyof U, thisValue: U[keyof U]) => EndType<U> & StateObject<U>;
|
|
69
|
-
export type PushArgs<U, T> = (update: Prettify<U> | ((prevState: NonNullable<Prettify<U>>[]) => NonNullable<Prettify<U>>), opts?: UpdateOpts<U>) => StateObject<T>;
|
|
70
54
|
type CutFunctionType<T> = (index?: number, options?: {
|
|
71
55
|
waitForSync?: boolean;
|
|
72
56
|
}) => StateObject<T>;
|
|
73
57
|
export type InferArrayElement<T> = T extends (infer U)[] ? U : never;
|
|
58
|
+
export type StreamOptions<T, R = T> = {
|
|
59
|
+
bufferSize?: number;
|
|
60
|
+
flushInterval?: number;
|
|
61
|
+
bufferStrategy?: 'sliding' | 'dropping' | 'accumulate';
|
|
62
|
+
store?: (buffer: T[]) => R | R[];
|
|
63
|
+
onFlush?: (buffer: T[]) => void;
|
|
64
|
+
};
|
|
65
|
+
export type StreamHandle<T> = {
|
|
66
|
+
write: (data: T) => void;
|
|
67
|
+
writeMany: (data: T[]) => void;
|
|
68
|
+
flush: () => void;
|
|
69
|
+
close: () => void;
|
|
70
|
+
pause: () => void;
|
|
71
|
+
resume: () => void;
|
|
72
|
+
};
|
|
74
73
|
export type ArrayEndType<TShape extends unknown> = {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
stream: <T = Prettify<InferArrayElement<TShape>>, R = T>(options?: StreamOptions<T, R>) => StreamHandle<T>;
|
|
75
|
+
findWith: findWithFuncType<Prettify<InferArrayElement<TShape>>>;
|
|
76
|
+
index: (index: number) => StateObject<Prettify<InferArrayElement<TShape>>> & {
|
|
77
|
+
insert: InsertTypeObj<Prettify<InferArrayElement<TShape>>>;
|
|
78
78
|
cut: CutFunctionType<TShape>;
|
|
79
79
|
_index: number;
|
|
80
|
-
} & EndType<InferArrayElement<TShape
|
|
81
|
-
insert:
|
|
80
|
+
} & EndType<Prettify<InferArrayElement<TShape>>>;
|
|
81
|
+
insert: InsertType<Prettify<InferArrayElement<TShape>>>;
|
|
82
82
|
cut: CutFunctionType<TShape>;
|
|
83
|
+
cutSelected: () => void;
|
|
83
84
|
cutByValue: (value: string | number | boolean) => void;
|
|
84
85
|
toggleByValue: (value: string | number | boolean) => void;
|
|
85
|
-
stateSort: (compareFn: (a: InferArrayElement<TShape
|
|
86
|
-
useVirtualView: (options: VirtualViewOptions) => VirtualStateObjectResult<InferArrayElement<TShape
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
uniqueInsert: (payload: UpdateArg<InferArrayElement<TShape>>, fields?: (keyof InferArrayElement<TShape>)[], onMatch?: (existingItem: any) => any) => void;
|
|
96
|
-
stateFind: (callbackfn: (value: InferArrayElement<TShape>, index: number) => boolean) => StateObject<InferArrayElement<TShape>> | undefined;
|
|
97
|
-
stateFilter: (callbackfn: (value: InferArrayElement<TShape>, index: number) => void) => ArrayEndType<TShape>;
|
|
98
|
-
getSelected: () => StateObject<InferArrayElement<TShape>> | undefined;
|
|
86
|
+
stateSort: (compareFn: (a: Prettify<InferArrayElement<TShape>>, b: Prettify<InferArrayElement<TShape>>) => number) => ArrayEndType<TShape>;
|
|
87
|
+
useVirtualView: (options: VirtualViewOptions) => VirtualStateObjectResult<Prettify<InferArrayElement<TShape>>[]>;
|
|
88
|
+
stateList: (callbackfn: (setter: StateObject<Prettify<InferArrayElement<TShape>>>, index: number, arraySetter: StateObject<TShape>) => void) => any;
|
|
89
|
+
stateMap: <U>(callbackfn: (setter: StateObject<Prettify<InferArrayElement<TShape>>>, index: number, arraySetter: StateObject<TShape>) => U) => U[];
|
|
90
|
+
$stateMap: (callbackfn: (setter: StateObject<Prettify<InferArrayElement<TShape>>>, index: number, arraySetter: StateObject<TShape>) => void) => any;
|
|
91
|
+
stateFlattenOn: <K extends keyof Prettify<InferArrayElement<TShape>>>(field: K) => StateObject<InferArrayElement<Prettify<InferArrayElement<TShape>>[K]>[]>;
|
|
92
|
+
uniqueInsert: (payload: InsertParams<Prettify<InferArrayElement<TShape>>>, fields?: (keyof Prettify<InferArrayElement<TShape>>)[], onMatch?: (existingItem: any) => any) => void;
|
|
93
|
+
stateFind: (callbackfn: (value: Prettify<InferArrayElement<TShape>>, index: number) => boolean) => StateObject<Prettify<InferArrayElement<TShape>>> | undefined;
|
|
94
|
+
stateFilter: (callbackfn: (value: Prettify<InferArrayElement<TShape>>, index: number) => void) => ArrayEndType<TShape>;
|
|
95
|
+
getSelected: () => StateObject<Prettify<InferArrayElement<TShape>>> | undefined;
|
|
99
96
|
clearSelected: () => void;
|
|
100
97
|
getSelectedIndex: () => number;
|
|
101
|
-
last: () => StateObject<InferArrayElement<TShape
|
|
98
|
+
last: () => StateObject<Prettify<InferArrayElement<TShape>>> | undefined;
|
|
102
99
|
} & EndType<TShape>;
|
|
103
100
|
export type FormOptsType = {
|
|
104
101
|
validation?: {
|
|
@@ -112,22 +109,20 @@ export type FormOptsType = {
|
|
|
112
109
|
};
|
|
113
110
|
export type FormControl<T> = (obj: FormElementParams<T>) => JSX.Element;
|
|
114
111
|
export type UpdateArg<S> = S | ((prevState: S) => S);
|
|
115
|
-
export type
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
[K in keyof T]-?: ObjectEndType<T[K]>;
|
|
122
|
-
} & {
|
|
123
|
-
stateObject: (callbackfn: (value: T, setter: StateObject<T>) => void) => any;
|
|
124
|
-
delete: () => void;
|
|
112
|
+
export type InsertParams<S> = S | ((prevState: {
|
|
113
|
+
state: S;
|
|
114
|
+
uuid: string;
|
|
115
|
+
}) => S);
|
|
116
|
+
export type UpdateType<T> = (payload: UpdateArg<T>) => {
|
|
117
|
+
synced: () => void;
|
|
125
118
|
};
|
|
119
|
+
export type InsertType<T> = (payload: InsertParams<T>, index?: number) => void;
|
|
120
|
+
export type InsertTypeObj<T> = (payload: InsertParams<T>) => void;
|
|
126
121
|
export type ValidationError = {
|
|
127
122
|
path: (string | number)[];
|
|
128
123
|
message: string;
|
|
129
124
|
};
|
|
130
|
-
type EffectFunction<T, R> = (state: T) => R;
|
|
125
|
+
type EffectFunction<T, R> = (state: T, deps: any[]) => R;
|
|
131
126
|
export type EndType<T, IsArrayElement = false> = {
|
|
132
127
|
addValidation: (errors: ValidationError[]) => void;
|
|
133
128
|
applyJsonPatch: (patches: any[]) => void;
|
|
@@ -138,13 +133,13 @@ export type EndType<T, IsArrayElement = false> = {
|
|
|
138
133
|
get: () => T;
|
|
139
134
|
$get: () => T;
|
|
140
135
|
$derive: <R>(fn: EffectFunction<T, R>) => R;
|
|
141
|
-
_status:
|
|
142
|
-
getStatus: () =>
|
|
136
|
+
_status: 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
|
|
137
|
+
getStatus: () => 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
|
|
143
138
|
showValidationErrors: () => string[];
|
|
144
139
|
setValidation: (ctx: string) => void;
|
|
145
140
|
removeValidation: (ctx: string) => void;
|
|
146
141
|
ignoreFields: (fields: string[]) => StateObject<T>;
|
|
147
|
-
|
|
142
|
+
isSelected: boolean;
|
|
148
143
|
setSelected: (value: boolean) => void;
|
|
149
144
|
toggleSelected: () => void;
|
|
150
145
|
getFormRef: () => React.RefObject<any> | undefined;
|
|
@@ -160,7 +155,8 @@ export type EndType<T, IsArrayElement = false> = {
|
|
|
160
155
|
} : {});
|
|
161
156
|
export type StateObject<T> = (T extends any[] ? ArrayEndType<T> : T extends Record<string, unknown> | object ? {
|
|
162
157
|
[K in keyof T]-?: StateObject<T[K]>;
|
|
163
|
-
}
|
|
158
|
+
} : T extends string | number | boolean | null ? EndType<T, true> : never) & EndType<T, true> & {
|
|
159
|
+
toggle: T extends boolean ? () => void : never;
|
|
164
160
|
getAllFormRefs: () => Map<string, React.RefObject<any>>;
|
|
165
161
|
_componentId: string | null;
|
|
166
162
|
getComponents: () => ComponentsType;
|
|
@@ -179,55 +175,48 @@ export type StateObject<T> = (T extends any[] ? ArrayEndType<T> : T extends Reco
|
|
|
179
175
|
updateLog: UpdateTypeDetail[] | undefined;
|
|
180
176
|
update: UpdateTypeDetail;
|
|
181
177
|
}) => void) => void;
|
|
182
|
-
_isServerSynced: () => boolean;
|
|
183
178
|
getLocalStorage: (key: string) => LocalStorageData<T> | null;
|
|
184
179
|
};
|
|
185
180
|
export type CogsUpdate<T extends unknown> = UpdateType<T>;
|
|
186
|
-
export type EffectiveSetState<TStateObject> = (newStateOrFunction: UpdateArg<TStateObject>, path: string[], updateObj: {
|
|
187
|
-
updateType: "update" | "insert" | "cut";
|
|
188
|
-
}, validationKey?: string, opts?: UpdateOpts<TStateObject>) => void;
|
|
189
181
|
export type UpdateTypeDetail = {
|
|
190
182
|
timeStamp: number;
|
|
191
183
|
stateKey: string;
|
|
192
|
-
updateType:
|
|
184
|
+
updateType: 'update' | 'insert' | 'cut';
|
|
193
185
|
path: string[];
|
|
194
|
-
status:
|
|
186
|
+
status: 'new' | 'sent' | 'synced';
|
|
195
187
|
oldValue: any;
|
|
196
188
|
newValue: any;
|
|
197
189
|
userId?: number;
|
|
198
190
|
};
|
|
199
|
-
export type
|
|
200
|
-
|
|
201
|
-
action: ({ state, actionType }: {
|
|
202
|
-
state: T;
|
|
203
|
-
actionType: string;
|
|
204
|
-
}) => void;
|
|
205
|
-
debounce?: number;
|
|
206
|
-
}[];
|
|
207
|
-
type ArrayToObject<T extends string[]> = Record<T[number], string>;
|
|
208
|
-
type CookieType<T> = {
|
|
209
|
-
timeStamp: number;
|
|
210
|
-
value: T;
|
|
211
|
-
cookieName: string;
|
|
212
|
-
OnUnMountCookie?: Boolean;
|
|
213
|
-
};
|
|
214
|
-
export type CogsCookiesType<T extends string[] = string[]> = CookieType<ArrayToObject<T>>;
|
|
215
|
-
export type ReactivityType = "none" | "component" | "deps" | "all";
|
|
191
|
+
export type ReactivityUnion = 'none' | 'component' | 'deps' | 'all';
|
|
192
|
+
export type ReactivityType = 'none' | 'component' | 'deps' | 'all' | Array<Prettify<'none' | 'component' | 'deps' | 'all'>>;
|
|
216
193
|
type ValidationOptionsType = {
|
|
217
194
|
key?: string;
|
|
218
195
|
zodSchema?: z.ZodTypeAny;
|
|
219
196
|
onBlur?: boolean;
|
|
220
197
|
};
|
|
198
|
+
type SyncApi = {
|
|
199
|
+
updateState: (data: {
|
|
200
|
+
operation: any;
|
|
201
|
+
}) => void;
|
|
202
|
+
connected: boolean;
|
|
203
|
+
clientId: string | null;
|
|
204
|
+
subscribers: string[];
|
|
205
|
+
};
|
|
221
206
|
export type OptionsType<T extends unknown = unknown> = {
|
|
222
207
|
log?: boolean;
|
|
223
208
|
componentId?: string;
|
|
224
|
-
|
|
209
|
+
cogsSync?: (stateObject: StateObject<T>) => SyncApi;
|
|
225
210
|
validation?: ValidationOptionsType;
|
|
226
|
-
enableServerState?: boolean;
|
|
227
211
|
serverState?: {
|
|
228
212
|
id?: string | number;
|
|
229
213
|
data?: T;
|
|
230
|
-
status?:
|
|
214
|
+
status?: 'pending' | 'error' | 'success' | 'loading';
|
|
215
|
+
timestamp?: number;
|
|
216
|
+
merge?: boolean | {
|
|
217
|
+
strategy: 'append' | 'prepend' | 'diff';
|
|
218
|
+
key?: string;
|
|
219
|
+
};
|
|
231
220
|
};
|
|
232
221
|
sync?: {
|
|
233
222
|
action: (state: T) => Promise<{
|
|
@@ -252,44 +241,12 @@ export type OptionsType<T extends unknown = unknown> = {
|
|
|
252
241
|
onChange?: (state: T) => void;
|
|
253
242
|
};
|
|
254
243
|
formElements?: FormsElementsType;
|
|
255
|
-
enabledSync?: (state: T) => boolean;
|
|
256
244
|
reactiveDeps?: (state: T) => any[] | true;
|
|
257
|
-
reactiveType?: ReactivityType
|
|
245
|
+
reactiveType?: ReactivityType;
|
|
258
246
|
syncUpdate?: Partial<UpdateTypeDetail>;
|
|
259
|
-
|
|
247
|
+
defaultState?: T;
|
|
260
248
|
dependencies?: any[];
|
|
261
249
|
};
|
|
262
|
-
export type ServerSyncType<T> = {
|
|
263
|
-
testKey?: string;
|
|
264
|
-
syncKey: (({ state }: {
|
|
265
|
-
state: T;
|
|
266
|
-
}) => string) | string;
|
|
267
|
-
syncFunction: ({ state }: {
|
|
268
|
-
state: T;
|
|
269
|
-
}) => void;
|
|
270
|
-
debounce?: number;
|
|
271
|
-
snapshot?: {
|
|
272
|
-
name: (({ state }: {
|
|
273
|
-
state: T;
|
|
274
|
-
}) => string) | string;
|
|
275
|
-
stateKeys: StateKeys[];
|
|
276
|
-
currentUrl: string;
|
|
277
|
-
currentParams?: URLSearchParams;
|
|
278
|
-
};
|
|
279
|
-
};
|
|
280
|
-
export type SyncActionsType<T> = {
|
|
281
|
-
syncKey: string;
|
|
282
|
-
rollBackState?: T;
|
|
283
|
-
actionTimeStamp: number;
|
|
284
|
-
retryCount?: number;
|
|
285
|
-
status: "success" | "waiting" | "rolledBack" | "error" | "cancelled" | "failed";
|
|
286
|
-
snapshot?: {
|
|
287
|
-
name: string;
|
|
288
|
-
stateKeys: StateKeys[];
|
|
289
|
-
currentUrl: string;
|
|
290
|
-
currentParams?: URLSearchParams;
|
|
291
|
-
};
|
|
292
|
-
};
|
|
293
250
|
export type ValidationWrapperOptions<T extends unknown = unknown> = {
|
|
294
251
|
children: React.ReactNode;
|
|
295
252
|
active: boolean;
|
|
@@ -336,26 +293,50 @@ type LocalStorageData<T> = {
|
|
|
336
293
|
lastUpdated: number;
|
|
337
294
|
lastSyncedWithServer?: number;
|
|
338
295
|
baseServerState?: T;
|
|
296
|
+
stateSource?: 'default' | 'server' | 'localStorage';
|
|
339
297
|
};
|
|
340
298
|
export declare const notifyComponent: (stateKey: string, componentId: string) => void;
|
|
341
|
-
export declare function useCogsStateFn<TStateObject extends unknown>(stateObject: TStateObject, { stateKey,
|
|
299
|
+
export declare function useCogsStateFn<TStateObject extends unknown>(stateObject: TStateObject, { stateKey, localStorage, formElements, reactiveDeps, reactiveType, componentId, defaultState, syncUpdate, dependencies, serverState, }?: {
|
|
342
300
|
stateKey?: string;
|
|
343
301
|
componentId?: string;
|
|
344
|
-
|
|
345
|
-
} & OptionsType<TStateObject>):
|
|
302
|
+
defaultState?: TStateObject;
|
|
303
|
+
} & OptionsType<TStateObject>): StateObject<TStateObject>;
|
|
304
|
+
export type MetaData = {
|
|
305
|
+
/**
|
|
306
|
+
* An array of the full, unique string IDs (e.g., `"stateKey.arrayName.id:123"`)
|
|
307
|
+
* of the items that belong to the current derived "view" of an array.
|
|
308
|
+
* This is the primary mechanism for tracking the state of filtered or sorted lists.
|
|
309
|
+
*
|
|
310
|
+
* - `stateFilter` populates this with only the IDs of items that passed the filter.
|
|
311
|
+
* - `stateSort` reorders this list to match the new sort order.
|
|
312
|
+
* - All subsequent chained operations (like `.get()`, `.index()`, or `.cut()`)
|
|
313
|
+
* MUST consult this list first to know which items they apply to and in what order.
|
|
314
|
+
*/
|
|
315
|
+
validIds?: string[];
|
|
316
|
+
/**
|
|
317
|
+
* An array of the actual filter functions that have been applied in a chain.
|
|
318
|
+
* This is primarily used by reactive renderers like `$stateMap` to make predictions.
|
|
319
|
+
*
|
|
320
|
+
* For example, when a new item is inserted into the original source array, a
|
|
321
|
+
* `$stateMap` renderer on a filtered view can use these functions to test if the
|
|
322
|
+
* newly inserted item should be dynamically rendered in its view.
|
|
323
|
+
*/
|
|
324
|
+
transforms?: Array<{
|
|
325
|
+
type: 'filter' | 'sort';
|
|
326
|
+
fn: Function;
|
|
327
|
+
}>;
|
|
328
|
+
};
|
|
346
329
|
export declare function $cogsSignal(proxy: {
|
|
347
330
|
_path: string[];
|
|
348
331
|
_stateKey: string;
|
|
349
332
|
_effect?: string;
|
|
333
|
+
_meta?: MetaData;
|
|
350
334
|
}): import('react').FunctionComponentElement<{
|
|
351
335
|
proxy: {
|
|
352
336
|
_path: string[];
|
|
353
337
|
_stateKey: string;
|
|
354
338
|
_effect?: string;
|
|
339
|
+
_meta?: MetaData;
|
|
355
340
|
};
|
|
356
341
|
}>;
|
|
357
|
-
export declare function $cogsSignalStore(proxy: {
|
|
358
|
-
_path: string[];
|
|
359
|
-
_stateKey: string;
|
|
360
|
-
}): import('react').ReactSVGElement;
|
|
361
342
|
export {};
|