forked from LGSInnovations/react-sigplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocketlayer.js
90 lines (80 loc) · 2.59 KB
/
websocketlayer.js
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
import React from 'react'; // eslint-disable-line no-unused-vars
import PropTypes from 'prop-types';
import Layer from './layer';
/**
* Wrapper around sigplot.Plot.overlay_websocket
*
* Typical use of this layer looks like
* <SigPlot>
* <WebsocketLayer wsurl={'ws://localhost:8080'}/>
* </SigPlot>
*/
class WebsocketLayer extends Layer {
static propTypes = {
/**
* URI to websocket server
*
* This usually looks like ws://<some URI>:<some port>
*
* Keep in mind that if the websocket server is on a different domain,
* most browsers/web-servers will block cross origin requests.
*
* Since this layer doesn't take any numeric data,
* we are omitting the use of the `data` prop here.
*/
wsurl: PropTypes.string,
/** Key-value pairs whose values alter plot settings */
overrides: PropTypes.object,
/** Layer options */
options: PropTypes.object,
};
static defaultProps = {
wsurl: '',
};
/**
* On mount, all we need to do is call overlay_websocket
*/
componentDidMount() {
const { wsurl, overrides, options } = this.props;
this.layer = this.context.overlay_websocket(wsurl, overrides, options);
}
/**
* Handles new properties being passed into <HrefLayer/>
*
* UNSAFE_componentWillReceiveProps() replaced with
* shouldComponentUpdate() as they have similar calling patterns.
* We are using this method for a side-effect, and therefore
* returning True. getDerivedStateFromProps() had an additional
* call at mount which UNSAFE_componentWillReceiveProps() lacked.
* Thus the usage of shouldComponentUpdate().
*
* This sits in the lifecycle right before `componentWillUpdate`,
* and most importantly `render`, so this is where we will call
* the plot's `reload` and `headermod` methods.
*
* @param nextProps the newly received properties
*
* @TODO Investigate whether deoverlay is necessary here
*/
shouldComponentUpdate(nextProps, _nextState) {
const { wsurl: oldWsurl, options: oldOptions } = this.props;
const {
wsurl: newWsurl,
overrides: newOverrides,
options: newOptions,
} = nextProps;
// we only care if `wsurl` or `options` changes;
if (newWsurl !== oldWsurl) {
this.context.deoverlay(this.layer);
this.layer = this.context.overlay_websocket(
newWsurl,
newOverrides,
newOptions
);
} else if (this.layer !== undefined && newOptions !== oldOptions) {
this.context.get_layer(this.layer).change_settings(newOptions);
}
return true;
}
}
export default WebsocketLayer;