From 151fcce298239ce20d5d5d0a1b2df54c857d9aef Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Tue, 18 Oct 2022 10:16:47 +0200 Subject: [PATCH] Implement `Sheet.deleteObject()` Co-authored-by: Elliot --- theatre/core/src/sheets/Sheet.ts | 8 ++++++++ theatre/core/src/sheets/TheatreSheet.ts | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/theatre/core/src/sheets/Sheet.ts b/theatre/core/src/sheets/Sheet.ts index 9d8ead8..1c2dab6 100644 --- a/theatre/core/src/sheets/Sheet.ts +++ b/theatre/core/src/sheets/Sheet.ts @@ -73,6 +73,14 @@ export default class Sheet { return this._objects.getState()[key] } + deleteObject(objectKey: ObjectAddressKey) { + this._objects.reduceState([], (state) => { + const newState = {...state} + delete newState[objectKey] + return newState + }) + } + getSequence(): Sequence { if (!this._sequence) { const lengthD = valueDerivation( diff --git a/theatre/core/src/sheets/TheatreSheet.ts b/theatre/core/src/sheets/TheatreSheet.ts index e5d8f4b..10fbb6a 100644 --- a/theatre/core/src/sheets/TheatreSheet.ts +++ b/theatre/core/src/sheets/TheatreSheet.ts @@ -72,6 +72,13 @@ export interface ISheet { options?: {override?: boolean}, ): ISheetObject + /** + * Deletes a previously created child object for the sheet + * + * @param key - Delete the child object created with this key + */ + deleteObject(key: string): void + /** * The Sequence of this Sheet */ @@ -164,6 +171,22 @@ export default class TheatreSheet implements ISheet { get address(): SheetAddress { return {...privateAPI(this).address} } + + deleteObject(key: string) { + const internal = privateAPI(this) + const sanitizedPath = validateAndSanitiseSlashedPathOrThrow( + key, + `sheet.deleteObject("${key}")`, + ) as ObjectAddressKey + + const obj = internal.getObject(sanitizedPath) + if (!obj) { + console.warn(`Object key "${sanitizedPath}" does not exist.`) + return + } + + internal.deleteObject(sanitizedPath as ObjectAddressKey) + } } const validateSequenceNameOrThrow = (value: string) => {