Refactored getArrayOfValidSequenceTracks()

This commit is contained in:
Aria Minaei 2021-11-14 17:08:03 +01:00
parent e479d32f3b
commit 67b8a708dc
3 changed files with 56 additions and 44 deletions

View file

@ -84,7 +84,6 @@ export const compound = <Props extends IShorthandCompoundProps>(
label: opts?.label, label: opts?.label,
sanitize: opts?.sanitize, sanitize: opts?.sanitize,
interpolate: opts?.interpolate, interpolate: opts?.interpolate,
isScalar: false,
} }
} }
@ -258,7 +257,6 @@ export const boolean = (
valueType: null as $IntentionalAny, valueType: null as $IntentionalAny,
[propTypeSymbol]: 'TheatrePropType', [propTypeSymbol]: 'TheatrePropType',
label: opts?.label, label: opts?.label,
isScalar: false,
sanitize(value: unknown) { sanitize(value: unknown) {
if (opts?.sanitize) return opts.sanitize(value) if (opts?.sanitize) return opts.sanitize(value)
return typeof value === 'boolean' ? value : undefined return typeof value === 'boolean' ? value : undefined
@ -311,7 +309,6 @@ export const string = (
valueType: null as $IntentionalAny, valueType: null as $IntentionalAny,
[propTypeSymbol]: 'TheatrePropType', [propTypeSymbol]: 'TheatrePropType',
label: opts?.label, label: opts?.label,
isScalar: false,
sanitize(value: unknown) { sanitize(value: unknown) {
if (opts?.sanitize) return opts.sanitize(value) if (opts?.sanitize) return opts.sanitize(value)
return typeof value === 'string' ? value : undefined return typeof value === 'string' ? value : undefined
@ -366,7 +363,6 @@ export function stringLiteral<Opts extends {[key in string]: string}>(
valueType: null as $IntentionalAny, valueType: null as $IntentionalAny,
as: opts?.as ?? 'menu', as: opts?.as ?? 'menu',
label: opts?.label, label: opts?.label,
isScalar: false,
sanitize(value: unknown) { sanitize(value: unknown) {
if (opts?.sanitize) return opts.sanitize(value) if (opts?.sanitize) return opts.sanitize(value)
return typeof value === 'string' && Object.keys(options).includes(value) return typeof value === 'string' && Object.keys(options).includes(value)
@ -387,7 +383,7 @@ interface IBasePropType<ValueType, PropTypes = ValueType> {
valueType: ValueType valueType: ValueType
[propTypeSymbol]: 'TheatrePropType' [propTypeSymbol]: 'TheatrePropType'
label: string | undefined label: string | undefined
isScalar: boolean isScalar?: true
sanitize?: Sanitizer<PropTypes> sanitize?: Sanitizer<PropTypes>
interpolate?: Interpolator<PropTypes> interpolate?: Interpolator<PropTypes>
} }

View file

@ -19,15 +19,12 @@ import type {
} from '@theatre/shared/utils/types' } from '@theatre/shared/utils/types'
import type {IDerivation, Pointer} from '@theatre/dataverse' import type {IDerivation, Pointer} from '@theatre/dataverse'
import {Atom, getPointerParts, prism, val} from '@theatre/dataverse' import {Atom, getPointerParts, prism, val} from '@theatre/dataverse'
import get from 'lodash-es/get'
import set from 'lodash-es/set' import set from 'lodash-es/set'
import getPropDefaultsOfSheetObject from './getPropDefaultsOfSheetObject' import getPropDefaultsOfSheetObject from './getPropDefaultsOfSheetObject'
import SheetObject from './SheetObject' import SheetObject from './SheetObject'
import logger from '@theatre/shared/logger' import logger from '@theatre/shared/logger'
import type { import type {PropTypeConfig_Compound} from '@theatre/core/propTypes'
PropTypeConfig, import {getPropConfigByPath} from '@theatre/shared/propTypes/utils'
PropTypeConfig_Compound,
} from '@theatre/core/propTypes'
export type IPropPathToTrackIdTree = { export type IPropPathToTrackIdTree = {
[key in string]?: SequenceTrackId | IPropPathToTrackIdTree [key in string]?: SequenceTrackId | IPropPathToTrackIdTree
@ -116,8 +113,6 @@ export default class SheetObjectTemplate {
> { > {
return this._cache.get('getArrayOfValidSequenceTracks', () => return this._cache.get('getArrayOfValidSequenceTracks', () =>
prism((): Array<{pathToProp: PathToProp; trackId: SequenceTrackId}> => { prism((): Array<{pathToProp: PathToProp; trackId: SequenceTrackId}> => {
const defaults = val(this.getDefaultValues())
const pointerToSheetState = const pointerToSheetState =
this.project.pointers.historic.sheetsById[this.address.sheetId] this.project.pointers.historic.sheetsById[this.address.sheetId]
@ -131,40 +126,20 @@ export default class SheetObjectTemplate {
trackId: SequenceTrackId trackId: SequenceTrackId
}> = [] }> = []
if (trackIdByPropPath) { if (!trackIdByPropPath) return emptyArray as $IntentionalAny
for (const [pathToPropInString, trackId] of Object.entries(
trackIdByPropPath,
)) {
let pathToProp
try {
pathToProp = JSON.parse(pathToPropInString)
} catch (e) {
logger.warn(
`property ${JSON.stringify(
pathToPropInString,
)} cannot be parsed. Skipping.`,
)
continue
}
const propConfig = get(this.config.props, pathToProp) as const _entries = Object.entries(trackIdByPropPath)
| PropTypeConfig for (const [pathToPropInString, trackId] of _entries) {
| undefined const pathToProp = parsePathToProp(pathToPropInString)
const defaultValue = get(defaults, pathToProp) if (!pathToProp) continue
if ( const propConfig = getPropConfigByPath(this.config, pathToProp)
typeof defaultValue !== 'number' &&
(!propConfig?.sanitize || !propConfig.interpolate) if (!propConfig || !propConfig?.sanitize || !propConfig.interpolate)
) {
//@checking defaultValue because tests don't provide prop config, and fail if this is omitted.
continue continue
}
arrayOfIds.push({pathToProp, trackId: trackId!}) arrayOfIds.push({pathToProp, trackId: trackId!})
} }
} else {
return emptyArray as $IntentionalAny
}
if (arrayOfIds.length === 0) { if (arrayOfIds.length === 0) {
return emptyArray as $IntentionalAny return emptyArray as $IntentionalAny
@ -207,3 +182,19 @@ export default class SheetObjectTemplate {
return defaultsAtPath as $FixMe return defaultsAtPath as $FixMe
} }
} }
function parsePathToProp(
pathToPropInString: string,
): undefined | Array<string | number> {
try {
const pathToProp = JSON.parse(pathToPropInString)
return pathToProp
} catch (e) {
logger.warn(
`property ${JSON.stringify(
pathToPropInString,
)} cannot be parsed. Skipping.`,
)
return undefined
}
}

View file

@ -1,5 +1,30 @@
import type {PropTypeConfig} from '@theatre/core/propTypes' import type {
PropTypeConfig,
PropTypeConfig_Compound,
PropTypeConfig_Enum,
} from '@theatre/core/propTypes'
import type {PathToProp} from '@theatre/shared/utils/addresses'
import type {$IntentionalAny} from '@theatre/shared/utils/types'
export function isPropConfigComposite(c: PropTypeConfig): boolean { export function isPropConfigComposite(
c: PropTypeConfig,
): c is PropTypeConfig_Compound<{}> | PropTypeConfig_Enum {
return c.type === 'compound' || c.type === 'enum' return c.type === 'compound' || c.type === 'enum'
} }
export function getPropConfigByPath(
parentConf: PropTypeConfig | undefined,
path: PathToProp,
): undefined | PropTypeConfig {
if (!parentConf) return undefined
const [key, ...rest] = path
if (typeof key === 'undefined') return parentConf
if (!isPropConfigComposite(parentConf)) return undefined
const sub =
parentConf.type === 'enum'
? parentConf.cases[key]
: (parentConf as $IntentionalAny).props[key]
return getPropConfigByPath(sub, rest)
}