Skip to content

Commit

Permalink
Implement transactionApi.__experimental_forgetObject() and `...forg…
Browse files Browse the repository at this point in the history
…etSheet()`
  • Loading branch information
AriaMinaei committed Mar 8, 2023
1 parent 583dd9d commit 4ba4d2f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
50 changes: 48 additions & 2 deletions theatre/studio/src/TheatreStudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
__experimental_disblePlayPauseKeyboardShortcut,
__experimental_enablePlayPauseKeyboardShortcut,
} from './UIRoot/useKeyboardShortcuts'
import type TheatreSheetObject from '@theatre/core/sheetObjects/TheatreSheetObject'
import type TheatreSheet from '@theatre/core/sheets/TheatreSheet'

export interface ITransactionAPI {
/**
Expand Down Expand Up @@ -60,6 +62,21 @@ export interface ITransactionAPI {
* @param pointer - A pointer, like object.props
*/
unset<V>(pointer: Pointer<V>): void

/**
* EXPERIMENTAL API - this api may be removed without notice.
*
* Makes Theatre forget about this object. This means all the prop overrides and sequenced props
* will be reset, and the object won't show up in the exported state.
*/
__experimental_forgetObject(object: TheatreSheetObject): void

/**
* EXPERIMENTAL API - this api may be removed without notice.
*
* Makes Theatre forget about this sheet.
*/
__experimental_forgetSheet(sheet: TheatreSheet): void
}
/**
*
Expand Down Expand Up @@ -474,8 +491,37 @@ export default class TheatreStudio implements IStudio {
}

transaction(fn: (api: ITransactionAPI) => void): void {
return getStudio().transaction(({set, unset}) => {
return fn({set, unset})
return getStudio().transaction(({set, unset, stateEditors}) => {
const __experimental_forgetObject = (object: TheatreSheetObject) => {
if (!isSheetObjectPublicAPI(object)) {
throw new Error(
`object in transactionApi.__experimental_forgetObject(object) must be the return type of sheet.object(...)`,
)
}

stateEditors.coreByProject.historic.sheetsById.forgetObject(
object.address,
)
}

const __experimental_forgetSheet = (sheet: TheatreSheet) => {
if (!isSheetPublicAPI(sheet)) {
throw new Error(
`sheet in transactionApi.__experimental_forgetSheet(sheet) must be the return type of project.sheet()`,
)
}

stateEditors.coreByProject.historic.sheetsById.forgetSheet(
sheet.address,
)
}

return fn({
set,
unset,
__experimental_forgetObject,
__experimental_forgetSheet,
})
})
}

Expand Down
23 changes: 23 additions & 0 deletions theatre/studio/src/store/stateEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,29 @@ namespace stateEditors {
return sheetsById[p.sheetId]!
}

export function forgetObject(
p: WithoutSheetInstance<SheetObjectAddress>,
) {
const sheetState =
drafts().historic.coreByProject[p.projectId].sheetsById[p.sheetId]
if (!sheetState) return
delete sheetState.staticOverrides.byObject[p.objectKey]

const sequence = sheetState.sequence
if (!sequence) return
delete sequence.tracksByObject[p.objectKey]
}

export function forgetSheet(p: WithoutSheetInstance<SheetAddress>) {
const sheetState =
drafts().historic.coreByProject[p.projectId].sheetsById[p.sheetId]
if (sheetState) {
delete drafts().historic.coreByProject[p.projectId].sheetsById[
p.sheetId
]
}
}

export namespace sequence {
export function _ensure(
p: WithoutSheetInstance<SheetAddress>,
Expand Down

0 comments on commit 4ba4d2f

Please sign in to comment.