Skip to content

Commit f59ea37

Browse files
author
Stefania
authored
Merge pull request #28 from arduino/simplify-subjects
automatically unsubscribe to upload, download and configure subjects
2 parents c66d070 + 62d3f45 commit f59ea37

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arduino-create-agent-js-client",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "JS module providing discovery of the Arduino Create Plugin and communication with it",
55
"main": "./src/index.js",
66
"module": "es/index.js",

src/boardConfiguration.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ export default class BoardConfiguration {
3333
this.daemon = daemon;
3434
this.serialMonitorContent = '';
3535
this.configuring = new BehaviorSubject({ status: this.CONFIGURE_NOPE });
36-
this.configureDone = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_DONE));
36+
this.configureDone = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_DONE))
37+
.pipe(first())
38+
.pipe(takeUntil(this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_ERROR))));
39+
this.configureError = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_ERROR))
40+
.pipe(first())
41+
.pipe(takeUntil(this.configureDone));
3742
this.configureInProgress = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_IN_PROGRESS));
38-
this.configureError = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_ERROR));
3943
this.daemon.serialMonitorMessages.subscribe(message => {
4044
this.serialMonitorContent += message;
4145
});
@@ -45,8 +49,8 @@ export default class BoardConfiguration {
4549
this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Starting board configuration...' });
4650
}
4751

48-
notifyError(msg) {
49-
this.configuring.next({ status: this.CONFIGURE_ERROR, msg: msg, err: msg});
52+
notifyError(err, msg) {
53+
this.configuring.next({ status: this.CONFIGURE_ERROR, err, msg });
5054
}
5155

5256
/**
@@ -189,7 +193,7 @@ export default class BoardConfiguration {
189193
return;
190194
}
191195

192-
this.daemon.uploadingDone.pipe(first()).subscribe(() => {
196+
this.daemon.uploadingDone.subscribe(() => {
193197
this.configuring.next({
194198
status: this.CONFIGURE_IN_PROGRESS,
195199
msg: 'Provisioning sketch uploaded successfully. Opening serial monitor...'
@@ -206,7 +210,7 @@ export default class BoardConfiguration {
206210
status: this.CONFIGURE_IN_PROGRESS,
207211
msg: 'CSR generated. Creating device...'
208212
});
209-
return createDeviceCb(csr)
213+
return createDeviceCb(csr);
210214
})
211215
.then(data => {
212216
this.configuring.next({
@@ -230,10 +234,10 @@ export default class BoardConfiguration {
230234
err: error.toString()
231235
});
232236
});
233-
this.daemon.openSerialMonitor(board.port, BAUDRATE);
237+
this.daemon.openSerialMonitor(board.port, BAUDRATE);
234238
});
235239

236-
this.daemon.uploadingError.pipe(first()).subscribe(upload => {
240+
this.daemon.uploadingError.subscribe(upload => {
237241
this.configuring.next({ status: this.CONFIGURE_ERROR, err: `Couldn't configure board at port ${board.port}. Upload failed with error: ${upload.err}` });
238242
});
239243

src/daemon.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ export default class Daemon {
3939
this.serialMonitorOpened = new BehaviorSubject(false);
4040
this.serialMonitorMessages = new Subject();
4141
this.uploading = new BehaviorSubject({ status: this.UPLOAD_NOPE });
42-
this.uploadingDone = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_DONE));
42+
this.uploadingDone = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_DONE))
43+
.pipe(first())
44+
.pipe(takeUntil(this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_ERROR))));
45+
this.uploadingError = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_ERROR))
46+
.pipe(first())
47+
.pipe(takeUntil(this.uploadingDone));
4348
this.uploadInProgress = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_IN_PROGRESS));
44-
this.uploadingError = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_ERROR));
4549
this.devicesList = new BehaviorSubject({
4650
serial: [],
4751
network: []
@@ -57,6 +61,10 @@ export default class Daemon {
5761
.subscribe(() => this.closeAllPorts());
5862
}
5963

64+
notifyUploadError(err) {
65+
this.uploading.next({ status: this.UPLOAD_ERROR, err });
66+
}
67+
6068
openChannel(cb) {
6169
this.channelOpen
6270
.subscribe(open => {

src/socket-daemon.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ export default class SocketDaemon extends Daemon {
6565
this.pluginURL = null;
6666

6767
this.downloading = new BehaviorSubject({ status: DOWNLOAD_NOPE });
68-
this.downloadingDone = this.downloading.pipe(filter(download => download.status === DOWNLOAD_DONE));
69-
this.downloadingError = this.downloading.pipe(filter(download => download.status === DOWNLOAD_ERROR));
68+
this.downloadingDone = this.downloading.pipe(filter(download => download.status === DOWNLOAD_DONE))
69+
.pipe(first())
70+
.pipe(takeUntil(this.downloading.pipe(filter(download => download.status === this.DOWNLOAD_ERROR))));
71+
this.downloadingError = this.downloading.pipe(filter(download => download.status === DOWNLOAD_ERROR))
72+
.pipe(first())
73+
.pipe(takeUntil(this.downloadingDone));
7074

7175
this.openChannel(() => this.socket.emit('command', 'list'));
7276

@@ -92,6 +96,10 @@ export default class SocketDaemon extends Daemon {
9296
});
9397
}
9498

99+
notifyDownloadError(err) {
100+
this.downloading.next({ status: this.DOWNLOAD_ERROR, err });
101+
}
102+
95103
/**
96104
* Look for the agent endpoint.
97105
* First search in http://LOOPBACK_ADDRESS, after in https://LOOPBACK_HOSTNAME if in Chrome or Firefox, otherwise vice versa.
@@ -443,7 +451,6 @@ export default class SocketDaemon extends Daemon {
443451
this.serialMonitorOpened.pipe(filter(open => !open))
444452
.pipe(first())
445453
.subscribe(() => {
446-
447454
fetch(`${this.pluginURL}/upload`, {
448455
method: 'POST',
449456
headers: {
@@ -473,7 +480,10 @@ export default class SocketDaemon extends Daemon {
473480
* Interrupt upload
474481
*/
475482
stopUploadCommand() {
476-
this.uploading.next(false);
483+
this.uploading.next({
484+
status: this.UPLOAD_ERROR,
485+
err: 'upload stopped'
486+
});
477487
this.socket.emit('command', 'killprogrammer');
478488
}
479489
}

0 commit comments

Comments
 (0)