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 {
IProjectConfig,
ISheetObject,
SheetObjectActionsConfig,
UnknownShorthandCompoundProps,
} from '@theatre/core'
import {val} from '@theatre/core'
import {getProject} from '@theatre/core'
import type {Pointer} from '@theatre/dataverse'
import {isPointer} from '@theatre/dataverse'
import type {IStudio} from '@theatre/studio'
import studio from '@theatre/studio'
import isEqual from 'lodash-es/isEqual'
import {useEffect, useMemo, useState, useRef} from 'react'
@ -46,7 +44,7 @@ export function initialize(state: IProjectConfig['state']) {
}
const allProps: Record<string, UnknownShorthandCompoundProps[]> = {}
const allActions: Record<string, SheetObjectActionsConfig[]> = {}
const allActions: Record<string, Record<string, () => void>[]> = {}
type Button = {
type: 'button'
@ -143,23 +141,8 @@ export function useControls<Config extends ControlsAndButtons>(
Object.fromEntries(
Object.entries(buttons).map(([key, value]) => [
`${folder ? `${folder}: ` : ''}${key}`,
(object: ISheetObject, studio: IStudio) => {
value
.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),
()
() => {
value.onClick()
},
]),
),
@ -179,7 +162,8 @@ export function useControls<Config extends ControlsAndButtons>(
() =>
sheet.object(panel, Object.assign({}, ...allProps[panel], props), {
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],
)
@ -191,7 +175,8 @@ export function useControls<Config extends ControlsAndButtons>(
// the very first values returned are not undefined
sheet.object(panel, Object.assign({}, ...allPanelProps), {
reconfigure: true,
actions: Object.assign({}, ...allPanelActions),
__actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION:
Object.assign({}, ...allPanelActions),
})
return () => {
@ -199,7 +184,8 @@ export function useControls<Config extends ControlsAndButtons>(
allActions[panel].splice(allPanelActions.indexOf(actions), 1)
sheet.object(panel, Object.assign({}, ...allPanelProps), {
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])

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.`)
}
}
export type {SheetObjectActionsConfig} from './sheets/TheatreSheet'

View file

@ -61,7 +61,7 @@ export default class SheetObjectTemplate {
readonly address: WithoutSheetInstance<SheetObjectAddress>
readonly type: 'Theatre_SheetObjectTemplate' = 'Theatre_SheetObjectTemplate'
protected _config: Atom<SheetObjectPropTypeConfig>
readonly _actions: Atom<SheetObjectActionsConfig>
readonly _temp_actions_atom: Atom<SheetObjectActionsConfig>
readonly _cache = new SimpleCache()
readonly project: Project
@ -73,12 +73,12 @@ export default class SheetObjectTemplate {
return this._config.pointer
}
get actions() {
return this._actions.get()
get _temp_actions() {
return this._temp_actions_atom.get()
}
get actionsPointer() {
return this._actions.pointer
get _temp_actionsPointer() {
return this._temp_actions_atom.pointer
}
constructor(
@ -86,11 +86,11 @@ export default class SheetObjectTemplate {
objectKey: ObjectAddressKey,
nativeObject: unknown,
config: SheetObjectPropTypeConfig,
actions: SheetObjectActionsConfig,
_temp_actions: SheetObjectActionsConfig,
) {
this.address = {...sheetTemplate.address, objectKey}
this._config = new Atom(config)
this._actions = new Atom(actions)
this._temp_actions_atom = new Atom(_temp_actions)
this.project = sheetTemplate.project
}
@ -107,8 +107,11 @@ export default class SheetObjectTemplate {
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,
options?: {
reconfigure?: boolean
actions?: SheetObjectActionsConfig
__actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION?: SheetObjectActionsConfig
},
): ISheetObject<Props>
@ -138,7 +138,10 @@ export default class TheatreSheet implements ISheet {
object<Props extends UnknownShorthandCompoundProps>(
key: string,
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> {
const internal = privateAPI(this)
const sanitizedPath = validateAndSanitiseSlashedPathOrThrow(
@ -157,6 +160,9 @@ export default class TheatreSheet implements ISheet {
*/
const nativeObject = null
const actions =
opts?.__actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION
if (existingObject) {
if (process.env.NODE_ENV !== 'production') {
const prevConfig = weakMapOfUnsanitizedProps.get(existingObject)
@ -179,8 +185,8 @@ export default class TheatreSheet implements ISheet {
}
}
if (opts?.actions) {
existingObject.template.setActions(opts.actions)
if (actions) {
existingObject.template._temp_setActions(actions)
}
return existingObject.publicApi as $IntentionalAny
@ -190,7 +196,7 @@ export default class TheatreSheet implements ISheet {
sanitizedPath as ObjectAddressKey,
nativeObject,
sanitizedConfig,
opts?.actions,
actions,
)
if (process.env.NODE_ENV !== 'production') {
weakMapOfUnsanitizedProps.set(object as $FixMe, config)

View file

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