cogsbox-state 0.5.472 → 0.5.474
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 +48 -18
- package/dist/CogsState.d.ts +98 -82
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +1030 -960
- package/dist/CogsState.jsx.map +1 -1
- package/dist/Components.d.ts.map +1 -1
- package/dist/Components.jsx +299 -219
- package/dist/Components.jsx.map +1 -1
- package/dist/PluginRunner.d.ts +10 -0
- package/dist/PluginRunner.d.ts.map +1 -0
- package/dist/PluginRunner.jsx +122 -0
- package/dist/PluginRunner.jsx.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +33 -26
- package/dist/index.js.map +1 -1
- package/dist/pluginStore.d.ts +81 -0
- package/dist/pluginStore.d.ts.map +1 -0
- package/dist/pluginStore.js +52 -0
- package/dist/pluginStore.js.map +1 -0
- package/dist/plugins.d.ts +1323 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +76 -0
- package/dist/plugins.js.map +1 -0
- package/dist/store.d.ts +50 -15
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +509 -470
- package/dist/store.js.map +1 -1
- package/dist/utility.d.ts +1 -1
- package/dist/utility.d.ts.map +1 -1
- package/dist/utility.js +12 -12
- package/dist/utility.js.map +1 -1
- package/dist/validation.d.ts +7 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +39 -0
- package/dist/validation.js.map +1 -0
- package/package.json +13 -3
- package/src/CogsState.tsx +657 -457
- package/src/Components.tsx +291 -194
- package/src/PluginRunner.tsx +203 -0
- package/src/index.ts +2 -0
- package/src/pluginStore.ts +176 -0
- package/src/plugins.ts +544 -0
- package/src/store.ts +748 -493
- package/src/utility.ts +31 -31
- package/src/validation.ts +84 -0
package/src/utility.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { TransformedStateType, CogsInitialState } from './CogsState';
|
|
2
|
+
|
|
3
3
|
export const isObject = (item: any): item is Record<string, any> => {
|
|
4
4
|
return (
|
|
5
5
|
item && typeof item === 'object' && !Array.isArray(item) && item !== null
|
|
@@ -318,15 +318,42 @@ export function getArrayLengthDifferencesArray(obj1: any, obj2: any) {
|
|
|
318
318
|
return convertedDiff;
|
|
319
319
|
}
|
|
320
320
|
|
|
321
|
+
export function debounce<F extends (...args: any[]) => any>(
|
|
322
|
+
func: (...args: any[]) => any,
|
|
323
|
+
wait: number
|
|
324
|
+
): F & { cancel: () => void } {
|
|
325
|
+
let timeoutID: NodeJS.Timeout | null = null;
|
|
326
|
+
|
|
327
|
+
const debounced: any = (...args: Parameters<F>) => {
|
|
328
|
+
if (timeoutID) {
|
|
329
|
+
clearTimeout(timeoutID);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
timeoutID = setTimeout(() => func(...args), wait);
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
debounced.cancel = () => {
|
|
336
|
+
if (timeoutID) {
|
|
337
|
+
clearTimeout(timeoutID);
|
|
338
|
+
timeoutID = null;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
return debounced as DebouncedFunction<F>;
|
|
343
|
+
}
|
|
344
|
+
export type DebouncedFunction<F extends (...args: any[]) => any> = F & {
|
|
345
|
+
cancel: () => void;
|
|
346
|
+
};
|
|
347
|
+
|
|
321
348
|
export function transformStateFunc<State extends unknown>(initialState: State) {
|
|
322
|
-
const isInitialStateType = (state: any): state is
|
|
349
|
+
const isInitialStateType = (state: any): state is CogsInitialState<State> => {
|
|
323
350
|
return Object.values(state).some((value) =>
|
|
324
351
|
value?.hasOwnProperty('initialState')
|
|
325
352
|
);
|
|
326
353
|
};
|
|
327
354
|
let initalOptions: GenericObject = {};
|
|
328
355
|
const transformInitialState = (
|
|
329
|
-
state:
|
|
356
|
+
state: CogsInitialState<State>
|
|
330
357
|
): GenericObject | GenericObject[] => {
|
|
331
358
|
const transformedState: GenericObject | GenericObject[] = {};
|
|
332
359
|
Object.entries(state).forEach(([key, value]) => {
|
|
@@ -351,30 +378,3 @@ export function transformStateFunc<State extends unknown>(initialState: State) {
|
|
|
351
378
|
GenericObject,
|
|
352
379
|
];
|
|
353
380
|
}
|
|
354
|
-
|
|
355
|
-
export function debounce<F extends (...args: any[]) => any>(
|
|
356
|
-
func: (...args: any[]) => any,
|
|
357
|
-
wait: number
|
|
358
|
-
): F & { cancel: () => void } {
|
|
359
|
-
let timeoutID: NodeJS.Timeout | null = null;
|
|
360
|
-
|
|
361
|
-
const debounced: any = (...args: Parameters<F>) => {
|
|
362
|
-
if (timeoutID) {
|
|
363
|
-
clearTimeout(timeoutID);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
timeoutID = setTimeout(() => func(...args), wait);
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
debounced.cancel = () => {
|
|
370
|
-
if (timeoutID) {
|
|
371
|
-
clearTimeout(timeoutID);
|
|
372
|
-
timeoutID = null;
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
|
|
376
|
-
return debounced as DebouncedFunction<F>;
|
|
377
|
-
}
|
|
378
|
-
export type DebouncedFunction<F extends (...args: any[]) => any> = F & {
|
|
379
|
-
cancel: () => void;
|
|
380
|
-
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// in utils.ts
|
|
2
|
+
|
|
3
|
+
import { UpdateTypeDetail } from './CogsState'; // Adjust path as needed
|
|
4
|
+
import { getGlobalStore } from './store';
|
|
5
|
+
import { isDeepEqual } from './utility';
|
|
6
|
+
import { ZodType } from 'zod';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* THIS IS THE CORRECTED FUNCTION.
|
|
10
|
+
*/
|
|
11
|
+
export function runValidation(
|
|
12
|
+
operation: UpdateTypeDetail,
|
|
13
|
+
trigger: 'onBlur' | 'onChange' | 'programmatic'
|
|
14
|
+
) {
|
|
15
|
+
const {
|
|
16
|
+
getInitialOptions,
|
|
17
|
+
getShadowMetadata,
|
|
18
|
+
setShadowMetadata,
|
|
19
|
+
notifyPathSubscribers,
|
|
20
|
+
} = getGlobalStore.getState();
|
|
21
|
+
const { stateKey, path, newValue, updateType } = operation;
|
|
22
|
+
|
|
23
|
+
if (updateType !== 'update') {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const validationOptions = getInitialOptions(stateKey)?.validation;
|
|
28
|
+
if (!validationOptions) return;
|
|
29
|
+
|
|
30
|
+
const currentMeta = getShadowMetadata(stateKey, path) || {};
|
|
31
|
+
const fieldSchema = currentMeta.typeInfo?.schema as ZodType | undefined;
|
|
32
|
+
const currentStatus = currentMeta.validation?.status;
|
|
33
|
+
|
|
34
|
+
let shouldValidate = false;
|
|
35
|
+
let severity: 'error' | 'warning' = 'error';
|
|
36
|
+
|
|
37
|
+
if (trigger === 'onBlur' && validationOptions.onBlur) {
|
|
38
|
+
shouldValidate = true;
|
|
39
|
+
severity = validationOptions.onBlur;
|
|
40
|
+
} else if (trigger === 'onChange' && validationOptions.onChange) {
|
|
41
|
+
shouldValidate = true;
|
|
42
|
+
severity = validationOptions.onChange;
|
|
43
|
+
} else if (trigger === 'onChange' && currentStatus === 'INVALID') {
|
|
44
|
+
shouldValidate = true;
|
|
45
|
+
severity = 'warning';
|
|
46
|
+
} else if (trigger === 'programmatic') {
|
|
47
|
+
if (validationOptions.onBlur || validationOptions.onChange) {
|
|
48
|
+
shouldValidate = true;
|
|
49
|
+
|
|
50
|
+
severity =
|
|
51
|
+
validationOptions.onBlur || validationOptions.onChange || 'error';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!shouldValidate || !fieldSchema) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The rest of the function is the same.
|
|
60
|
+
const result = fieldSchema.safeParse(newValue);
|
|
61
|
+
const newValidationState = {
|
|
62
|
+
status: result.success ? 'VALID' : 'INVALID',
|
|
63
|
+
errors: result.success
|
|
64
|
+
? []
|
|
65
|
+
: [
|
|
66
|
+
{
|
|
67
|
+
source: 'client',
|
|
68
|
+
message: result.error.issues[0]?.message || 'Invalid value',
|
|
69
|
+
severity: severity,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
lastValidated: Date.now(),
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
if (!isDeepEqual(currentMeta.validation, newValidationState)) {
|
|
76
|
+
setShadowMetadata(stateKey, path, {
|
|
77
|
+
...currentMeta,
|
|
78
|
+
validation: newValidationState,
|
|
79
|
+
});
|
|
80
|
+
notifyPathSubscribers([stateKey, ...path].join('.'), {
|
|
81
|
+
type: 'VALIDATION_UPDATE',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|