All files / Sources/IO/Core/HttpDataSetSeriesReader index.js

3.84% Statements 3/78
0% Branches 0/28
0% Functions 0/24
3.89% Lines 3/77

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                              1x                                                                                                                                                                                                                                                                                                                                               1x                                               1x                
// For vtk factory
import 'vtk.js/Sources/Common/DataModel/ImageData';
import 'vtk.js/Sources/Common/DataModel/PolyData';
 
import vtk from 'vtk.js/Sources/vtk';
import macro from 'vtk.js/Sources/macros';
import DataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper';
import HttpDataSetReader from '../HttpDataSetReader';
 
// Enable data soure for DataAccessHelper
import 'vtk.js/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper'; // Just need HTTP
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper'; // HTTP + zip
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper'; // html + base64 + zip
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper'; // zip
 
const HTTP_DATA_ACCESS = DataAccessHelper.get('http');
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
function processDataSet(publicAPI, model, dataset, resolve, reject, loadData) {
  model.readers = [];
 
  const pendingPromises = [];
  dataset.series.forEach((timeStep) => {
    const newReader = HttpDataSetReader.newInstance({
      fetchGzip: model.fetchGzip,
      dataAccessHelper: model.dataAccessHelper,
    });
    pendingPromises.push(
      newReader.setUrl(`${model.baseURL}/${timeStep.url}`, { loadData })
    );
    model.readers.push({
      timeStep: timeStep.timeStep,
      reader: newReader,
    });
  });
 
  return Promise.all(pendingPromises).then(
    () => {
      const range = publicAPI.getTimeRange();
      if (range && range.length !== 0) {
        publicAPI.setUpdateTimeStep(range[0]);
      }
      resolve(publicAPI);
    },
    (error) => {
      reject(error);
    }
  );
}
 
// ----------------------------------------------------------------------------
// vtkHttpDataSetSeriesReader methods
// ----------------------------------------------------------------------------
 
function vtkHttpDataSetSeriesReader(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkHttpDataSetSeriesReader');
 
  // Empty output by default
  model.output[0] = vtk({ vtkClass: 'vtkPolyData' });
 
  // Create default dataAccessHelper if not available
  if (!model.dataAccessHelper) {
    model.dataAccessHelper = HTTP_DATA_ACCESS;
  }
 
  model.currentReader = null;
 
  publicAPI.updateMetaData = (loadData = false) => {
    if (model.compression === 'zip') {
      return new Promise((resolve, reject) => {
        HTTP_DATA_ACCESS.fetchBinary(model.baseURL).then((zipContent) => {
          model.dataAccessHelper = DataAccessHelper.get('zip', {
            zipContent,
            callback: (zip) => {
              model.baseURL = '';
              model.dataAccessHelper.fetchJSON(publicAPI, 'index.json').then(
                (dataset) => {
                  processDataSet(
                    publicAPI,
                    model,
                    dataset,
                    resolve,
                    reject,
                    loadData
                  );
                },
                (error) => {
                  reject(error);
                }
              );
            },
          });
        });
      });
    }
 
    return new Promise((resolve, reject) => {
      model.dataAccessHelper.fetchJSON(publicAPI, model.url).then(
        (dataset) =>
          processDataSet(publicAPI, model, dataset, resolve, reject, loadData),
        (error) => {
          reject(error);
        }
      );
    });
  };
 
  publicAPI.setUrl = (url, options = {}) => {
    if (!url.endsWith('index.json') && !options.fullpath) {
      model.baseURL = url;
      model.url = `${url}/index.json`;
    } else {
      model.url = url;
 
      // Remove the file in the URL
      const path = url.split('/');
      path.pop();
      model.baseURL = path.join('/');
    }
 
    model.compression = options.compression;
 
    return publicAPI.updateMetaData(options.loadData);
  };
 
  publicAPI.getTimeSteps = () =>
    model.readers.map((reader) => reader.timeStep).sort();
 
  publicAPI.getTimeRange = () => {
    const timeSteps = publicAPI.getTimeSteps();
    const length = timeSteps.length;
    if (length >= 1) {
      return [timeSteps[0], timeSteps[length - 1]];
    }
    return [];
  };
 
  publicAPI.setUpdateTimeStep = (timeStep) => {
    const reader = model.readers.reduce((newReader, currentReader) => {
      if (currentReader.timeStep <= timeStep) {
        return currentReader;
      }
      return newReader;
    });
 
    if (reader === undefined) {
      return;
    }
 
    model.currentReader = reader.reader;
    model.output[0] = model.currentReader.getOutputData();
    model.output[0].modified();
    publicAPI.modified();
  };
 
  publicAPI.requestData = (inData, outData) => {
    if (model.currentReader) {
      outData[0] = model.currentReader.getOutputData();
    }
  };
 
  // Toggle arrays to load
  publicAPI.enableArray = (location, name, enable = true) => {
    if (model.currentReader) {
      model.currentReader.reader.enableArray(location, name, enable);
    }
  };
 
  publicAPI.loadData = () => {
    if (model.currentReader) {
      model.currentReader.reader.loadData();
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  fetchGzip: false,
  url: null,
  baseURL: null,
  dataAccessHelper: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.get(publicAPI, model, ['url', 'baseURL']);
  macro.set(publicAPI, model, ['dataAccessHelper']);
  macro.algo(publicAPI, model, 0, 1);
 
  // Object methods
  vtkHttpDataSetSeriesReader(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkHttpDataSetSeriesReader'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };