|
| 1 | +import React from 'react'; // eslint-disable-line no-unused-vars |
| 2 | +import Layer from './layer'; |
| 3 | + |
| 4 | +export default class WPipeLayer extends Layer { |
| 5 | + static propTypes = { |
| 6 | + /** |
| 7 | + * URI to WPIPE websocket server |
| 8 | + * |
| 9 | + * This usually looks like ws://<some URI>:<some port> |
| 10 | + * |
| 11 | + * Keep in mind that if the websocket server is on a different domain, |
| 12 | + * most browsers/web-servers will block cross origin requests. |
| 13 | + * |
| 14 | + * Since this layer doesn't take any numeric data, |
| 15 | + * we are omitting the use of the `data` prop here. |
| 16 | + */ |
| 17 | + wsurl: PropTypes.string, |
| 18 | + |
| 19 | + /** Key-value pairs whose values alter plot settings */ |
| 20 | + overrides: PropTypes.object, |
| 21 | + |
| 22 | + /** Layer options */ |
| 23 | + options: PropTypes.object, |
| 24 | + |
| 25 | + /** Frames per second throttles the data flow to the client by the specified */ |
| 26 | + fps: PropTypes.number, |
| 27 | + }; |
| 28 | + |
| 29 | + /** |
| 30 | + * Handles WPipeLayer being mounted onto the DOM |
| 31 | + * |
| 32 | + * All we need to do when the component 'mounts', |
| 33 | + * is call `plot.overlay_wpipe` with the relevant |
| 34 | + * websocket url and options. This will return our layer object. |
| 35 | + * |
| 36 | + * A large portion of the time, especially for dynamic |
| 37 | + * systems, this will look like a single |
| 38 | + * `this.plot.overlay_wpipe(wsurl, null, {"layerType": "1D", pipesize: ...)` |
| 39 | + * upon mount. |
| 40 | + */ |
| 41 | + componentDidMount() { |
| 42 | + const { wsurl, options, layerOptions, fps } = this.props; |
| 43 | + this.layer = this.plot.overlay_wpipe(wsurl, options, layerOptions, fps); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Handles new properties being passed into <WPipeLayer/> |
| 48 | + * |
| 49 | + * This will be replaced by |
| 50 | + * |
| 51 | + * static getDerivedStateFromProps(nextProps, prevState) |
| 52 | + * |
| 53 | + * in React 17. |
| 54 | + * |
| 55 | + * This sits in the lifecycle right before `shouldComponentUpdate`, |
| 56 | + * `componentWillUpdate`, and most importantly `render`, so this is |
| 57 | + * where we will call the plot's `reload` and `headermod` methods. |
| 58 | + * |
| 59 | + * @param nextProps the newly received properties |
| 60 | + */ |
| 61 | + UNSAFE_componentWillReceiveProps(nextProps) { |
| 62 | + const { |
| 63 | + wsurl: currentWsurl, |
| 64 | + options: currentOptions, |
| 65 | + layerOptions: currentLayerOptions, |
| 66 | + fps: currentFps, |
| 67 | + } = this.props; |
| 68 | + const { |
| 69 | + wsurl: nextWsurl, |
| 70 | + options: nextOptions, |
| 71 | + layerOptions: nextLayerOptions, |
| 72 | + fps: nextFps, |
| 73 | + } = nextProps; |
| 74 | + |
| 75 | + // if the wsurl changes, we'll go ahead |
| 76 | + // and delete the layer and create a new one |
| 77 | + // otherwise, we only need to headermod |
| 78 | + // with the new options |
| 79 | + if (nextWsurl !== currentWsurl || currentFps !== nextFps) { |
| 80 | + this.plot.delete_layer(this.layer); |
| 81 | + this.layer = this.plot.overlay_wpipe(nextWsurl, options, layerOptions, nextFps); |
| 82 | + } else if (nextOptions !== currentOptions) { |
| 83 | + this.plot.headermod(this.layer, nextOptions); |
| 84 | + } else if (nextLayerOptions !== currentLayerOptions) { |
| 85 | + this.plot.get_layer(this.layer).change_settings(nextLayerOptions); |
| 86 | + } |
| 87 | + return false; |
| 88 | + } |
| 89 | +} |
0 commit comments