Skip to content

Commit

Permalink
optimize typings UIStoreActions / storeUpdater #163
Browse files Browse the repository at this point in the history
  • Loading branch information
elbakerino committed Mar 19, 2022
1 parent 7d584b3 commit 3c6e47e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
4 changes: 2 additions & 2 deletions packages/demo/src/material-ui/material-ui-custom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
StoreKeys, StoreSchemaType, WidgetProps,
UIMetaProvider, UIStoreProvider, useUIMeta,
WithValue, extractValue,
WidgetsBindingFactory, WithScalarValue,
WidgetsBindingFactory, WithScalarValue, onChangeHandler,
} from '@ui-schema/ui-schema'
import { browserT } from '../t'
import { UIApiProvider, loadSchemaUIApi } from '@ui-schema/ui-schema/UIApi'
Expand Down Expand Up @@ -194,7 +194,7 @@ const FreeFormEditor = () => {
const showValidity = true
const [store, setStore] = React.useState(() => createStore(OrderedMap()))

const onChange = React.useCallback((actions) => {
const onChange: onChangeHandler = React.useCallback((actions) => {
setStore(storeUpdater(actions))
}, [setStore])

Expand Down
6 changes: 3 additions & 3 deletions packages/ui-schema/src/UIStore/UIStoreProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ export interface WithOnChange<A = UIStoreActions> {
export interface WithValue<A = UIStoreActions> {
value: any | undefined
internalValue: any | undefined
showValidity: UIStoreContext['showValidity']
showValidity?: UIStoreContext['showValidity']
onChange: onChangeHandler<A>
}

export interface WithScalarValue<A = UIStoreActions> {
value: string | number | boolean | undefined | null
internalValue: any
showValidity: UIStoreContext['showValidity']
showValidity?: UIStoreContext['showValidity']
onChange: onChangeHandler<A>
}

export interface WithValidity<A = UIStoreActions> {
validity: any
onChange: onChangeHandler<A>
showValidity: UIStoreContext['showValidity']
showValidity?: UIStoreContext['showValidity']
}
31 changes: 17 additions & 14 deletions packages/ui-schema/src/UIStoreActions/UIStoreActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,52 @@ export type UIStoreActionScoped<S extends UIStoreType = UIStoreType, D extends U
effect?: (newData: D, newStore: S) => void
}

export interface UIStoreAction extends UIStoreActionScoped {
export interface UIStoreAction<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData> extends UIStoreActionScoped<S, D> {
type: string
schema?: StoreSchemaType
required?: boolean
}

export type UIStoreActionListItemAdd = UIStoreActionListItemAddWithValue | UIStoreActionListItemAddWithSchema

export interface UIStoreActionListItemAddWithValue extends UIStoreAction {
export interface UIStoreActionListItemAddWithValue<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData> extends UIStoreAction<S, D> {
type: 'list-item-add'
itemValue: any
}

export interface UIStoreActionListItemAddWithSchema extends UIStoreAction {
export interface UIStoreActionListItemAddWithSchema<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData> extends UIStoreAction<S, D> {
type: 'list-item-add'
schema: StoreSchemaType
}

export interface UIStoreActionListItemDelete extends UIStoreAction {
export type UIStoreActionListItemAdd<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData> =
UIStoreActionListItemAddWithValue<S, D> |
UIStoreActionListItemAddWithSchema<S, D>

export interface UIStoreActionListItemDelete<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData> extends UIStoreAction<S, D> {
type: 'list-item-delete'
index: number
}

export interface UIStoreActionListItemMove extends UIStoreAction {
export interface UIStoreActionListItemMove<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData> extends UIStoreAction<S, D> {
type: 'list-item-move'
fromIndex: number
toIndex: number
}

export interface UIStoreActionUpdate<D extends UIStoreUpdaterData = UIStoreUpdaterData> extends UIStoreAction {
export interface UIStoreActionUpdate<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData> extends UIStoreAction<S, D> {
type: 'update'
updater: UIStoreUpdaterFn<D>
}

export interface UIStoreActionSet<D extends UIStoreUpdaterData = UIStoreUpdaterData> extends UIStoreAction {
export interface UIStoreActionSet<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData> extends UIStoreAction<S, D> {
type: 'set'
data: D
}

export type UIStoreActions<D extends UIStoreUpdaterData = UIStoreUpdaterData> =
UIStoreActionListItemAdd |
UIStoreActionListItemDelete |
UIStoreActionListItemMove |
UIStoreActionUpdate<D> |
UIStoreActionSet<D>
export type UIStoreActions<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData> =
UIStoreActionListItemAdd<S, D> |
UIStoreActionListItemDelete<S, D> |
UIStoreActionListItemMove<S, D> |
UIStoreActionUpdate<S, D> |
UIStoreActionSet<S, D>

4 changes: 2 additions & 2 deletions packages/ui-schema/src/storeUpdater/storeActionHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { UIStoreUpdaterFn } from '@ui-schema/ui-schema/UIStore'
import { UIStoreType, UIStoreUpdaterFn } from '@ui-schema/ui-schema/UIStore'
import { UIStoreActions, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStoreActions'
import { storeActionReducers } from '@ui-schema/ui-schema/storeUpdater/storeActionReducers'

/**
* @deprecated use `storeActionReducers` instead
*/
export const storeActionHandler = storeActionReducers as <D extends UIStoreUpdaterData = UIStoreUpdaterData, A extends UIStoreActions<D> = UIStoreActions<D>>(action: A) => UIStoreUpdaterFn<D> | D
export const storeActionHandler = storeActionReducers as <S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData, A = UIStoreActions<S, D>>(action: A) => UIStoreUpdaterFn<D> | D
6 changes: 4 additions & 2 deletions packages/ui-schema/src/storeUpdater/storeActionReducers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { UIStoreUpdaterFn } from '@ui-schema/ui-schema/UIStore'
import { UIStoreType, UIStoreUpdaterFn } from '@ui-schema/ui-schema/UIStore'
import { List, Map, OrderedMap } from 'immutable'
import { moveItem } from '@ui-schema/ui-schema/Utils/moveItem'
import { SchemaTypesType } from '@ui-schema/ui-schema/CommonTypings'
import { UIStoreActions, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStoreActions'

export const storeActionReducers = <D extends UIStoreUpdaterData = UIStoreUpdaterData, A extends UIStoreActions<D> = UIStoreActions<D>>(action: A): UIStoreUpdaterFn<D> | D => {
export const storeActionReducers = <S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData, A extends UIStoreActions<S, D> = UIStoreActions<S, D>>(
action: A
): UIStoreUpdaterFn<D> | D => {
switch (action.type) {
case 'list-item-add':
return ({value = List(), internal = Map(), ...r}) => {
Expand Down
15 changes: 9 additions & 6 deletions packages/ui-schema/src/storeUpdater/storeUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import {
UIStoreStateData, UIStoreUpdaterFn,
} from '@ui-schema/ui-schema/UIStore'
import { scopeUpdaterValues, scopeUpdaterInternals, scopeUpdaterValidity } from '@ui-schema/ui-schema/storeScopeUpdater'
import { UIStoreActions, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStoreActions'
import { UIStoreAction, UIStoreActions, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStoreActions'
import { storeActionReducers } from '@ui-schema/ui-schema/storeUpdater'

// todo: unify this type and the `setter` in `ScopeUpdaterMapType`
export type ScopeOnChangeHandler<S extends UIStoreType = UIStoreType, A extends UIStoreActions = UIStoreActions> = (
export type ScopeOnChangeHandler<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData, A = UIStoreActions<S, D>> = (
store: S,
storeKeys: StoreKeys,
newValue: any,
action?: A | undefined
) => S

export type ScopeUpdaterMapType<D extends UIStoreUpdaterData = UIStoreUpdaterData, A extends UIStoreActions = UIStoreActions> = {
export type ScopeUpdaterMapType<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData, A = UIStoreActions<S, D>> = {
[k in keyof D]: ({
setter: <S extends UIStoreType>(
store: S,
Expand Down Expand Up @@ -68,7 +68,7 @@ export const scopeUpdaterMapDefault: ScopeUpdaterMapType = {
},
}

export const createStoreUpdater = <S extends UIStoreType = UIStoreType, A extends UIStoreActions = UIStoreActions, D extends UIStoreUpdaterData = UIStoreUpdaterData, SM extends ScopeUpdaterMapType<D, A> = ScopeUpdaterMapType<D, A>>(
export const createStoreUpdater = <S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData, A extends UIStoreAction<S, D> = UIStoreActions<S, D>, SM extends ScopeUpdaterMapType<S, D, A> = ScopeUpdaterMapType<S, D, A>>(
actionReducers: (action: A) => UIStoreUpdaterFn<D> | D,
scopeUpdaterMap: SM,
) => {
Expand Down Expand Up @@ -129,8 +129,11 @@ export const createStoreUpdater = <S extends UIStoreType = UIStoreType, A extend
}

export const storeUpdater =
<S extends UIStoreType = UIStoreType, A extends UIStoreActions = UIStoreActions>(
<S extends UIStoreType = UIStoreType, D extends UIStoreUpdaterData = UIStoreUpdaterData, A extends UIStoreActions<S, D> = UIStoreActions<S, D>>(
actions: A[] | A
) => {
return createStoreUpdater<S, A>(storeActionReducers, scopeUpdaterMapDefault)(actions)
return createStoreUpdater<S, D, A>(
storeActionReducers as (action: A) => UIStoreUpdaterFn<D> | D,
scopeUpdaterMapDefault as ScopeUpdaterMapType<S, D, A>,
)(actions)
}

0 comments on commit 3c6e47e

Please sign in to comment.