Skip to content

Commit c586822

Browse files
author
Alberto Iannaccone
committed
automatically unsubscribe to upload, download and configure subjects
1 parent c66d070 commit c586822

File tree

4 files changed

+137
-59
lines changed

4 files changed

+137
-59
lines changed

package.json

Lines changed: 99 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,77 @@
11
{
2-
"name": "arduino-create-agent-js-client",
3-
"version": "1.0.4",
4-
"description": "JS module providing discovery of the Arduino Create Plugin and communication with it",
5-
"main": "./src/index.js",
6-
"module": "es/index.js",
7-
"jsnext:main": "es/index.js",
8-
"files": [
9-
"dist",
10-
"lib",
11-
"es",
12-
"src"
2+
"_from": "arduino-create-agent-js-client@^1.0.3",
3+
"_id": "arduino-create-agent-js-client@1.0.3",
4+
"_inBundle": false,
5+
"_integrity": "sha512-EOFDmI6Qe0YpEFzkZUwntS/kW9+tb5D7El8YroXQOA2Tw5q/ntSxT4awybqAjOerWCf9BfHuBYwOP7XMFveLOw==",
6+
"_location": "/arduino-create-agent-js-client",
7+
"_phantomChildren": {
8+
"after": "0.8.2",
9+
"backo2": "1.0.2",
10+
"base64-arraybuffer": "0.1.5",
11+
"blob": "0.0.4",
12+
"component-bind": "1.0.0",
13+
"component-emitter": "1.2.1",
14+
"component-inherit": "0.0.3",
15+
"has-binary": "0.1.7",
16+
"has-cors": "1.1.0",
17+
"indexof": "0.0.1",
18+
"json3": "3.3.2",
19+
"object-component": "0.0.3",
20+
"options": "0.0.6",
21+
"parsejson": "0.0.3",
22+
"parseqs": "0.0.5",
23+
"parseuri": "0.0.5",
24+
"to-array": "0.1.4",
25+
"wtf-8": "1.0.0",
26+
"yeast": "0.1.2"
27+
},
28+
"_requested": {
29+
"type": "range",
30+
"registry": true,
31+
"raw": "arduino-create-agent-js-client@^1.0.3",
32+
"name": "arduino-create-agent-js-client",
33+
"escapedName": "arduino-create-agent-js-client",
34+
"rawSpec": "^1.0.3",
35+
"saveSpec": null,
36+
"fetchSpec": "^1.0.3"
37+
},
38+
"_requiredBy": [
39+
"/"
1340
],
14-
"directories": {
15-
"lib": "lib"
41+
"_resolved": "https://registry.npmjs.org/arduino-create-agent-js-client/-/arduino-create-agent-js-client-1.0.3.tgz",
42+
"_shasum": "c33caef538f1001ffa43a0cb6a11f40a990a9518",
43+
"_spec": "arduino-create-agent-js-client@^1.0.3",
44+
"_where": "/home/alberto/work/create-gettingstarted-site",
45+
"bugs": {
46+
"url": "https://github.com/arduino/arduino-create-agent-js-client/issues"
1647
},
48+
"bundleDependencies": false,
49+
"contributors": [
50+
{
51+
"name": "Stefania Mellai",
52+
"email": "s.mellai@arduino.cc"
53+
},
54+
{
55+
"name": "Fabrizio Mirabito",
56+
"email": "f.mirabito@arduino.cc"
57+
},
58+
{
59+
"name": "Alberto Iannaccone",
60+
"email": "a.iannaccone@arduino.cc"
61+
},
62+
{
63+
"name": "Gabriele Destefanis",
64+
"email": "g.destefanis@topsolution.it"
65+
}
66+
],
1767
"dependencies": {
1868
"detect-browser": "^2.5.1",
1969
"rxjs": "^6.2.1",
2070
"semver-compare": "^1.0.0",
2171
"socket.io-client": "1.7.4"
2272
},
73+
"deprecated": false,
74+
"description": "JS module providing discovery of the Arduino Create Plugin and communication with it",
2375
"devDependencies": {
2476
"babel-cli": "^6.26.0",
2577
"babel-core": "^6.26.3",
@@ -48,42 +100,47 @@
48100
"webpack-cli": "^3.0.8",
49101
"webpack-dev-server": "^2.11.1"
50102
},
51-
"scripts": {
52-
"test": "",
53-
"dev": "webpack-dev-server",
54-
"lint": "./node_modules/.bin/eslint src",
55-
"lint-fix": "./node_modules/.bin/eslint --fix src --ext .js",
56-
"clean": "rimraf lib dist es",
57-
"build": "npm run build:commonjs && npm run build:umd && npm run build:umd:min && npm run build:es",
58-
"build:watch": "echo 'build && watch the COMMONJS version of the package - for other version, run specific tasks' && npm run build:commonjs:watch",
59-
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
60-
"build:commonjs:watch": "npm run build:commonjs -- --watch",
61-
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es",
62-
"build:es:watch": "npm run build:es -- --watch",
63-
"build:umd": "cross-env BABEL_ENV=es NODE_ENV=development node_modules/.bin/rollup src/index.js --config --sourcemap --output dist/create-plugin.js",
64-
"build:umd:watch": "npm run build:umd -- --watch",
65-
"build:umd:min": "cross-env BABEL_ENV=es NODE_ENV=production rollup src/index.js --config --output dist/create-plugin.min.js",
66-
"prepare": "npm run clean && npm test && npm run build"
67-
},
68-
"repository": {
69-
"type": "git",
70-
"url": "git+https://github.com/arduino/arduino-create-agent-js-client.git"
103+
"directories": {
104+
"lib": "lib"
71105
},
106+
"files": [
107+
"dist",
108+
"lib",
109+
"es",
110+
"src"
111+
],
112+
"homepage": "https://github.com/arduino/arduino-create-agent-js-client#readme",
113+
"jsnext:main": "es/index.js",
72114
"keywords": [
73115
"arduino",
74116
"create",
75117
"agent",
76118
"plugin"
77119
],
78-
"contributors": [
79-
"Stefania Mellai <s.mellai@arduino.cc>",
80-
"Fabrizio Mirabito <f.mirabito@arduino.cc>",
81-
"Alberto Iannaccone <a.iannaccone@arduino.cc>",
82-
"Gabriele Destefanis <g.destefanis@topsolution.it>"
83-
],
84120
"license": "GPLv3",
85-
"bugs": {
86-
"url": "https://github.com/arduino/arduino-create-agent-js-client/issues"
121+
"main": "./src/index.js",
122+
"module": "es/index.js",
123+
"name": "arduino-create-agent-js-client",
124+
"repository": {
125+
"type": "git",
126+
"url": "git+https://github.com/arduino/arduino-create-agent-js-client.git"
127+
},
128+
"scripts": {
129+
"build": "npm run build:commonjs && npm run build:umd && npm run build:umd:min && npm run build:es",
130+
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
131+
"build:commonjs:watch": "npm run build:commonjs -- --watch",
132+
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es",
133+
"build:es:watch": "npm run build:es -- --watch",
134+
"build:umd": "cross-env BABEL_ENV=es NODE_ENV=development node_modules/.bin/rollup src/index.js --config --sourcemap --output dist/create-plugin.js",
135+
"build:umd:min": "cross-env BABEL_ENV=es NODE_ENV=production rollup src/index.js --config --output dist/create-plugin.min.js",
136+
"build:umd:watch": "npm run build:umd -- --watch",
137+
"build:watch": "echo 'build && watch the COMMONJS version of the package - for other version, run specific tasks' && npm run build:commonjs:watch",
138+
"clean": "rimraf lib dist es",
139+
"dev": "webpack-dev-server",
140+
"lint": "eslint src",
141+
"lint-fix": "eslint --fix src --ext .js",
142+
"prepare": "npm run clean && npm test && npm run build",
143+
"test": ""
87144
},
88-
"homepage": "https://github.com/arduino/arduino-create-agent-js-client#readme"
145+
"version": "1.0.3"
89146
}

src/boardConfiguration.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ 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));
37-
this.configureInProgress = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_IN_PROGRESS));
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.configureInProgress = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_IN_PROGRESS))
40+
.pipe(first())
41+
.pipe(takeUntil(this.configureDone));
3842
this.configureError = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_ERROR));
3943
this.daemon.serialMonitorMessages.subscribe(message => {
4044
this.serialMonitorContent += message;
@@ -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

Lines changed: 11 additions & 5 deletions
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, msg) {
65+
this.uploading.next({ status: this.UPLOAD_ERROR, err, msg });
66+
}
67+
6068
openChannel(cb) {
6169
this.channelOpen
6270
.subscribe(open => {
@@ -111,9 +119,7 @@ export default class Daemon {
111119
if (typeof this.stopUploadCommand === 'function') {
112120
this.stopUploadCommand();
113121
}
114-
else {
115-
throw new Error('Stop Upload not supported on Chrome OS');
116-
}
122+
throw new Error('Stop Upload not supported on Chrome OS');
117123
}
118124

119125
initUpload() {

src/socket-daemon.js

Lines changed: 15 additions & 4 deletions
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, msg) {
100+
this.downloading.next({ status: this.DOWNLOAD_ERROR, err, msg });
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,11 @@ 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+
msg: 'Upload stopped'
487+
});
477488
this.socket.emit('command', 'killprogrammer');
478489
}
479490
}

0 commit comments

Comments
 (0)