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

26.41% Statements 14/53
0% Branches 0/25
15.38% Functions 2/13
27.45% Lines 14/51

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            2x                                       2x                                       2x                                       2x                   2x           2x           2x                                             2x                       1x                 2x     2x 2x     2x         1x          
import macro from 'vtk.js/Sources/macros';
import DefaultProtocol from 'vtk.js/Sources/IO/Core/ImageStream/DefaultProtocol';
import ViewStream from 'vtk.js/Sources/IO/Core/ImageStream/ViewStream';
 
function vtkImageStream(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageStream');
 
  // --------------------------------------------------------------------------
  // Internal private method
  // --------------------------------------------------------------------------
 
  function onImage(data) {
    const message = data[0];
    if (!message || !message.image) {
      return;
    }
    for (let i = 0; i < model.viewStreams.length; i++) {
      model.viewStreams[i].processMessage(message);
    }
  }
 
  // --------------------------------------------------------------------------
  // PublicAPI
  // --------------------------------------------------------------------------
 
  publicAPI.setServerAnimationFPS = (maxFPS = 30) => {
    let changeDetected = false;
    if (model.serverAnimationFPS !== maxFPS) {
      model.serverAnimationFPS = maxFPS;
      changeDetected = true;
    }
 
    if (!model.protocol) {
      return Promise.resolve(true);
    }
 
    if (changeDetected) {
      publicAPI.modified();
    }
 
    return model.protocol.setServerAnimationFPS(maxFPS);
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.connect = (session, protocol = DefaultProtocol) => {
    if (model.connected || !session || !protocol) {
      return;
    }
    model.protocol = protocol(session);
    model.protocol
      .subscribeToImageStream(onImage)
      .promise // new API in wslink 1.0.5+
      .then((subscription) => {
        model.renderTopicSubscription = subscription;
        model.connected = true;
      })
      .catch((e) => {
        model.connected = false;
        console.error(e);
      });
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.disconnect = () => {
    if (model.protocol && model.connected && model.renderTopicSubscription) {
      model.protocol.unsubscribeToImageStream(model.renderTopicSubscription);
      model.renderTopicSubscription = null;
    }
    model.connected = false;
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.registerViewStream = (view) => {
    model.viewStreams.push(view);
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.unregisterViewStream = (view) => {
    model.viewStreams = model.viewStreams.filter((v) => v !== view);
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.createViewStream = (viewId = '-1', size = [400, 400]) => {
    const {
      setServerAnimationFPS,
      getServerAnimationFPS,
      unregisterViewStream,
    } = publicAPI;
    const viewStream = ViewStream.newInstance({
      protocol: model.protocol,
      unregisterViewStream,
      sharedAPI: {
        setServerAnimationFPS,
        getServerAnimationFPS,
      },
    });
    viewStream.setViewId(viewId);
    viewStream.setSize(size[0], size[1]);
    publicAPI.registerViewStream(viewStream);
 
    return viewStream;
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.delete = macro.chain(() => {
    while (model.viewStreams.length) {
      model.viewStreams.pop().delete();
    }
    publicAPI.disconnect();
  }, publicAPI.delete);
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  // protocol: null,
  viewStreams: [],
  serverAnimationFPS: -1,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  macro.obj(publicAPI, model);
  macro.get(publicAPI, model, ['serverAnimationFPS', 'protocol']);
 
  // Object specific methods
  vtkImageStream(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImageStream');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };