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

View file

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