Skip to content

Commit

Permalink
Update src/composables/use-experience-controls.composable.ts
Browse files Browse the repository at this point in the history
Co-authored-by: Guillermo Cacheda <[email protected]>
  • Loading branch information
annacv and CachedaCodes authored Nov 9, 2023
1 parent 49f0f15 commit 8b41dca
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/composables/use-experience-controls.composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,28 @@ import { useStore } from './use-store.composable';
*
* @returns The experience controls property value.
*/
export const useXControls = ({
path,
defaultValue
}: {
path: never;
defaultValue?: string | number | boolean | number[];
}): ComputedRef => {
const experienceControls = (useStore('experienceControls') as unknown as ExperienceControlsState)
.controls.controls;
// Rename
export const useExperienceControls = (): {
getControlFromPath: <SomeType>(path: string, defaultValue?: SomeType) => ComputedRef<SomeType>;
} => {
const experienceControls = (useStore('experienceControls') as ExperienceControlsState).controls
.controls;

return computed(() => {
return getSafePropertyChain(experienceControls, path, defaultValue);
});
// extracted the current logic to a function. The function can be typed so the return type is known and also it infers the return type from the default value
const getControlFromPath = <SomeType>(
path: string,
defaultValue?: SomeType
): ComputedRef<SomeType> => {
return computed(() => {
return getSafePropertyChain(
experienceControls,
path as ExtractPath<typeof experienceControls>,
defaultValue
) as SomeType;
});
};

return {
getControlFromPath
};
};

0 comments on commit 8b41dca

Please sign in to comment.