From 1ba89d76e1346809e3d8a0aaf1e0213d6154b186 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Sun, 27 Jun 2021 13:46:14 +0200 Subject: [PATCH] Fix: Moved privateAPI() to the core package --- theatre/core/src/CoreBundle.ts | 5 +++- theatre/{shared => core}/src/privateAPIs.ts | 4 --- theatre/core/src/projects/TheatreProject.ts | 2 +- theatre/core/src/sequences/TheatreSequence.ts | 2 +- .../src/sheetObjects/TheatreSheetObject.ts | 2 +- theatre/core/src/sheets/TheatreSheet.ts | 2 +- theatre/shared/src/testUtils.ts | 2 +- theatre/shared/src/utils/reactDataverse.ts | 2 +- theatre/studio/src/Scrub.ts | 2 +- theatre/studio/src/Studio.ts | 16 ++++++++++-- theatre/studio/src/StudioBundle.ts | 4 +-- theatre/studio/src/TheatreStudio.ts | 25 ++++++++----------- theatre/studio/src/index.ts | 5 ++-- 13 files changed, 40 insertions(+), 33 deletions(-) rename theatre/{shared => core}/src/privateAPIs.ts (84%) diff --git a/theatre/core/src/CoreBundle.ts b/theatre/core/src/CoreBundle.ts index 8b0e11e..6b34b6a 100644 --- a/theatre/core/src/CoreBundle.ts +++ b/theatre/core/src/CoreBundle.ts @@ -1,8 +1,10 @@ -import type Studio from '@theatre/studio/Studio' +import type {Studio} from '@theatre/studio/Studio' import projectsSingleton from './projects/projectsSingleton' +import {privateAPI} from './privateAPIs' export type CoreBits = { projectsP: typeof projectsSingleton.atom.pointer.projects + privateAPI: typeof privateAPI } export default class CoreBundle { @@ -24,6 +26,7 @@ export default class CoreBundle { this._studio = studio const bits: CoreBits = { projectsP: projectsSingleton.atom.pointer.projects, + privateAPI: privateAPI, } callback(bits) diff --git a/theatre/shared/src/privateAPIs.ts b/theatre/core/src/privateAPIs.ts similarity index 84% rename from theatre/shared/src/privateAPIs.ts rename to theatre/core/src/privateAPIs.ts index b486cf1..50e4ec2 100644 --- a/theatre/shared/src/privateAPIs.ts +++ b/theatre/core/src/privateAPIs.ts @@ -6,8 +6,6 @@ import type SheetObject from '@theatre/core/sheetObjects/SheetObject' import type {ISheetObject} from '@theatre/core/sheetObjects/TheatreSheetObject' import type Sheet from '@theatre/core/sheets/Sheet' import type {ISheet} from '@theatre/core/sheets/TheatreSheet' -import type Studio from '@theatre/studio/Studio' -import type {IStudio} from '@theatre/studio/TheatreStudio' import type {$IntentionalAny} from '@theatre/shared/utils/types' const publicAPIToPrivateAPIMap = new WeakMap() @@ -16,7 +14,6 @@ export function privateAPI(pub: IProject): Project export function privateAPI(pub: ISheet): Sheet export function privateAPI(pub: ISheetObject<$IntentionalAny>): SheetObject export function privateAPI(pub: ISequence): Sequence -export function privateAPI(pub: IStudio | IStudio['ui']): Studio export function privateAPI(pub: {}): unknown { return publicAPIToPrivateAPIMap.get(pub) } @@ -28,7 +25,6 @@ export function setPrivateAPI( pub: ISheetObject<$IntentionalAny>, priv: SheetObject, ): void -export function setPrivateAPI(pub: IStudio | IStudio['ui'], priv: Studio): void export function setPrivateAPI(pub: {}, priv: {}): void { publicAPIToPrivateAPIMap.set(pub, priv) } diff --git a/theatre/core/src/projects/TheatreProject.ts b/theatre/core/src/projects/TheatreProject.ts index c448b51..967eb9d 100644 --- a/theatre/core/src/projects/TheatreProject.ts +++ b/theatre/core/src/projects/TheatreProject.ts @@ -1,4 +1,4 @@ -import {privateAPI, setPrivateAPI} from '@theatre/shared/privateAPIs' +import {privateAPI, setPrivateAPI} from '@theatre/core/privateAPIs' import Project from '@theatre/core/projects/Project' import type {ISheet} from '@theatre/core/sheets/TheatreSheet' import type {ProjectAddress} from '@theatre/shared/utils/addresses' diff --git a/theatre/core/src/sequences/TheatreSequence.ts b/theatre/core/src/sequences/TheatreSequence.ts index a54e25a..4ac22ae 100644 --- a/theatre/core/src/sequences/TheatreSequence.ts +++ b/theatre/core/src/sequences/TheatreSequence.ts @@ -1,5 +1,5 @@ import logger from '@theatre/shared/logger' -import {privateAPI, setPrivateAPI} from '@theatre/shared/privateAPIs' +import {privateAPI, setPrivateAPI} from '@theatre/core/privateAPIs' import {defer} from '@theatre/shared/utils/defer' import type Sequence from './Sequence' import type {IPlaybackDirection, IPlaybackRange} from './Sequence' diff --git a/theatre/core/src/sheetObjects/TheatreSheetObject.ts b/theatre/core/src/sheetObjects/TheatreSheetObject.ts index bec4fb6..76e30b2 100644 --- a/theatre/core/src/sheetObjects/TheatreSheetObject.ts +++ b/theatre/core/src/sheetObjects/TheatreSheetObject.ts @@ -1,4 +1,4 @@ -import {privateAPI, setPrivateAPI} from '@theatre/shared/privateAPIs' +import {privateAPI, setPrivateAPI} from '@theatre/core/privateAPIs' import type {IProject} from '@theatre/core/projects/TheatreProject' import coreTicker from '@theatre/core/coreTicker' import type {ISheet} from '@theatre/core/sheets/TheatreSheet' diff --git a/theatre/core/src/sheets/TheatreSheet.ts b/theatre/core/src/sheets/TheatreSheet.ts index ac82b9c..4608276 100644 --- a/theatre/core/src/sheets/TheatreSheet.ts +++ b/theatre/core/src/sheets/TheatreSheet.ts @@ -1,4 +1,4 @@ -import {privateAPI, setPrivateAPI} from '@theatre/shared/privateAPIs' +import {privateAPI, setPrivateAPI} from '@theatre/core/privateAPIs' import type {IProject} from '@theatre/core/projects/TheatreProject' import type TheatreSequence from '@theatre/core/sequences/TheatreSequence' import type {ISequence} from '@theatre/core/sequences/TheatreSequence' diff --git a/theatre/shared/src/testUtils.ts b/theatre/shared/src/testUtils.ts index 0a0490e..1a5a663 100644 --- a/theatre/shared/src/testUtils.ts +++ b/theatre/shared/src/testUtils.ts @@ -1,7 +1,7 @@ /* eslint-disable no-restricted-syntax */ import '@theatre/studio' import {getProject} from '@theatre/core' -import {privateAPI} from '@theatre/shared/privateAPIs' +import {privateAPI} from '@theatre/core/privateAPIs' import type {ProjectState_Historic} from '@theatre/core/projects/store/storeTypes' import type {SheetState_Historic} from '@theatre/core/projects/store/types/SheetState_Historic' import * as t from '@theatre/shared/propTypes' diff --git a/theatre/shared/src/utils/reactDataverse.ts b/theatre/shared/src/utils/reactDataverse.ts index 362bb59..b1b3c57 100644 --- a/theatre/shared/src/utils/reactDataverse.ts +++ b/theatre/shared/src/utils/reactDataverse.ts @@ -160,7 +160,7 @@ function useDerivation(der: IDerivation, debugLabel?: string): T { }, [der]) useLayoutEffect(() => { - return () => { + return function onUnmount() { refs.current.unmounted = true removeFromQueue(refs.current.queueItem) } diff --git a/theatre/studio/src/Scrub.ts b/theatre/studio/src/Scrub.ts index a6480ac..49a4ce8 100644 --- a/theatre/studio/src/Scrub.ts +++ b/theatre/studio/src/Scrub.ts @@ -2,7 +2,7 @@ import forEachDeep from '@theatre/shared/utils/forEachDeep' import type {$FixMe} from '@theatre/shared/utils/types' import type {Pointer} from '@theatre/dataverse' import {getPointerParts} from '@theatre/dataverse' -import type Studio from './Studio' +import type {Studio} from './Studio' import type {CommitOrDiscard} from './StudioStore/StudioStore' import logger from '@theatre/shared/logger' import {isSheetObject} from '@theatre/shared/instanceTypes' diff --git a/theatre/studio/src/Studio.ts b/theatre/studio/src/Studio.ts index a851088..e73b527 100644 --- a/theatre/studio/src/Studio.ts +++ b/theatre/studio/src/Studio.ts @@ -13,8 +13,10 @@ import type {IStudio} from './TheatreStudio' import TheatreStudio from './TheatreStudio' import {nanoid} from 'nanoid/non-secure' import type Project from '@theatre/core/projects/Project' +import type {CoreBits} from '@theatre/core/CoreBundle' +import type {privateAPI} from '@theatre/core/privateAPIs' -export default class Studio { +export class Studio { readonly atomP: Pointer readonly ui!: UI readonly publicApi: IStudio @@ -26,6 +28,7 @@ export default class Studio { this._projectsProxy.pointer private readonly _store = new StudioStore() + private _corePrivateApi: typeof privateAPI | undefined constructor() { this.address = {studioId: nanoid(10)} @@ -59,7 +62,12 @@ export default class Studio { attachToProjects(projectsD.getValue()) } - setProjectsP(projectsP: Pointer>) { + setCoreBits(coreBits: CoreBits) { + this._corePrivateApi = coreBits.privateAPI + this._setProjectsP(coreBits.projectsP) + } + + private _setProjectsP(projectsP: Pointer>) { this._projectsProxy.setPointer(projectsP) } @@ -78,4 +86,8 @@ export default class Studio { __dev_startHistoryFromScratch(newHistoricPart: StudioHistoricState) { return this._store.__dev_startHistoryFromScratch(newHistoricPart) } + + get corePrivateAPI() { + return this._corePrivateApi + } } diff --git a/theatre/studio/src/StudioBundle.ts b/theatre/studio/src/StudioBundle.ts index dc709ff..5bb8b67 100644 --- a/theatre/studio/src/StudioBundle.ts +++ b/theatre/studio/src/StudioBundle.ts @@ -1,6 +1,6 @@ import type CoreBundle from '@theatre/core/CoreBundle' import type {CoreBits} from '@theatre/core/CoreBundle' -import type Studio from './Studio' +import type {Studio} from './Studio' export default class StudioBundle { private _coreBundle: undefined | CoreBundle @@ -22,6 +22,6 @@ export default class StudioBundle { coreBits = bits }) - this._studio.setProjectsP(coreBits.projectsP) + this._studio.setCoreBits(coreBits) } } diff --git a/theatre/studio/src/TheatreStudio.ts b/theatre/studio/src/TheatreStudio.ts index ab46967..3e331af 100644 --- a/theatre/studio/src/TheatreStudio.ts +++ b/theatre/studio/src/TheatreStudio.ts @@ -2,15 +2,15 @@ import type {ISheetObject} from '@theatre/core' import studioTicker from '@theatre/studio/studioTicker' import type {IDerivation, Pointer} from '@theatre/dataverse' import {prism} from '@theatre/dataverse' -import {privateAPI, setPrivateAPI} from '@theatre/shared/privateAPIs' import SimpleCache from '@theatre/shared/utils/SimpleCache' import type {VoidFn} from '@theatre/shared/utils/types' import type {IScrub} from '@theatre/studio/Scrub' -import type Studio from '@theatre/studio/Studio' +import type {Studio} from '@theatre/studio/Studio' import {isSheetObjectPublicAPI} from '@theatre/shared/instanceTypes' import {getOutlineSelection} from './selectors' import type SheetObject from '@theatre/core/sheetObjects/SheetObject' +import getStudio from './getStudio' export interface ITransactionAPI { set(pointer: Pointer, value: V): void @@ -39,19 +39,19 @@ export interface IStudio { export default class TheatreStudio implements IStudio { readonly ui = { show() { - privateAPI(this).ui.show() + getStudio().ui.show() }, hide() { - privateAPI(this).ui.hide() + getStudio().ui.hide() }, get showing(): boolean { - return privateAPI(this).ui._showing + return getStudio().ui._showing }, restore() { - privateAPI(this).ui.restore() + getStudio().ui.restore() }, } @@ -60,13 +60,10 @@ export default class TheatreStudio implements IStudio { /** * @internal */ - constructor(internals: Studio) { - setPrivateAPI(this, internals) - setPrivateAPI(this.ui, internals) - } + constructor(internals: Studio) {} transaction(fn: (api: ITransactionAPI) => void): void { - return privateAPI(this).transaction(({set, unset}) => { + return getStudio().transaction(({set, unset}) => { return fn({set, unset}) }) } @@ -88,9 +85,9 @@ export default class TheatreStudio implements IStudio { __experimental_setSelection(selection: Array): void { const sanitizedSelection = [...selection] .filter((s) => isSheetObjectPublicAPI(s)) - .map((s) => privateAPI(s)) + .map((s) => getStudio().corePrivateAPI!(s)) - privateAPI(this).transaction(({stateEditors}) => { + getStudio().transaction(({stateEditors}) => { stateEditors.studio.historic.panels.outline.selection.set( sanitizedSelection, ) @@ -106,6 +103,6 @@ export default class TheatreStudio implements IStudio { } scrub(): IScrub { - return privateAPI(this).scrub() + return getStudio().scrub() } } diff --git a/theatre/studio/src/index.ts b/theatre/studio/src/index.ts index 78e78ac..5b8f648 100644 --- a/theatre/studio/src/index.ts +++ b/theatre/studio/src/index.ts @@ -1,5 +1,5 @@ import {setStudio} from '@theatre/studio/getStudio' -import Studio from '@theatre/studio/Studio' +import {Studio} from '@theatre/studio/Studio' export type {IScrub} from '@theatre/studio/Scrub' export type {IStudio} from '@theatre/studio/TheatreStudio' import * as globalVariableNames from '@theatre/shared/globalVariableNames' @@ -8,6 +8,7 @@ import StudioBundle from './StudioBundle' import type CoreBundle from '@theatre/core/CoreBundle' const studioPrivateAPI = new Studio() +setStudio(studioPrivateAPI) export const studio = studioPrivateAPI.publicApi export default studio @@ -15,8 +16,6 @@ if (process.env.NODE_ENV !== 'test') { studio.ui.show() } -setStudio(studioPrivateAPI) - registerStudioBundle() function registerStudioBundle() {