cogsbox-state 0.5.438 → 0.5.439
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 +14 -3
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +21 -10
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -713,30 +713,41 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
713
713
|
return { useCogsState, setCogsOptions } as CogsApi<State>;
|
|
714
714
|
};
|
|
715
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
|
|
716
729
|
export function createCogsStateFromSync<
|
|
717
|
-
|
|
718
|
-
>(syncSchema: {
|
|
719
|
-
schemas: any;
|
|
720
|
-
notifications: any;
|
|
721
|
-
}): ReturnType<typeof createCogsState<T>> {
|
|
730
|
+
TSyncSchema extends { schemas: Record<string, any>; notifications: any },
|
|
731
|
+
>(syncSchema: TSyncSchema): CogsApi<ExtractStateFromSync<TSyncSchema>> {
|
|
722
732
|
// Extract initial state
|
|
723
|
-
|
|
733
|
+
type StateType = ExtractStateFromSync<TSyncSchema>;
|
|
734
|
+
const initialState = {} as StateType;
|
|
724
735
|
|
|
725
736
|
for (const key in syncSchema.schemas) {
|
|
726
737
|
const entry = syncSchema.schemas[key];
|
|
727
738
|
if (entry.rawSchema) {
|
|
728
|
-
initialState[key as keyof
|
|
739
|
+
initialState[key as keyof StateType] = entry.rawSchema;
|
|
729
740
|
} else if (entry.schemas?.defaults) {
|
|
730
|
-
initialState[key as keyof
|
|
741
|
+
initialState[key as keyof StateType] = entry.schemas.defaults;
|
|
731
742
|
} else {
|
|
732
|
-
initialState[key as keyof
|
|
743
|
+
initialState[key as keyof StateType] = {} as any;
|
|
733
744
|
}
|
|
734
745
|
}
|
|
735
746
|
|
|
736
747
|
return createCogsState(initialState, {
|
|
737
748
|
__fromSyncSchema: true,
|
|
738
749
|
__syncNotifications: syncSchema.notifications,
|
|
739
|
-
})
|
|
750
|
+
});
|
|
740
751
|
}
|
|
741
752
|
|
|
742
753
|
const {
|