Rename deleteObject() and related params

Since `sheet.deleteObject()` doesn't actually delete the values
of the props of that object, I decided to rename it to `detachObject()`.

Also, the param `override` sounded too similar to the concept of value overrides,
so I renamed it to `reconfigure`.
This commit is contained in:
Aria Minaei 2022-10-20 12:33:24 +02:00 committed by Aria
parent cb8fa2f20f
commit a8a9b5ef05
3 changed files with 28 additions and 13 deletions

View file

@ -135,7 +135,7 @@ Then you can use it in your JSX like any other editable component. Note the make
(value) => value.type, (value) => value.type,
), ),
), ),
{override: true}, {reconfigure: true},
) )
return return
} else { } else {
@ -212,7 +212,7 @@ Then you can use it in your JSX like any other editable component. Note the make
return () => { return () => {
untap() untap()
sheetObject.sheet.deleteObject(theatreKey) sheetObject.sheet.detachObject(theatreKey)
allRegisteredObjects.delete(sheetObject) allRegisteredObjects.delete(sheetObject)
editorStore.getState().removeEditable(storeKey) editorStore.getState().removeEditable(storeKey)
} }

View file

@ -89,7 +89,7 @@ export default class SheetObjectTemplate {
return new SheetObject(sheet, this, nativeObject) return new SheetObject(sheet, this, nativeObject)
} }
overrideConfig(config: SheetObjectPropTypeConfig) { reconfigure(config: SheetObjectPropTypeConfig) {
this._config.setState(config) this._config.setState(config)
} }

View file

@ -46,7 +46,7 @@ export interface ISheet {
* *
* @param key - Each object is identified by a key, which is a non-empty string * @param key - Each object is identified by a key, which is a non-empty string
* @param props - The props of the object. See examples * @param props - The props of the object. See examples
* @param options - TODO * @param options - (Optional) Provide `{reconfigure: true}` to reconfigure an existing object. Reac the example below for details.
* *
* @returns An Object * @returns An Object
* *
@ -65,20 +65,35 @@ export interface ISheet {
* // Create an object with nested props * // Create an object with nested props
* const obj = sheet.object("obj", {position: {x: 0, y: 0}}) * const obj = sheet.object("obj", {position: {x: 0, y: 0}})
* obj.value.position // {x: 0, y: 0} * obj.value.position // {x: 0, y: 0}
*
* // you can also reconfigure an existing object:
* const obj = sheet.object("obj", {foo: 0})
* console.log(object.value.foo) // prints 0
*
* const obj2 = sheet.object("obj", {bar: 0}, {reconfigure: true})
* console.log(object.value.foo) // prints undefined, since we've removed this prop via reconfiguring the object
* console.log(object.value.bar) // prints 0, since we've introduced this prop by reconfiguring the object
*
* assert(obj === obj2) // passes, because reconfiguring the object returns the same object
* ``` * ```
*/ */
object<Props extends UnknownShorthandCompoundProps>( object<Props extends UnknownShorthandCompoundProps>(
key: string, key: string,
props: Props, props: Props,
options?: {override?: boolean}, options?: {
reconfigure?: boolean
},
): ISheetObject<Props> ): ISheetObject<Props>
/** /**
* Deletes a previously created child object for the sheet * Detaches a previously created child object from the sheet.
* *
* @param key - Delete the child object created with this key * If you call `sheet.object(key)` again with the same `key`, the object's values of the object's
* props WILL NOT be reset to their initial values.
*
* @param key - The `key` of the object previously given to `sheet.object(key, ...)`.
*/ */
deleteObject(key: string): void detachObject(key: string): void
/** /**
* The Sequence of this Sheet * The Sequence of this Sheet
@ -105,7 +120,7 @@ export default class TheatreSheet implements ISheet {
object<Props extends UnknownShorthandCompoundProps>( object<Props extends UnknownShorthandCompoundProps>(
key: string, key: string,
config: Props, config: Props,
opts?: {override?: boolean}, opts?: {reconfigure?: boolean},
): ISheetObject<Props> { ): ISheetObject<Props> {
const internal = privateAPI(this) const internal = privateAPI(this)
const sanitizedPath = validateAndSanitiseSlashedPathOrThrow( const sanitizedPath = validateAndSanitiseSlashedPathOrThrow(
@ -129,9 +144,9 @@ export default class TheatreSheet implements ISheet {
const prevConfig = weakMapOfUnsanitizedProps.get(existingObject) const prevConfig = weakMapOfUnsanitizedProps.get(existingObject)
if (prevConfig) { if (prevConfig) {
if (!deepEqual(config, prevConfig)) { if (!deepEqual(config, prevConfig)) {
if (opts?.override === true) { if (opts?.reconfigure === true) {
const sanitizedConfig = compound(config) const sanitizedConfig = compound(config)
existingObject.template.overrideConfig(sanitizedConfig) existingObject.template.reconfigure(sanitizedConfig)
weakMapOfUnsanitizedProps.set(existingObject, config) weakMapOfUnsanitizedProps.set(existingObject, config)
return existingObject.publicApi as $IntentionalAny return existingObject.publicApi as $IntentionalAny
} else { } else {
@ -139,7 +154,7 @@ export default class TheatreSheet implements ISheet {
`You seem to have called sheet.object("${key}", config) twice, with different values for \`config\`. ` + `You seem to have called sheet.object("${key}", config) twice, with different values for \`config\`. ` +
`This is disallowed because changing the config of an object on the fly would make it difficult to reason about.\n\n` + `This is disallowed because changing the config of an object on the fly would make it difficult to reason about.\n\n` +
`You can fix this by either re-using the existing object, or calling sheet.object("${key}", config) with the same config.\n\n` + `You can fix this by either re-using the existing object, or calling sheet.object("${key}", config) with the same config.\n\n` +
`If you mean to override the object's config, set \`{override: true}\` in sheet.object("${key}", config, {override: true})`, `If you mean to reconfigure the object's config, set \`{reconfigure: true}\` in sheet.object("${key}", config, {reconfigure: true})`,
) )
} }
} }
@ -173,7 +188,7 @@ export default class TheatreSheet implements ISheet {
return {...privateAPI(this).address} return {...privateAPI(this).address}
} }
deleteObject(key: string) { detachObject(key: string) {
const internal = privateAPI(this) const internal = privateAPI(this)
const sanitizedPath = validateAndSanitiseSlashedPathOrThrow( const sanitizedPath = validateAndSanitiseSlashedPathOrThrow(
key, key,