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/dist/CogsState.d.ts +9 -13
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +123 -126
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +32 -71
package/package.json
CHANGED
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
|
-
|
|
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 {
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
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
|
-
|
|
748
|
-
|
|
749
|
-
|
|
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 {
|