All files / Sources/Proxy/Core/SourceProxy index.js

38.98% Statements 23/59
21.05% Branches 8/38
30% Functions 3/10
39.65% Lines 23/58

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                1x       1x                                 1x                     1x                                     1x 1x     1x     1x         1x       1x                             1x         1x             1x             1x             1x   1x 1x             1x 1x 1x     1x   1x             1x                
import macro from 'vtk.js/Sources/macros';
 
// ----------------------------------------------------------------------------
// vtkSourceProxy methods
// ----------------------------------------------------------------------------
 
function vtkSourceProxy(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkSourceProxy');
 
  // API ----------------------------------------------------------------------
 
  publicAPI.setInputProxy = (source) => {
    if (model.inputSubscription) {
      model.inputSubscription();
      model.inputSubscription = null;
    }
    model.inputProxy = source;
    if (model.inputProxy) {
      model.inputSubscription = source.onModified(
        publicAPI.update,
        -1
      ).unsubscribe; // Trigger at next cycle
    }
    publicAPI.update();
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.setInputData = (ds, type) => {
    if (model.dataset !== ds) {
      model.dataset = ds;
      model.type = type || ds.getClassName();
      publicAPI.modified();
      publicAPI.invokeDatasetChange();
    }
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.setInputAlgorithm = (algo, type, autoUpdate = true) => {
    model.type = type;
    if (model.algo !== algo) {
      model.algo = algo;
      if (model.algoSubscription) {
        model.algoSubscription();
        model.algoSubscription = null;
      }
      if (algo && autoUpdate) {
        model.algoSubscription = algo.onModified(() => {
          publicAPI.update();
        }, -1).unsubscribe; // Trigger at next cycle
        publicAPI.update();
      }
    }
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.update = () => {
    Iif (model.algo && model.inputProxy) {
      model.algo.setInputData(model.inputProxy.getDataset());
    }
    Iif (model.updateDomain && model.inputProxy) {
      model.updateDomain(publicAPI, model.inputProxy.getDataset());
    }
    Iif (model.algo) {
      publicAPI.setInputData(model.algo.getOutputData(), model.type);
    }
  };
 
  publicAPI.getUpdate = () => model.algo.getMTime() > model.dataset.getMTime();
 
  // --------------------------------------------------------------------------
 
  publicAPI.delete = macro.chain(() => {
    if (model.algoSubscription) {
      model.algoSubscription();
      model.algoSubscription = null;
    }
    if (model.inputSubscription) {
      model.inputSubscription();
      model.inputSubscription = null;
    }
  }, publicAPI.delete);
 
  // --------------------------------------------------------------------------
  // Initialisation
  // --------------------------------------------------------------------------
 
  Iif (model.inputProxy) {
    model.inputSubscription = model.inputProxy.onModified(() => {
      publicAPI.update();
    }, -1).unsubscribe; // Trigger at next cycle
  }
  Iif (model.algoFactory) {
    publicAPI.setInputAlgorithm(
      model.algoFactory.newInstance(),
      null,
      model.autoUpdate
    );
  }
  publicAPI.update();
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  name: 'Default source',
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  macro.obj(publicAPI, model);
  macro.get(publicAPI, model, [
    'name',
    'type',
    'dataset',
    'algo',
    'inputProxy',
  ]);
  macro.set(publicAPI, model, ['name']);
  macro.event(publicAPI, model, 'DatasetChange');
  macro.proxy(publicAPI, model);
 
  // Object specific methods
  vtkSourceProxy(publicAPI, model);
 
  Iif (model.proxyPropertyMapping) {
    macro.proxyPropertyMapping(publicAPI, model, model.proxyPropertyMapping);
  }
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkSourceProxy');
 
// ----------------------------------------------------------------------------
 
export default {
  newInstance,
  extend,
};