Skip to content

Commit

Permalink
js: adapt Legacy/Poller to new code
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hartmann authored and JackUrb committed Feb 21, 2023
1 parent 0e801c9 commit d17a404
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
19 changes: 8 additions & 11 deletions js/Legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class Poller {
* Wrapper around what would regularly be socket communications, but handled
* through a POST-based polling loop
*/
constructor(app) {
this.app = app;
constructor(correctPathname, _handleMessage, onConnect, onDisconnect) {
this.onConnect = onConnect;
this.onDisconnect = onDisconnect;
var url = window.location;
this.target =
url.protocol + '//' + url.host + app.correctPathname() + 'socket_wrap';
this.handleMessage = app._handleMessage;
url.protocol + '//' + url.host + correctPathname() + 'socket_wrap';
this.onmessage = _handleMessage;
fetch(this.target)
.then((res) => {
return res.json();
Expand All @@ -38,15 +39,11 @@ class Poller {
finishSetup = (sid) => {
this.sid = sid;
this.poller_id = window.setInterval(() => this.poll(), POLLING_INTERVAL);
this.app.setState({
connected: true,
});
this.onConnect(true);
};

close = () => {
this.app.setState({ connected: false }, () => {
this.app._socket = null;
});
this.onDisconnect();
window.clearInterval(this.poller_id);
};

Expand Down Expand Up @@ -82,7 +79,7 @@ class Poller {
// Must re-encode message as handle message expects json
// in this particular format from sockets
// TODO Could refactor message parsing out elsewhere.
this.handleMessage({ data: msg });
this.onmessage({ data: msg });
});
}
},
Expand Down
37 changes: 21 additions & 16 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,29 @@ function App() {
if (_socket.current) {
return;
}

const _onConnect = () => setConnected(true);
const _onDisconnect = () => {
// check if is mounted. error can appear on unmounted component
if (mounted.current) {
callbacks.current.push(() => {
_socket.current = null;
});
setConnected(false);
}
};

// eslint-disable-next-line no-undef
if (USE_POLLING) {
_socket.current = new Poller(this);
_socket.current = new Poller(
correctPathname,
_handleMessage,
_onConnect,
_onDisconnect
);
return;
}

var url = window.location;
var ws_protocol = null;
if (url.protocol == 'https:') {
Expand All @@ -271,21 +289,8 @@ function App() {
);

socket.onmessage = _handleMessage;

socket.onopen = () => {
setConnected(true);
};

socket.onerror = socket.onclose = () => {
// check if is mounted. error can appear on unmounted component
if (mounted.current) {
setConnected(false);
callbacks.current.push(() => {
socket.current = null;
});
}
};

socket.onopen = _onConnect;
socket.onerror = socket.onclose = _onDisconnect;
_socket.current = socket;
};

Expand Down

0 comments on commit d17a404

Please sign in to comment.