@@ -36,6 +36,7 @@ import {
36
36
BehaviorSubject ,
37
37
interval
38
38
} from 'rxjs' ;
39
+ import { filter } from 'rxjs/operators' ;
39
40
import ReaderWriter from './reader-writer' ;
40
41
41
42
// Required agent version
@@ -55,11 +56,6 @@ const LOOKUP_PORT_END = 9000;
55
56
const CANT_FIND_AGENT_MESSAGE = 'Arduino Create Agent cannot be found' ;
56
57
let updateAttempts = 0 ;
57
58
58
- export const AGENT_STATUS_FOUND = 'AGENT_FOUND' ;
59
- export const AGENT_STATUS_NOT_FOUND = 'AGENT_NOT_FOUND' ;
60
- export const WS_STATUS_CONNECTED = 'WS_CONNECTED' ;
61
- export const WS_STATUS_DISCONNECTED = 'WS_DISCONNECTED' ;
62
-
63
59
let orderedPluginAddresses = [ LOOPBACK_ADDRESS , LOOPBACK_HOSTNAME ] ;
64
60
65
61
if ( browser . name !== 'chrome' && browser . name !== 'firefox' ) {
@@ -70,18 +66,23 @@ export default class SocketDaemon {
70
66
constructor ( ) {
71
67
this . selectedProtocol = PROTOCOL . HTTP ;
72
68
this . agentInfo = { } ;
73
- this . found = false ;
74
69
75
- this . agentDiscoveryStatus = new BehaviorSubject ( AGENT_STATUS_NOT_FOUND ) ;
76
- this . wsConnectionStatus = new BehaviorSubject ( WS_STATUS_DISCONNECTED ) ;
70
+ this . agentFound = new BehaviorSubject ( false ) ;
71
+ this . wsConnected = new BehaviorSubject ( false ) ;
77
72
this . wsError = new Subject ( ) ;
78
73
79
74
this . readerWriter = new ReaderWriter ( ) ;
80
- this . wsConnectionStatus . subscribe ( status => {
81
- if ( status === WS_STATUS_CONNECTED ) {
82
- this . readerWriter . initSocket ( this . socket ) ;
83
- }
84
- } ) ;
75
+ this . wsConnected
76
+ . pipe ( filter ( status => status ) )
77
+ . subscribe ( ( ) => {
78
+ this . readerWriter . initSocket ( this . socket )
79
+ } ) ;
80
+
81
+ this . agentFound
82
+ . pipe ( filter ( status => ! status ) )
83
+ . subscribe ( ( ) => {
84
+ this . agentInfo = { } ;
85
+ } ) ;
85
86
}
86
87
87
88
/**
@@ -92,7 +93,7 @@ export default class SocketDaemon {
92
93
findAgent ( ) {
93
94
const find = ( ) => this . tryAllPorts ( )
94
95
. catch ( err => {
95
- this . agentDiscoveryStatus . next ( AGENT_STATUS_NOT_FOUND ) ;
96
+ this . agentFound . next ( false ) ;
96
97
return err ;
97
98
} )
98
99
. finally ( ( ) => {
@@ -127,10 +128,10 @@ export default class SocketDaemon {
127
128
128
129
return Promise . all ( pluginLookups )
129
130
. then ( responses => {
130
- this . found = responses . some ( r => {
131
+ const found = responses . some ( r => {
131
132
if ( r && r . response && r . response . status === 200 ) {
132
133
this . agentInfo = r . data ;
133
- this . agentDiscoveryStatus . next ( AGENT_STATUS_FOUND ) ;
134
+ this . agentFound . next ( true ) ;
134
135
this . wsConnect ( ) ;
135
136
if ( r . response . url . indexOf ( PROTOCOL . HTTPS ) === 0 ) {
136
137
this . selectedProtocol = PROTOCOL . HTTPS ;
@@ -145,7 +146,7 @@ export default class SocketDaemon {
145
146
return false ;
146
147
} ) ;
147
148
148
- if ( this . found ) {
149
+ if ( found ) {
149
150
if ( this . agentInfo . version && ( semVerCompare ( this . agentInfo . version , MIN_VERSION ) >= 0 || this . agentInfo . version . indexOf ( 'dev' ) !== - 1 ) ) {
150
151
return this . agentInfo ;
151
152
}
@@ -180,7 +181,7 @@ export default class SocketDaemon {
180
181
this . socket . emit ( 'command' , 'downloadtool windows-drivers latest arduino keep' ) ;
181
182
this . socket . emit ( 'command' , 'downloadtool bossac 1.7.0 arduino keep' ) ;
182
183
183
- this . wsConnectionStatus . next ( WS_STATUS_CONNECTED ) ;
184
+ this . wsConnected . next ( true ) ;
184
185
185
186
// Periodically asks for the ports
186
187
if ( ! this . portsPollingSubscription ) {
@@ -195,7 +196,7 @@ export default class SocketDaemon {
195
196
if ( this . portsPollingSubscription ) {
196
197
this . portsPollingSubscription . unsubscribe ( ) ;
197
198
}
198
- this . wsConnectionStatus . next ( WS_STATUS_DISCONNECTED ) ;
199
+ this . wsConnected . next ( false ) ;
199
200
this . findAgent ( ) ;
200
201
} ) ;
201
202
}
@@ -238,7 +239,7 @@ export default class SocketDaemon {
238
239
* @return {Promise }
239
240
*/
240
241
stopPlugin ( ) {
241
- if ( this . found ) {
242
+ if ( this . agentFound . getValue ( ) ) {
242
243
return fetch ( `${ this . agentInfo [ this . selectedProtocol ] } /pause` , { method : 'POST' } ) ;
243
244
}
244
245
}
0 commit comments