cogsbox-state 0.5.471 → 0.5.473

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.
Files changed (46) hide show
  1. package/README.md +2 -5
  2. package/dist/CogsState.d.ts +105 -79
  3. package/dist/CogsState.d.ts.map +1 -1
  4. package/dist/CogsState.jsx +1082 -987
  5. package/dist/CogsState.jsx.map +1 -1
  6. package/dist/Components.d.ts.map +1 -1
  7. package/dist/Components.jsx +293 -243
  8. package/dist/Components.jsx.map +1 -1
  9. package/dist/PluginRunner.d.ts +10 -0
  10. package/dist/PluginRunner.d.ts.map +1 -0
  11. package/dist/PluginRunner.jsx +128 -0
  12. package/dist/PluginRunner.jsx.map +1 -0
  13. package/dist/index.d.ts +2 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +33 -26
  16. package/dist/index.js.map +1 -1
  17. package/dist/pluginStore.d.ts +43 -0
  18. package/dist/pluginStore.d.ts.map +1 -0
  19. package/dist/pluginStore.js +52 -0
  20. package/dist/pluginStore.js.map +1 -0
  21. package/dist/plugins.d.ts +1326 -0
  22. package/dist/plugins.d.ts.map +1 -0
  23. package/dist/plugins.js +76 -0
  24. package/dist/plugins.js.map +1 -0
  25. package/dist/store.d.ts +69 -26
  26. package/dist/store.d.ts.map +1 -1
  27. package/dist/store.js +436 -152
  28. package/dist/store.js.map +1 -1
  29. package/dist/utility.d.ts +1 -1
  30. package/dist/utility.d.ts.map +1 -1
  31. package/dist/utility.js +12 -12
  32. package/dist/utility.js.map +1 -1
  33. package/dist/validation.d.ts +7 -0
  34. package/dist/validation.d.ts.map +1 -0
  35. package/dist/validation.js +39 -0
  36. package/dist/validation.js.map +1 -0
  37. package/package.json +18 -13
  38. package/src/CogsState.tsx +719 -458
  39. package/src/Components.tsx +304 -180
  40. package/src/PluginRunner.tsx +208 -0
  41. package/src/index.ts +2 -0
  42. package/src/pluginStore.ts +159 -0
  43. package/src/plugins.ts +548 -0
  44. package/src/store.ts +881 -189
  45. package/src/utility.ts +31 -31
  46. package/src/validation.ts +84 -0
package/src/utility.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { InitialStateType, TransformedStateType } from './CogsState';
2
- import { getGlobalStore } from './store';
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 InitialStateType<State> => {
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: InitialStateType<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
+ }