import type { Studio } from '@theatre/studio/Studio' ; import delay from '@theatre/shared/utils/delay' ; import { original } from 'immer' ; import type Project from './Project' ; import type { OnDiskState } from './store/storeTypes' ; import globals from '@theatre/shared/globals' ; remarks export default async function initialiseProjectState ( studio , project , onDiskState ) { await delay ( 0 ) ; studio . transaction ( ( { drafts } ) => { const projectId = project . address . projectId ; drafts . ephemeral . coreByProject [ projectId ] = { lastExportedObject : null , loadingState : { type : 'loading' } , } ; drafts . ahistoric . coreByProject [ projectId ] = { ahistoricStuff : '' , } ; function useInitialState ( ) { drafts . ephemeral . coreByProject [ projectId ] . loadingState = { type : 'loaded' , } ; drafts . historic . coreByProject [ projectId ] = { sheetsById : { } , definitionVersion : globals . currentProjectStateDefinitionVersion , revisionHistory : [ ] , } ; } function useOnDiskState ( state ) { drafts . ephemeral . coreByProject [ projectId ] . loadingState = { type : 'loaded' , } ; drafts . historic . coreByProject [ projectId ] = state ; } function useBrowserState ( ) { drafts . ephemeral . coreByProject [ projectId ] . loadingState = { type : 'loaded' , } ; } function browserStateIsNotBasedOnDiskState ( onDiskState ) { drafts . ephemeral . coreByProject [ projectId ] . loadingState = { type : 'browserStateIsNotBasedOnDiskState' , onDiskState , } ; } const browserState = original ( drafts . historic ) ?. coreByProject [ project . address . projectId ] ; if ( ! browserState ) { if ( ! onDiskState ) { useInitialState ( ) ; } else { useOnDiskState ( onDiskState ) ; } } else { if ( ! onDiskState ) { useBrowserState ( ) ; } else { if ( browserState . revisionHistory . indexOf ( onDiskState . revisionHistory [ 0 ] ) == - 1 ) { browserStateIsNotBasedOnDiskState ( onDiskState ) ; } else { useBrowserState ( ) ; } } } } ) ; }