Mark the actions api as unstable

This commit is contained in:
Aria Minaei 2023-01-22 20:35:42 +01:00
parent 164442a6ea
commit 415ec76942
5 changed files with 33 additions and 40 deletions

View file

@ -1,14 +1,12 @@
import type { import type {
IProjectConfig, IProjectConfig,
ISheetObject, ISheetObject,
SheetObjectActionsConfig,
UnknownShorthandCompoundProps, UnknownShorthandCompoundProps,
} from '@theatre/core' } from '@theatre/core'
import {val} from '@theatre/core' import {val} from '@theatre/core'
import {getProject} from '@theatre/core' import {getProject} from '@theatre/core'
import type {Pointer} from '@theatre/dataverse' import type {Pointer} from '@theatre/dataverse'
import {isPointer} from '@theatre/dataverse' import {isPointer} from '@theatre/dataverse'
import type {IStudio} from '@theatre/studio'
import studio from '@theatre/studio' import studio from '@theatre/studio'
import isEqual from 'lodash-es/isEqual' import isEqual from 'lodash-es/isEqual'
import {useEffect, useMemo, useState, useRef} from 'react' import {useEffect, useMemo, useState, useRef} from 'react'
@ -46,7 +44,7 @@ export function initialize(state: IProjectConfig['state']) {
} }
const allProps: Record<string, UnknownShorthandCompoundProps[]> = {} const allProps: Record<string, UnknownShorthandCompoundProps[]> = {}
const allActions: Record<string, SheetObjectActionsConfig[]> = {} const allActions: Record<string, Record<string, () => void>[]> = {}
type Button = { type Button = {
type: 'button' type: 'button'
@ -143,23 +141,8 @@ export function useControls<Config extends ControlsAndButtons>(
Object.fromEntries( Object.fromEntries(
Object.entries(buttons).map(([key, value]) => [ Object.entries(buttons).map(([key, value]) => [
`${folder ? `${folder}: ` : ''}${key}`, `${folder ? `${folder}: ` : ''}${key}`,
(object: ISheetObject, studio: IStudio) => { () => {
value value.onClick()
.onClick
// (path, value) => {
// // this is not ideal because it will create a separate undo level for each set call,
// // but this is the only thing that theatre's public API allows us to do.
// // Wrapping the whole thing in a transaction wouldn't work either because side effects
// // would be run twice.
// maybeTransaction((api) => {
// api.set(
// get(folder ? object.props[folder] : object.props, path),
// value,
// )
// })
// },
// (path) => get(folder ? object.value[folder] : object.value, path),
()
}, },
]), ]),
), ),
@ -179,7 +162,8 @@ export function useControls<Config extends ControlsAndButtons>(
() => () =>
sheet.object(panel, Object.assign({}, ...allProps[panel], props), { sheet.object(panel, Object.assign({}, ...allProps[panel], props), {
reconfigure: true, reconfigure: true,
actions: Object.assign({}, ...allActions[panel], actions), __actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION:
Object.assign({}, ...allActions[panel], actions),
}), }),
[panel, props, actions], [panel, props, actions],
) )
@ -191,7 +175,8 @@ export function useControls<Config extends ControlsAndButtons>(
// the very first values returned are not undefined // the very first values returned are not undefined
sheet.object(panel, Object.assign({}, ...allPanelProps), { sheet.object(panel, Object.assign({}, ...allPanelProps), {
reconfigure: true, reconfigure: true,
actions: Object.assign({}, ...allPanelActions), __actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION:
Object.assign({}, ...allPanelActions),
}) })
return () => { return () => {
@ -199,7 +184,8 @@ export function useControls<Config extends ControlsAndButtons>(
allActions[panel].splice(allPanelActions.indexOf(actions), 1) allActions[panel].splice(allPanelActions.indexOf(actions), 1)
sheet.object(panel, Object.assign({}, ...allPanelProps), { sheet.object(panel, Object.assign({}, ...allPanelProps), {
reconfigure: true, reconfigure: true,
actions: Object.assign({}, ...allPanelActions), __actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION:
Object.assign({}, ...allPanelActions),
}) })
} }
}, [props, actions, allPanelActions, allPanelProps, sheet, panel]) }, [props, actions, allPanelActions, allPanelProps, sheet, panel])

View file

@ -206,5 +206,3 @@ export function val<T>(pointer: PointerType<T>): T {
throw new Error(`Called val(p) where p is not a pointer.`) throw new Error(`Called val(p) where p is not a pointer.`)
} }
} }
export type {SheetObjectActionsConfig} from './sheets/TheatreSheet'

View file

