@@ -10,20 +10,20 @@ import {
1010 ErrorResult ,
1111 ImportDataSourcesResult ,
1212 asIntermediateResult ,
13+ StateFileSetupResult ,
1314} from '@/src/io/import/common' ;
14- import {
15- DataSource ,
16- ChunkSource ,
17- StateFileLeaf ,
18- } from '@/src/io/import/dataSource' ;
15+ import { DataSource , ChunkSource } from '@/src/io/import/dataSource' ;
1916import handleDicomFile from '@/src/io/import/processors/handleDicomFile' ;
2017import extractArchive from '@/src/io/import/processors/extractArchive' ;
2118import extractArchiveTarget from '@/src/io/import/processors/extractArchiveTarget' ;
2219import handleAmazonS3 from '@/src/io/import/processors/handleAmazonS3' ;
2320import handleGoogleCloudStorage from '@/src/io/import/processors/handleGoogleCloudStorage' ;
2421import importSingleFile from '@/src/io/import/processors/importSingleFile' ;
2522import handleRemoteManifest from '@/src/io/import/processors/remoteManifest' ;
26- import { restoreStateFile } from '@/src/io/import/processors/restoreStateFile' ;
23+ import {
24+ restoreStateFile ,
25+ completeStateFileRestore ,
26+ } from '@/src/io/import/processors/restoreStateFile' ;
2727import updateFileMimeType from '@/src/io/import/processors/updateFileMimeType' ;
2828import handleConfig from '@/src/io/import/processors/handleConfig' ;
2929import {
@@ -74,39 +74,18 @@ const applyConfigsPostState = (
7474 }
7575 } ) ;
7676
77- function findStateFileLeaf ( dataSource : DataSource ) : StateFileLeaf | undefined {
77+ function findStateFileLeaf ( dataSource : DataSource ) {
7878 let current : DataSource | undefined = dataSource ;
7979 while ( current ) {
8080 if ( current . stateFileLeaf ) return current . stateFileLeaf ;
8181 current = current . parent ;
8282 }
83- // For collections (DICOM volumes), check the first source's parent chain
8483 if ( dataSource . type === 'collection' && dataSource . sources . length > 0 ) {
8584 return findStateFileLeaf ( dataSource . sources [ 0 ] ) ;
8685 }
8786 return undefined ;
8887}
8988
90- async function handleStateFileResult (
91- result : LoadableResult ,
92- importContext : ImportContext
93- ) {
94- const stateLeaf = findStateFileLeaf ( result . dataSource ) ;
95- if ( stateLeaf && importContext . stateFileContext ) {
96- const ctx = importContext . stateFileContext ;
97- ctx . stateIDToStoreID . set ( stateLeaf . stateID , result . dataID ) ;
98-
99- // Phase 2: Immediately bind view to this data so user sees streaming
100- ctx . onLeafImported ( stateLeaf . stateID , result . dataID ) ;
101-
102- ctx . pendingLeafCount -- ;
103- if ( ctx . pendingLeafCount === 0 ) {
104- // Phase 3: Restore tools/segments after all data loaded
105- await ctx . onAllLeavesImported ( ) ;
106- }
107- }
108- }
109-
11089async function importDicomChunkSources ( sources : ChunkSource [ ] ) {
11190 if ( sources . length === 0 ) return [ ] ;
11291
@@ -180,6 +159,7 @@ export async function importDataSources(
180159
181160 const chunkSources : DataSource [ ] = [ ] ;
182161 const configResults : ConfigResult [ ] = [ ] ;
162+ const stateFileSetups : StateFileSetupResult [ ] = [ ] ;
183163 const results : ImportDataSourcesResult [ ] = [ ] ;
184164
185165 let queue = dataSources . map ( ( src ) => ( {
@@ -194,14 +174,16 @@ export async function importDataSources(
194174 queue = queue . filter ( ( _ , i ) => i !== index ) ;
195175
196176 switch ( result . type ) {
177+ case 'stateFileSetup' :
178+ stateFileSetups . push ( result ) ;
179+ // fallthrough to handle dataSources
197180 case 'intermediate' : {
198181 const [ chunks , otherSources ] = partition (
199182 ( ds ) => ds . type === 'chunk' ,
200183 result . dataSources
201184 ) ;
202185 chunkSources . push ( ...chunks ) ;
203186
204- // try loading intermediate results
205187 queue . push (
206188 ...otherSources . map ( ( src ) => ( {
207189 promise : evaluateChain ( src , handlers , importContext ) ,
@@ -220,11 +202,8 @@ export async function importDataSources(
220202 break ;
221203 case 'ok' :
222204 case 'error' :
223- results . push ( result ) ;
224- break ;
225205 case 'data' :
226206 results . push ( result ) ;
227- await handleStateFileResult ( result , importContext ) ;
228207 break ;
229208 default :
230209 throw new Error ( `Invalid result: ${ result } ` ) ;
@@ -243,10 +222,6 @@ export async function importDataSources(
243222 try {
244223 const dicomResults = await importDicomChunkSources ( dicomChunkSources ) ;
245224 results . push ( ...dicomResults ) ;
246- // Handle state file results for DICOM volumes
247- for ( const dicomResult of dicomResults ) {
248- await handleStateFileResult ( dicomResult , importContext ) ;
249- }
250225 } catch ( err ) {
251226 const errorSource =
252227 dicomChunkSources . length === 1
@@ -255,11 +230,27 @@ export async function importDataSources(
255230 results . push ( asErrorResult ( ensureError ( err ) , errorSource ) ) ;
256231 }
257232
258- // save data sources
259- useDatasetStore ( ) . addDataSources (
260- results . filter ( ( result ) : result is LoadableResult => result . type === 'data' )
233+ const loadableResults = results . filter (
234+ ( r ) : r is LoadableResult => r . type === 'data'
261235 ) ;
262236
237+ useDatasetStore ( ) . addDataSources ( loadableResults ) ;
238+
239+ for ( const setup of stateFileSetups ) {
240+ const stateIDToStoreID : Record < string , string > = { } ;
241+ for ( const loadable of loadableResults ) {
242+ const leaf = findStateFileLeaf ( loadable . dataSource ) ;
243+ if ( leaf ) {
244+ stateIDToStoreID [ leaf . stateID ] = loadable . dataID ;
245+ }
246+ }
247+ await completeStateFileRestore (
248+ setup . manifest ,
249+ setup . stateFiles ,
250+ stateIDToStoreID
251+ ) ;
252+ }
253+
263254 return results ;
264255}
265256
0 commit comments