-
Notifications
You must be signed in to change notification settings - Fork 3
/
with-remote-data-updates.js
57 lines (49 loc) · 2.14 KB
/
with-remote-data-updates.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
const React = require('react');
const { withRouter } = require('next/router');
const io = require('socket.io-client');
const { DEFAULT_LIVE_UPDATE_PORT, LIVE_UPDATE_EVENT_NAME, LIVE_UPDATE_NAMESPACE } = require('./lib/client-consts');
module.exports.withRemoteDataUpdates = function withRemoteDataUpdates(WrappedComponent) {
class Component extends React.Component {
componentDidMount() {
if (!this.props.liveUpdate) {
return;
}
// console.log('withSSGPage componentDidMount', this.props);
const liveUpdatePort =
typeof this.props.liveUpdatePort !== 'undefined' ? this.props.liveUpdatePort || location.port : DEFAULT_LIVE_UPDATE_PORT;
const eventName = this.props.liveUpdateEventName || LIVE_UPDATE_EVENT_NAME;
const namespace = this.props.liveUpdateNamespace || LIVE_UPDATE_NAMESPACE;
this.socket = io(`${location.protocol}//${location.hostname}${prefixPort(liveUpdatePort)}${namespace}`);
this.socket.on(eventName, () => {
this.props.router.replace(this.props.router.pathname, this.props.router.asPath, {
scroll: false
});
});
this.socket.on('connect', () => {
this.socket.emit('hello');
});
}
componentWillUnmount() {
if (!this.props.liveUpdate) {
return;
}
// console.log('withSSGPage componentWillUnmount');
if (this.socket) {
this.socket.close();
}
}
render() {
// console.log('withSSGPage render', this.props);
return React.createElement(WrappedComponent, this.props, null);
// return <WrappedComponent {...this.props} />;
}
}
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
Component.displayName = `WithRemoteDataUpdates(${getDisplayName(WrappedComponent)})`;
return withRouter(Component);
};
function prefixPort(port) {
return port ? `:${port}` : '';
}