All files / dicomImageLoader/src/imageLoader/wadouri dataSetCacheManager.ts

45.76% Statements 27/59
33.33% Branches 7/21
58.33% Functions 7/12
45.76% Lines 27/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229                                                          428x   428x       180x           231286x         231286x 3960x     231286x                                                           180x                 180x               180x     180x   180x     180x             180x                             180x         180x 180x                       180x           180x       180x 180x   180x                 180x                   180x   180x   180x                                             180x                                                
import type { DataSet } from 'dicom-parser';
import * as dicomParser from 'dicom-parser';
import { xhrRequest } from '../internal/index';
import dataSetFromPartialContent from './dataset-from-partial-content';
import type {
  LoadRequestFunction,
  DICOMLoaderDataSetWithFetchMore,
} from '../../types';
import { combineFrameInstanceDataset } from './combineFrameInstanceDataset';
import multiframeDataset from './retrieveMultiframeDataset';
import { loadedDataSets, purgeLoadedDataSets } from './loadedDataSets';
import { eventTarget, triggerEvent } from '@cornerstonejs/core';
 
export interface CornerstoneWadoLoaderCacheManagerInfoResponse {
  cacheSizeInBytes: number;
  numberOfDataSetsCached: number;
}
 
export interface CornerstoneWadoLoaderCachedPromise
  extends Promise<DataSet | DICOMLoaderDataSetWithFetchMore> {
  cacheCount?: number;
}
 
/**
 * This object supports loading of DICOM P10 dataset from a uri and caching it so it can be accessed
 * by the caller.  This allows a caller to access the datasets without having to go through cornerstone's
 * image loader mechanism.  One reason a caller may need to do this is to determine the number of frames
 * in a multiframe sop instance so it can create the imageId's correctly.
 */
let cacheSizeInBytes = 0;
 
let promises: Record<string, CornerstoneWadoLoaderCachedPromise> = {};
 
// returns true if the wadouri for the specified index has been loaded
function isLoaded(uri: string): boolean {
  return loadedDataSets[uri] !== undefined;
}
 
function get(uri: string): DataSet {
  let dataSet;
 
  Iif (uri.includes('&frame=')) {
    const { frame, dataSet: multiframeDataSet } =
      multiframeDataset.retrieveMultiframeDataset(uri);
 
    dataSet = combineFrameInstanceDataset(frame, multiframeDataSet);
  } else if (loadedDataSets[uri]) {
    dataSet = loadedDataSets[uri].dataSet;
  }
 
  return dataSet;
}
 
function update(uri: string, dataSet: DataSet) {
  const loadedDataSet = loadedDataSets[uri];
 
  if (!loadedDataSet) {
    console.error(`No loaded dataSet for uri ${uri}`);
 
    return;
  }
  // Update dataset
  cacheSizeInBytes -= loadedDataSet.dataSet.byteArray.length;
  loadedDataSet.dataSet = dataSet;
  cacheSizeInBytes += dataSet.byteArray.length;
 
  triggerEvent(eventTarget, 'datasetscachechanged', {
    uri,
    action: 'updated',
    cacheInfo: getInfo(),
  });
}
 
// loads the dicom dataset from the wadouri sp
function load(
  uri: string,
  loadRequest: LoadRequestFunction = xhrRequest as LoadRequestFunction,
  imageId: string
): CornerstoneWadoLoaderCachedPromise {
  // if already loaded return it right away
  Iif (loadedDataSets[uri]) {
    // console.log('using loaded dataset ' + uri);
    return new Promise((resolve) => {
      loadedDataSets[uri].cacheCount++;
      resolve(loadedDataSets[uri].dataSet);
    });
  }
 
  // if we are currently loading this uri, increment the cacheCount and return its promise
  Iif (promises[uri]) {
    // console.log('returning existing load promise for ' + uri);
    promises[uri].cacheCount++;
 
    return promises[uri];
  }
 
  // This uri is not loaded or being loaded, load it via an xhrRequest
  const loadDICOMPromise = loadRequest(uri, imageId);
 
  // handle success and failure of the XHR request load
  const promise: CornerstoneWadoLoaderCachedPromise = new Promise(
    (resolve, reject) => {
      loadDICOMPromise
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        .then(async function (dicomPart10AsArrayBuffer: any /* , xhr*/) {
          const partialContent = {
            isPartialContent: false,
            fileTotalLength: null,
          };
 
          // Allow passing extra data with the loader promise so as not to change
          // the API
          Iif (!(dicomPart10AsArrayBuffer instanceof ArrayBuffer)) {
            if (!dicomPart10AsArrayBuffer.arrayBuffer) {
              return reject(
                new Error(
                  'If not returning ArrayBuffer, must return object with `arrayBuffer` parameter'
                )
              );
            }
            partialContent.isPartialContent =
              dicomPart10AsArrayBuffer.flags.isPartialContent;
            partialContent.fileTotalLength =
              dicomPart10AsArrayBuffer.flags.fileTotalLength;
            dicomPart10AsArrayBuffer = dicomPart10AsArrayBuffer.arrayBuffer;
          }
 
          const byteArray = new Uint8Array(dicomPart10AsArrayBuffer);
 
          // Reject the promise if parsing the dicom file fails
          let dataSet: DataSet | DICOMLoaderDataSetWithFetchMore;
 
          try {
            Iif (partialContent.isPartialContent) {
              // This dataSet object will include a fetchMore function,
              dataSet = await dataSetFromPartialContent(
                byteArray,
                loadRequest,
                {
                  uri,
                  imageId,
                  fileTotalLength: partialContent.fileTotalLength,
                }
              );
            } else {
              dataSet = dicomParser.parseDicom(byteArray);
            }
          } catch (error) {
            return reject(error);
          }
 
          loadedDataSets[uri] = {
            dataSet,
            cacheCount: promise.cacheCount,
          };
          cacheSizeInBytes += dataSet.byteArray.length;
          resolve(dataSet);
 
          triggerEvent(eventTarget, 'datasetscachechanged', {
            uri,
            action: 'loaded',
            cacheInfo: getInfo(),
          });
        }, reject)
        .then(
          () => {
            // Remove the promise if success
            delete promises[uri];
          },
          () => {
            // Remove the promise if failure
            delete promises[uri];
          }
        );
    }
  );
 
  promise.cacheCount = 1;
 
  promises[uri] = promise;
 
  return promise;
}
 
// remove the cached/loaded dicom dataset for the specified wadouri to free up memory
function unload(uri: string): void {
  // console.log('unload for ' + uri);
  if (loadedDataSets[uri]) {
    loadedDataSets[uri].cacheCount--;
    if (loadedDataSets[uri].cacheCount === 0) {
      // console.log('removing loaded dataset for ' + uri);
      cacheSizeInBytes -= loadedDataSets[uri].dataSet.byteArray.length;
      delete loadedDataSets[uri];
 
      triggerEvent(eventTarget, 'datasetscachechanged', {
        uri,
        action: 'unloaded',
        cacheInfo: getInfo(),
      });
    }
  }
}
 
export function getInfo(): CornerstoneWadoLoaderCacheManagerInfoResponse {
  return {
    cacheSizeInBytes,
    numberOfDataSetsCached: Object.keys(loadedDataSets).length,
  };
}
 
// removes all cached datasets from memory
function purge(): void {
  purgeLoadedDataSets();
  promises = {};
  cacheSizeInBytes = 0;
}
 
export default {
  isLoaded,
  load,
  unload,
  getInfo,
  purge,
  get,
  update,
};
 
export { loadedDataSets };