More progress on shorthand types
This commit is contained in:
parent
aefb769855
commit
f6cf3711f4
14 changed files with 109 additions and 103 deletions
|
@ -12,13 +12,13 @@ import {propTypeSymbol} from './internals'
|
|||
* Usage:
|
||||
* ```ts
|
||||
* // the root prop type of an object is always a compound
|
||||
* const props = t.compound({
|
||||
* const props = {
|
||||
* // compounds can be nested
|
||||
* position: t.compound({
|
||||
* x: t.number(0),
|
||||
* y: t.number(0)
|
||||
* })
|
||||
* })
|
||||
* }
|
||||
*
|
||||
* const obj = sheet.obj('key', props)
|
||||
* console.log(obj.value) // {position: {x: 10.3, y: -1}}
|
||||
|
|
|
@ -28,17 +28,18 @@ export type IShorthandCompoundProps = {
|
|||
[K in string]: IShorthandProp
|
||||
}
|
||||
|
||||
type ShorthandPropToLonghandProp<P extends IShorthandProp> = P extends string
|
||||
? PropTypeConfig_String
|
||||
: P extends number
|
||||
? PropTypeConfig_Number
|
||||
: P extends boolean
|
||||
? PropTypeConfig_Boolean
|
||||
: P extends PropTypeConfig
|
||||
? P
|
||||
: P extends IShorthandCompoundProps
|
||||
? PropTypeConfig_Compound<ShorthandCompoundPropsToLonghandCompoundProps<P>>
|
||||
: never
|
||||
export type ShorthandPropToLonghandProp<P extends IShorthandProp> =
|
||||
P extends string
|
||||
? PropTypeConfig_String
|
||||
: P extends number
|
||||
? PropTypeConfig_Number
|
||||
: P extends boolean
|
||||
? PropTypeConfig_Boolean
|
||||
: P extends PropTypeConfig
|
||||
? P
|
||||
: P extends IShorthandCompoundProps
|
||||
? PropTypeConfig_Compound<ShorthandCompoundPropsToLonghandCompoundProps<P>>
|
||||
: never
|
||||
|
||||
export type ShorthandCompoundPropsToLonghandCompoundProps<
|
||||
P extends IShorthandCompoundProps,
|
||||
|
|
|
@ -6,37 +6,37 @@ import type {SheetObjectAddress} from '@theatre/shared/utils/addresses'
|
|||
import SimpleCache from '@theatre/shared/utils/SimpleCache'
|
||||
import type {
|
||||
$FixMe,
|
||||
$IntentionalAny,
|
||||
DeepPartialOfSerializableValue,
|
||||
VoidFn,
|
||||
} from '@theatre/shared/utils/types'
|
||||
import type {IDerivation, Pointer} from '@theatre/dataverse'
|
||||
import {prism, val} from '@theatre/dataverse'
|
||||
import type {PropTypeConfig_Compound} from '@theatre/core/propTypes'
|
||||
import type SheetObject from './SheetObject'
|
||||
import type {
|
||||
IShorthandCompoundProps,
|
||||
ShorthandPropToLonghandProp,
|
||||
} from '@theatre/core/propTypes/internals'
|
||||
|
||||
export interface ISheetObject<
|
||||
Props extends PropTypeConfig_Compound<$IntentionalAny> = PropTypeConfig_Compound<$IntentionalAny>,
|
||||
> {
|
||||
export interface ISheetObject<Props extends IShorthandCompoundProps = {}> {
|
||||
readonly type: 'Theatre_SheetObject_PublicAPI'
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
readonly value: Props['valueType']
|
||||
readonly props: Pointer<Props['valueType']>
|
||||
readonly value: ShorthandPropToLonghandProp<Props>['valueType']
|
||||
readonly props: Pointer<this['value']>
|
||||
|
||||
readonly sheet: ISheet
|
||||
readonly project: IProject
|
||||
readonly address: SheetObjectAddress
|
||||
|
||||
onValuesChange(fn: (values: Props['valueType']) => void): VoidFn
|
||||
onValuesChange(fn: (values: this['value']) => void): VoidFn
|
||||
// prettier-ignore
|
||||
set initialValue(value: DeepPartialOfSerializableValue<Props['valueType']>)
|
||||
set initialValue(value: DeepPartialOfSerializableValue<this['value']>)
|
||||
}
|
||||
|
||||
export default class TheatreSheetObject<
|
||||
Props extends PropTypeConfig_Compound<$IntentionalAny>,
|
||||
Props extends IShorthandCompoundProps = {},
|
||||
> implements ISheetObject<Props>
|
||||
{
|
||||
get type(): 'Theatre_SheetObject_PublicAPI' {
|
||||
|
@ -51,7 +51,7 @@ export default class TheatreSheetObject<
|
|||
setPrivateAPI(this, internals)
|
||||
}
|
||||
|
||||
get props(): Pointer<Props['valueType']> {
|
||||
get props(): Pointer<this['value']> {
|
||||
return privateAPI(this).propsP as $FixMe
|
||||
}
|
||||
|
||||
|
@ -67,7 +67,7 @@ export default class TheatreSheetObject<
|
|||
return {...privateAPI(this).address}
|
||||
}
|
||||
|
||||
private _valuesDerivation(): IDerivation<Props['valueType']> {
|
||||
private _valuesDerivation(): IDerivation<this['value']> {
|
||||
return this._cache.get('onValuesChangeDerivation', () => {
|
||||
const sheetObject = privateAPI(this)
|
||||
const d: IDerivation<Props> = prism(() => {
|
||||
|
@ -77,15 +77,15 @@ export default class TheatreSheetObject<
|
|||
})
|
||||
}
|
||||
|
||||
onValuesChange(fn: (values: Props['valueType']) => void): VoidFn {
|
||||
onValuesChange(fn: (values: this['value']) => void): VoidFn {
|
||||
return this._valuesDerivation().tapImmediate(coreTicker, fn)
|
||||
}
|
||||
|
||||
get value(): Props['valueType'] {
|
||||
get value(): ShorthandPropToLonghandProp<Props>['valueType'] {
|
||||
return this._valuesDerivation().getValue()
|
||||
}
|
||||
|
||||
set initialValue(val: DeepPartialOfSerializableValue<Props['valueType']>) {
|
||||
set initialValue(val: DeepPartialOfSerializableValue<this['value']>) {
|
||||
privateAPI(this).setInitialValue(val)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import type {IProject} from '@theatre/core/projects/TheatreProject'
|
|||
import type TheatreSequence from '@theatre/core/sequences/TheatreSequence'
|
||||
import type {ISequence} from '@theatre/core/sequences/TheatreSequence'
|
||||
import type {PropTypeConfig_Compound} from '@theatre/core/propTypes'
|
||||
import {compound} from '@theatre/core/propTypes'
|
||||
import type {ISheetObject} from '@theatre/core/sheetObjects/TheatreSheetObject'
|
||||
import type Sheet from '@theatre/core/sheets/Sheet'
|
||||
import type {SheetAddress} from '@theatre/shared/utils/addresses'
|
||||
|
@ -11,6 +12,7 @@ import {validateAndSanitiseSlashedPathOrThrow} from '@theatre/shared/utils/slash
|
|||
import type {$IntentionalAny} from '@theatre/shared/utils/types'
|
||||
import userReadableTypeOfValue from '@theatre/shared/utils/userReadableTypeOfValue'
|
||||
import deepEqual from 'fast-deep-equal'
|
||||
import type {IShorthandCompoundProps} from '@theatre/core/propTypes/internals'
|
||||
|
||||
export type SheetObjectConfig<
|
||||
Props extends PropTypeConfig_Compound<$IntentionalAny>,
|
||||
|
@ -21,14 +23,16 @@ export interface ISheet {
|
|||
readonly project: IProject
|
||||
readonly address: SheetAddress
|
||||
|
||||
object<Props extends PropTypeConfig_Compound<$IntentionalAny>>(
|
||||
object<Props extends IShorthandCompoundProps>(
|
||||
key: string,
|
||||
config: SheetObjectConfig<Props>,
|
||||
config: Props,
|
||||
): ISheetObject<Props>
|
||||
|
||||
readonly sequence: ISequence
|
||||
}
|
||||
|
||||
const weakMapOfUnsanitizedProps = new WeakMap()
|
||||
|
||||
export default class TheatreSheet implements ISheet {
|
||||
get type(): 'Theatre_Sheet_PublicAPI' {
|
||||
return 'Theatre_Sheet_PublicAPI'
|
||||
|
@ -40,9 +44,9 @@ export default class TheatreSheet implements ISheet {
|
|||
setPrivateAPI(this, sheet)
|
||||
}
|
||||
|
||||
object<Props extends PropTypeConfig_Compound<$IntentionalAny>>(
|
||||
object<Props extends IShorthandCompoundProps>(
|
||||
key: string,
|
||||
config: SheetObjectConfig<Props>,
|
||||
config: Props,
|
||||
): ISheetObject<Props> {
|
||||
const internal = privateAPI(this)
|
||||
const sanitizedPath = validateAndSanitiseSlashedPathOrThrow(
|
||||
|
@ -67,7 +71,11 @@ export default class TheatreSheet implements ISheet {
|
|||
|
||||
return existingObject.publicApi as $IntentionalAny
|
||||
} else {
|
||||
const object = internal.createObject(sanitizedPath, nativeObject, config)
|
||||
const object = internal.createObject(
|
||||
sanitizedPath,
|
||||
nativeObject,
|
||||
compound(config),
|
||||
)
|
||||
return object.publicApi as $IntentionalAny
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,16 +31,13 @@ export async function setupTestSheet(sheetState: SheetState_Historic) {
|
|||
ticker.tick()
|
||||
await project.ready
|
||||
const sheetPublicAPI = project.sheet('Sheet')
|
||||
const objPublicAPI = sheetPublicAPI.object(
|
||||
'obj',
|
||||
t.compound({
|
||||
position: {
|
||||
x: 0,
|
||||
y: t.number(1),
|
||||
z: t.number(2),
|
||||
},
|
||||
}),
|
||||
)
|
||||
const objPublicAPI = sheetPublicAPI.object('obj', {
|
||||
position: {
|
||||
x: 0,
|
||||
y: t.number(1),
|
||||
z: t.number(2),
|
||||
},
|
||||
})
|
||||
|
||||
const obj = privateAPI(objPublicAPI)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue