From 6cb1dc4c0aa303c367a3a6b6c8a0631ab255eec5 Mon Sep 17 00:00:00 2001 From: divdavem Date: Thu, 28 Sep 2023 09:48:51 +0200 Subject: [PATCH] refactor: simplifying types (#161) --- core/lib/services/stores.ts | 56 +++++++++++++++++----------------- core/lib/services/writables.ts | 2 +- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/core/lib/services/stores.ts b/core/lib/services/stores.ts index b80b62cd12..2100c07a73 100644 --- a/core/lib/services/stores.ts +++ b/core/lib/services/stores.ts @@ -2,8 +2,8 @@ import type {ReadableSignal, StoreInput, StoreOptions, StoresInputValues, Update import {asReadable, batch, computed, derived, get, readable, writable} from '@amadeus-it-group/tansu'; import {identity} from '../utils'; -export type ToWritableSignal = { - [K in keyof P & keyof V as `${K & string}$`]-?: WritableSignal; +export type ToWritableSignal

= { + [K in keyof P as `${K & string}$`]-?: WritableSignal; }; export type ReadableSignals = { @@ -44,7 +44,7 @@ export type ToState(stores: ToWritableSignal) { +export function createPatch(stores: ToWritableSignal) { return function >(storesValues?: U | void) { batch(() => { for (const [name, value] of Object.entries(storesValues ?? {})) { @@ -87,13 +87,13 @@ const update = function (this: WritableSignal, updater: Updater = (value: U) => T | typeof INVALID_VALUE; +export type NormalizeValue = (value: T) => T | typeof INVALID_VALUE; -export interface WritableWithDefaultOptions { +export interface WritableWithDefaultOptions { /** * the normalize value function. should return the invalidValue symbol when the provided value is invalid */ - normalizeValue?: NormalizeValue; + normalizeValue?: NormalizeValue; /** * the equal function, allowing to compare two values. used to check if a previous and current values are equals. */ @@ -114,15 +114,15 @@ export interface WritableWithDefaultOptions { * @param own$ - Store containing the own value * @returns a writable store with the extra default value and normalization logic described above */ -export function writableWithDefault( +export function writableWithDefault( defValue: T, - config$: ReadableSignal = readable(undefined), - options: WritableWithDefaultOptions = {}, - own$: WritableSignal = writable(undefined) -): WritableSignal { - const {normalizeValue = identity as any, equal = Object.is} = options; + config$: ReadableSignal = readable(undefined), + options: WritableWithDefaultOptions = {}, + own$: WritableSignal = writable(undefined) +): WritableSignal { + const {normalizeValue = identity, equal = Object.is} = options; const getDefValue = () => defValue; - const callNormalizeValue = (value: U | undefined, defValue = getDefValue) => { + const callNormalizeValue = (value: T | undefined, defValue = getDefValue) => { const normalizedValue = value === undefined ? undefined : normalizeValue(value); if (normalizedValue === INVALID_VALUE) { console.error('Not using invalid value', value); @@ -136,7 +136,7 @@ export function writableWithDefault( const validatedDefConfig$ = computed(() => callNormalizeValue(config$()), {equal}); const validatedOwnValue$ = computed(() => callNormalizeValue(own$(), validatedDefConfig$), {equal}); return asReadable(validatedOwnValue$, { - set(value: U | undefined) { + set(value: T | undefined) { if (value !== undefined) { const normalizedValue = normalizeValue(value); if (normalizedValue === INVALID_VALUE) { @@ -151,7 +151,7 @@ export function writableWithDefault( }); } -export type ConfigValidator = {[K in keyof T & keyof U]?: WritableWithDefaultOptions}; +export type ConfigValidator = {[K in keyof T]?: WritableWithDefaultOptions}; /** * Returns true if the provided argument is a store (ReadableSignal). @@ -176,7 +176,7 @@ export const toWritableStore = (x: WritableSignal | T) => (isStore(x) ? x export const normalizeConfigStores = ( keys: (keyof T)[], - config?: ReadableSignal | ValuesOrReadableSignals + config?: ReadableSignal> | ValuesOrReadableSignals ): ReadableSignals => { const res: ReadableSignals = {}; if (config) { @@ -226,20 +226,20 @@ export const mergeConfigStores = (keys: (keyof T)[], config1?: * const {propA$, propB$} = writablesWithDefault(defConfig, {config}, validation); * ``` */ -export const writablesWithDefault = ( +export const writablesWithDefault = ( defConfig: T, - propsConfig?: PropsConfig, - options?: ConfigValidator -): ToWritableSignal => { + propsConfig?: PropsConfig, + options?: ConfigValidator +): ToWritableSignal => { const res: any = {}; - const keys = Object.keys(defConfig) as (string & keyof T & keyof U)[]; - const configStores = normalizeConfigStores(keys, propsConfig?.config); + const keys = Object.keys(defConfig) as (string & keyof T)[]; + const configStores = normalizeConfigStores(keys, propsConfig?.config); const props = propsConfig?.props; for (const key of keys) { - const propValue = props?.[key] as undefined | WritableSignal | U[typeof key]; + const propValue = props?.[key] as undefined | WritableSignal | T[typeof key]; res[`${key}$`] = writableWithDefault(defConfig[key], configStores[key], options?.[key], toWritableStore(propValue)); } - return res as ToWritableSignal; + return res as ToWritableSignal; }; /** @@ -268,11 +268,11 @@ export const writablesWithDefault = ( * const [{propA$, propB$}, patch] = writablesForProps(defConfig, config, validation); * ``` */ -export const writablesForProps = ( +export const writablesForProps = ( defConfig: T, - propsConfig?: PropsConfig, - options?: {[K in keyof T & keyof U]?: WritableWithDefaultOptions} -): [ToWritableSignal, ReturnType>] => { + propsConfig?: PropsConfig, + options?: {[K in keyof T]?: WritableWithDefaultOptions} +): [ToWritableSignal, ReturnType>] => { const stores = writablesWithDefault(defConfig, propsConfig, options); return [stores, createPatch(stores)]; }; diff --git a/core/lib/services/writables.ts b/core/lib/services/writables.ts index 183e81455e..48d8db1912 100644 --- a/core/lib/services/writables.ts +++ b/core/lib/services/writables.ts @@ -15,7 +15,7 @@ export const typeBoolean: WritableWithDefaultOptions = { normalizeValue: testToNormalizeValue(isBoolean), }; -export const typeString: WritableWithDefaultOptions = { +export const typeString: WritableWithDefaultOptions = { normalizeValue: testToNormalizeValue(isString), };