cogsbox-state 0.5.437 → 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 +20 -10
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -713,31 +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
|
+
|
|
716
728
|
// Then create a simple helper that extracts state from sync schema
|
|
717
729
|
export function createCogsStateFromSync<
|
|
718
|
-
|
|
719
|
-
>(syncSchema: {
|
|
720
|
-
schemas: any;
|
|
721
|
-
notifications: any;
|
|
722
|
-
}): ReturnType<typeof createCogsState<T>> {
|
|
730
|
+
TSyncSchema extends { schemas: Record<string, any>; notifications: any },
|
|
731
|
+
>(syncSchema: TSyncSchema): CogsApi<ExtractStateFromSync<TSyncSchema>> {
|
|
723
732
|
// Extract initial state
|
|
724
|
-
|
|
733
|
+
type StateType = ExtractStateFromSync<TSyncSchema>;
|
|
734
|
+
const initialState = {} as StateType;
|
|
725
735
|
|
|
726
736
|
for (const key in syncSchema.schemas) {
|
|
727
737
|
const entry = syncSchema.schemas[key];
|
|
728
738
|
if (entry.rawSchema) {
|
|
729
|
-
initialState[key as keyof
|
|
739
|
+
initialState[key as keyof StateType] = entry.rawSchema;
|
|
730
740
|
} else if (entry.schemas?.defaults) {
|
|
731
|
-
initialState[key as keyof
|
|
741
|
+
initialState[key as keyof StateType] = entry.schemas.defaults;
|
|
732
742
|
} else {
|
|
733
|
-
initialState[key as keyof
|
|
743
|
+
initialState[key as keyof StateType] = {} as any;
|
|
734
744
|
}
|
|
735
745
|
}
|
|
736
746
|
|
|
737
747
|
return createCogsState(initialState, {
|
|
738
748
|
__fromSyncSchema: true,
|
|
739
749
|
__syncNotifications: syncSchema.notifications,
|
|
740
|
-
})
|
|
750
|
+
});
|
|
741
751
|
}
|
|
742
752
|
|
|
743
753
|
const {
|