From e67ab4c0ec5f28bba721e09196b6f8cf3aaba373 Mon Sep 17 00:00:00 2001 From: Anne Haley Date: Tue, 27 Sep 2022 15:37:40 -0400 Subject: [PATCH] Load the next scan each time a new scan is viewed (includes into next experiment) (#597) --- web_client/src/store/index.ts | 90 +++++++++++++++++------------------ 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/web_client/src/store/index.ts b/web_client/src/store/index.ts index f4fe82d6..2ad534ef 100644 --- a/web_client/src/store/index.ts +++ b/web_client/src/store/index.ts @@ -29,6 +29,7 @@ Vue.use(Vuex); const fileCache = new Map(); const frameCache = new Map(); let readDataQueue = []; +const loadedData = []; const pendingFrameDownloads = new Set(); const poolSize = Math.floor(navigator.hardwareConcurrency / 2) || 2; let taskRunId = -1; @@ -201,32 +202,46 @@ function startReaderWorkerPool() { }); } -// cache frames associated with scans of current experiment -function checkLoadExperiment(oldValue, newValue) { - if ( - !newValue - || newValue === oldValue - ) { - return; +function queueLoadScan(scan, loadNext = false) { + // load all frames in target scan + if (!loadedData.includes(scan.id)) { + store.state.scanFrames[scan.id].forEach( + (frameId) => { + readDataQueue.push({ + experimentId: scan.experiment, + scanId: scan.id, + frame: store.state.frames[frameId], + }); + }, + ); + loadedData.push(scan.id); } - readDataQueue = []; - const newExperimentScans = store.state.experimentScans[newValue.id]; - newExperimentScans.forEach((scanId) => { - const scanFrames = store.state.scanFrames[scanId].map( - (frameId) => store.state.frames[frameId], - ); - scanFrames.forEach((frame) => { - readDataQueue.push({ - // TODO don't hardcode projectId - projectId: 1, - experimentId: newValue.id, - scanId, - frame, - }); - }); - }); - startReaderWorkerPool(); + // semi-recursive; we only recurse on the first call + // to queue up the next scan as well + // to prefetch further ahead, modify the recursion + if (loadNext) { + const scansInSameExperiment = store.state.experimentScans[scan.experiment]; + let nextScan; + if (scan.id === scansInSameExperiment[scansInSameExperiment.length - 1]) { + // load first scan in next experiment + const experimentIds = Object.keys(store.state.experimentScans); + const nextExperimentId = experimentIds[experimentIds.indexOf(scan.experiment) + 1]; + const nextExperimentScans = store.state.experimentScans[nextExperimentId]; + if (nextExperimentScans && nextExperimentScans.length > 0) { + nextScan = store.state.allScans[ + nextExperimentScans[0] + ]; + } + } else { + // load next scan in same experiment + nextScan = store.state.allScans[scansInSameExperiment[ + scansInSameExperiment.indexOf(scan.id) + 1 + ]]; + } + if (nextScan) queueLoadScan(nextScan); + startReaderWorkerPool(); + } } // get next frame (across experiments and scans) @@ -749,25 +764,11 @@ const { commit('setErrorLoadingFrame', false); const oldScan = getters.currentScan; const newScan = state.scans[frame.scan]; - const oldExperiment = getters.currentExperiment - ? getters.currentExperiment - : null; - const newExperimentId = state.scans[frame.scan].experiment; - const newExperiment = state.experiments[newExperimentId]; - - // Check if we should cancel the currently loading experiment - if ( - newExperiment - && oldExperiment - && newExperiment.id !== oldExperiment.id - && taskRunId >= 0 - ) { - state.workerPool.cancel(taskRunId); - pendingFrameDownloads.forEach(({ abortController }) => { - abortController.abort(); - }); - pendingFrameDownloads.clear(); - taskRunId = -1; + + if (newScan !== oldScan && newScan) { + queueLoadScan( + newScan, true, + ); } let newProxyManager = false; @@ -828,9 +829,6 @@ const { commit('setLoadingFrame', false); } - // If necessary, queue loading scans of new experiment - checkLoadExperiment(oldExperiment, newExperiment); - // check for window lock expiry if (state.windowLocked.lock) { const { currentViewData } = getters;