@ -61,7 +61,7 @@ export default class SheetObjectTemplate {
readonly address: WithoutSheetInstance<SheetObjectAddress> readonly address: WithoutSheetInstance<SheetObjectAddress>
readonly type: 'Theatre_SheetObjectTemplate' = 'Theatre_SheetObjectTemplate' readonly type: 'Theatre_SheetObjectTemplate' = 'Theatre_SheetObjectTemplate'
protected _config: Atom<SheetObjectPropTypeConfig> protected _config: Atom<SheetObjectPropTypeConfig>
readonly _actions: Atom<SheetObjectActionsConfig> readonly _temp_actions_atom: Atom<SheetObjectActionsConfig>
readonly _cache = new SimpleCache() readonly _cache = new SimpleCache()
readonly project: Project readonly project: Project
@ -73,12 +73,12 @@ export default class SheetObjectTemplate {
return this._config.pointer return this._config.pointer
} }
get actions() { get _temp_actions() {
return this._actions.get() return this._temp_actions_atom.get()
} }
get actionsPointer() { get _temp_actionsPointer() {
return this._actions.pointer return this._temp_actions_atom.pointer
} }
constructor( constructor(
@ -86,11 +86,11 @@ export default class SheetObjectTemplate {
objectKey: ObjectAddressKey, objectKey: ObjectAddressKey,
nativeObject: unknown, nativeObject: unknown,
config: SheetObjectPropTypeConfig, config: SheetObjectPropTypeConfig,
actions: SheetObjectActionsConfig, _temp_actions: SheetObjectActionsConfig,
) { ) {
this.address = {...sheetTemplate.address, objectKey} this.address = {...sheetTemplate.address, objectKey}
this._config = new Atom(config) this._config = new Atom(config)
this._actions = new Atom(actions) this._temp_actions_atom = new Atom(_temp_actions)
this.project = sheetTemplate.project this.project = sheetTemplate.project
} }
@ -107,8 +107,11 @@ export default class SheetObjectTemplate {
this._config.set(config) this._config.set(config)
} }
setActions(actions: SheetObjectActionsConfig) { /**
this._actions.set(actions) * The `actions` api is temporary until we implement events.
*/
_temp_setActions(actions: SheetObjectActionsConfig) {
this._temp_actions_atom.set(actions)
} }
/** /**

View file

@ -99,7 +99,7 @@ export interface ISheet {
props: Props, props: Props,
options?: { options?: {
reconfigure?: boolean reconfigure?: boolean
actions?: SheetObjectActionsConfig __actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION?: SheetObjectActionsConfig
}, },
): ISheetObject<Props> ): ISheetObject<Props>
@ -138,7 +138,10 @@ export default class TheatreSheet implements ISheet {
object<Props extends UnknownShorthandCompoundProps>( object<Props extends UnknownShorthandCompoundProps>(
key: string, key: string,
config: Props, config: Props,
opts?: {reconfigure?: boolean; actions?: SheetObjectActionsConfig}, opts?: {
reconfigure?: boolean
__actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION?: SheetObjectActionsConfig
},
): ISheetObject<Props> { ): ISheetObject<Props> {
const internal = privateAPI(this) const internal = privateAPI(this)
const sanitizedPath = validateAndSanitiseSlashedPathOrThrow( const sanitizedPath = validateAndSanitiseSlashedPathOrThrow(
@ -157,6 +160,9 @@ export default class TheatreSheet implements ISheet {
*/ */
const nativeObject = null const nativeObject = null
const actions =
opts?.__actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION
if (existingObject) { if (existingObject) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const prevConfig = weakMapOfUnsanitizedProps.get(existingObject) const prevConfig = weakMapOfUnsanitizedProps.get(existingObject)
@ -179,8 +185,8 @@ export default class TheatreSheet implements ISheet {
} }
} }
if (opts?.actions) { if (actions) {
existingObject.template.setActions(opts.actions) existingObject.template._temp_setActions(actions)
} }
return existingObject.publicApi as $IntentionalAny return existingObject.publicApi as $IntentionalAny
@ -190,7 +196,7 @@ export default class TheatreSheet implements ISheet {
sanitizedPath as ObjectAddressKey, sanitizedPath as ObjectAddressKey,
nativeObject, nativeObject,
sanitizedConfig, sanitizedConfig,
opts?.actions, actions,
) )
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
weakMapOfUnsanitizedProps.set(object as $FixMe, config) weakMapOfUnsanitizedProps.set(object as $FixMe, config)

View file

@ -43,7 +43,7 @@ const ObjectDetails: React.FC<{
}> = ({objects}) => { }> = ({objects}) => {
const obj = objects[0] const obj = objects[0]
const config = useVal(obj.template.configPointer) const config = useVal(obj.template.configPointer)
const actions = useVal(obj.template.actionsPointer) const actions = useVal(obj.template._temp_actionsPointer)
return ( return (
<> <>