cogsbox-state 0.5.439 → 0.5.441

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.439",
3
+ "version": "0.5.441",
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
@@ -554,44 +554,7 @@ type CogsApi<T extends Record<string, any>> = {
554
554
  setCogsOptions: SetCogsOptionsFunc<T>;
555
555
  };
556
556
 
557
- type ExtractStateFromSyncSchema<T> = T extends {
558
- schemas: infer S;
559
- notifications: any;
560
- }
561
- ? S extends Record<string, any>
562
- ? {
563
- [K in keyof S]: S[K] extends { rawSchema: infer R }
564
- ? R
565
- : S[K] extends { schemas: { defaults: infer D } }
566
- ? D
567
- : never;
568
- }
569
- : never
570
- : never;
571
-
572
- // Type to extract just the sync schema structure
573
- type SyncSchemaStructure<T = any> = {
574
- schemas: Record<
575
- string,
576
- {
577
- rawSchema?: any;
578
- schemas?: {
579
- sql?: any;
580
- client?: any;
581
- validation?: any;
582
- defaults?: any;
583
- };
584
- api?: {
585
- initialData?: string;
586
- update?: string;
587
- };
588
- validate?: (data: unknown, ctx: any) => any;
589
- validateClient?: (data: unknown) => any;
590
- serializable?: any;
591
- }
592
- >;
593
- notifications: Record<string, (state: any, context: any) => any>;
594
- };
557
+ // Minimal change - just add a second parameter to detect sync schema
595
558
  export const createCogsState = <State extends Record<StateKeys, unknown>>(
596
559
  initialState: State,
597
560
  opt?: {
@@ -614,6 +577,7 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
614
577
  .getState()
615
578
  .setInitialStateOptions('__notifications', opt.__syncNotifications);
616
579
  }
580
+
617
581
  // ... rest of your existing createCogsState code unchanged ...
618
582
 
619
583
  Object.keys(statePart).forEach((key) => {
@@ -712,42 +676,39 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
712
676
 
713
677
  return { useCogsState, setCogsOptions } as CogsApi<State>;
714
678
  };
715
-
716
- type ExtractStateFromSync<T> = T extends { schemas: infer S }
717
- ? S extends Record<string, any>
718
- ? {
719
- [K in keyof S]: S[K] extends { rawSchema: infer R }
720
- ? R
721
- : S[K] extends { schemas: { defaults: infer D } }
722
- ? D
723
- : any;
724
- }
725
- : never
726
- : never;
727
-
728
- // Then create a simple helper that extracts state from sync schema
679
+ // or wherever your shape types are
729
680
  export function createCogsStateFromSync<
730
- TSyncSchema extends { schemas: Record<string, any>; notifications: any },
731
- >(syncSchema: TSyncSchema): CogsApi<ExtractStateFromSync<TSyncSchema>> {
732
- // Extract initial state
733
- type StateType = ExtractStateFromSync<TSyncSchema>;
734
- const initialState = {} as StateType;
735
-
736
- for (const key in syncSchema.schemas) {
737
- const entry = syncSchema.schemas[key];
738
- if (entry.rawSchema) {
739
- initialState[key as keyof StateType] = entry.rawSchema;
740
- } else if (entry.schemas?.defaults) {
741
- initialState[key as keyof StateType] = entry.schemas.defaults;
742
- } else {
743
- initialState[key as keyof StateType] = {} as any;
744
- }
681
+ TSyncSchema extends {
682
+ schemas: Record<
683
+ string,
684
+ {
685
+ schemas: { defaults: any };
686
+ [key: string]: any;
687
+ }
688
+ >;
689
+ notifications: Record<string, any>;
690
+ },
691
+ >(
692
+ syncSchema: TSyncSchema
693
+ ): CogsApi<{
694
+ [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaults'];
695
+ }> {
696
+ const schemas = syncSchema.schemas;
697
+ const initialState: any = {};
698
+
699
+ // Extract defaults from each entry
700
+ for (const key in schemas) {
701
+ const entry = schemas[key]!;
702
+ initialState[key] = entry.schemas?.defaults || {};
745
703
  }
746
704
 
747
- return createCogsState(initialState, {
748
- __fromSyncSchema: true,
749
- __syncNotifications: syncSchema.notifications,
750
- });
705
+ // Store sync metadata
706
+ // getGlobalStore.getState().setInitialStateOptions('__syncSchema', syncSchema);
707
+ getGlobalStore
708
+ .getState()
709
+ .setInitialStateOptions('__notifications', syncSchema.notifications);
710
+
711
+ return createCogsState(initialState);
751
712
  }
752
713
 
753
714
  const {