Skip to content

Commit b6fc04f

Browse files
committed
Removed need for explicit connect action on daemon
1 parent 651d196 commit b6fc04f

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

src/socket-daemon.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
BehaviorSubject,
3737
interval
3838
} from 'rxjs';
39-
import { filter } from 'rxjs/operators';
4039
import ReaderWriter from './reader-writer';
4140

4241
// Required agent version
@@ -73,15 +72,23 @@ export default class SocketDaemon {
7372

7473
this.readerWriter = new ReaderWriter();
7574
this.wsConnected
76-
.pipe(filter(status => status))
77-
.subscribe(() => {
78-
this.readerWriter.initSocket(this.socket)
75+
.subscribe(status => {
76+
if (status) {
77+
this.readerWriter.initSocket(this.socket);
78+
}
79+
else {
80+
this.findAgent();
81+
}
7982
});
8083

8184
this.agentFound
82-
.pipe(filter(status => !status))
83-
.subscribe(() => {
84-
this.agentInfo = {};
85+
.subscribe(status => {
86+
if (status) {
87+
this.wsConnect();
88+
}
89+
else {
90+
this.agentInfo = {};
91+
}
8592
});
8693
}
8794

@@ -131,8 +138,6 @@ export default class SocketDaemon {
131138
const found = responses.some(r => {
132139
if (r && r.response && r.response.status === 200) {
133140
this.agentInfo = r.data;
134-
this.agentFound.next(true);
135-
this.wsConnect();
136141
if (r.response.url.indexOf(PROTOCOL.HTTPS) === 0) {
137142
this.selectedProtocol = PROTOCOL.HTTPS;
138143
}
@@ -141,6 +146,7 @@ export default class SocketDaemon {
141146
this.agentInfo[this.selectedProtocol] = this.agentInfo[this.selectedProtocol].replace('localhost', '127.0.0.1');
142147
}
143148
this.readerWriter.initPluginUrl(this.agentInfo[this.selectedProtocol]);
149+
this.agentFound.next(true);
144150
return true;
145151
}
146152
return false;
@@ -197,7 +203,6 @@ export default class SocketDaemon {
197203
this.portsPollingSubscription.unsubscribe();
198204
}
199205
this.wsConnected.next(false);
200-
this.findAgent();
201206
});
202207
}
203208

test/app.jsx

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,34 @@ class App extends React.Component {
1919
agentInfo: '',
2020
serialMonitorContent: ''
2121
};
22-
23-
this.connect = this.connect.bind(this);
2422
this.handleOpen = this.handleOpen.bind(this);
2523
this.handleClose = this.handleClose.bind(this);
24+
this.daemon = Daemon;
2625
}
2726

2827
componentDidMount() {
29-
Daemon.agentFound.subscribe(status => {
28+
this.daemon.agentFound.subscribe(status => {
3029
this.setState({
3130
agentStatus: status,
32-
agentInfo: JSON.stringify(Daemon.agentInfo, null, 2)
31+
agentInfo: JSON.stringify(this.daemon.agentInfo, null, 2)
3332
});
3433
});
3534

36-
Daemon.wsConnected.subscribe(status => {
35+
this.daemon.wsConnected.subscribe(status => {
3736
this.setState({ wsStatus: status });
3837
});
3938

40-
Daemon.wsError.subscribe(this.showError);
39+
this.daemon.wsError.subscribe(this.showError);
4140

42-
Daemon.readerWriter.messageSubject.subscribe(() => {
41+
this.daemon.readerWriter.messageSubject.subscribe(() => {
4342
this.setState({
44-
serialDevices: Daemon.readerWriter.devicesList.serial,
45-
networkDevices: Daemon.readerWriter.devicesList.network
43+
serialDevices: this.daemon.readerWriter.devicesList.serial,
44+
networkDevices: this.daemon.readerWriter.devicesList.network
4645
});
4746
});
4847

4948
const serialTextarea = document.getElementById('serial-textarea');
50-
Daemon.readerWriter.serialMonitorSubject.subscribe(message => {
49+
this.daemon.readerWriter.serialMonitorSubject.subscribe(message => {
5150
this.setState({ serialMonitorContent: this.state.serialMonitorContent + message });
5251
scrollToBottom(serialTextarea);
5352
});
@@ -61,21 +60,15 @@ class App extends React.Component {
6160
this.setState({ error: '' });
6261
}
6362

64-
connect() {
65-
this.clearError();
66-
Daemon.findAgent()
67-
.catch(this.showError);
68-
}
69-
7063
handleOpen(e, port) {
7164
this.setState({ serialMonitorContent: '' });
7265
e.preventDefault();
73-
Daemon.readerWriter.openSerialMonitor(port);
66+
this.daemon.readerWriter.openSerialMonitor(port);
7467
}
7568

7669
handleClose(e, port) {
7770
e.preventDefault();
78-
Daemon.readerWriter.closeSerialMonitor(port);
71+
this.daemon.readerWriter.closeSerialMonitor(port);
7972
}
8073

8174
render() {
@@ -85,7 +78,6 @@ class App extends React.Component {
8578
return (
8679
<div>
8780
<h1>Test Arduino Create Plugin</h1>
88-
<button id="connect" onClick={ this.connect }>Connect</button>
8981
<p>Agent status: <span id="agent-status" className={ this.state.agentStatus ? 'found' : 'not-found' }>
9082
{ this.state.agentStatus ? 'Found' : 'Not found' }
9183
</span></p>

0 commit comments

Comments
 (0)