@@ -7,13 +7,13 @@ export type AvailablePorts = Record<string, [Port, Array<Board>]>;
7
7
export namespace AvailablePorts {
8
8
export function byProtocol ( availablePorts : AvailablePorts ) : Map < string , AvailablePorts > {
9
9
const grouped = new Map < string , AvailablePorts > ( ) ;
10
- for ( const address of Object . keys ( availablePorts ) ) {
11
- const [ port , boards ] = availablePorts [ address ] ;
10
+ for ( const portID of Object . keys ( availablePorts ) ) {
11
+ const [ port , boards ] = availablePorts [ portID ] ;
12
12
let ports = grouped . get ( port . protocol ) ;
13
13
if ( ! ports ) {
14
14
ports = { } as AvailablePorts ;
15
15
}
16
- ports [ address ] = [ port , boards ] ;
16
+ ports [ portID ] = [ port , boards ] ;
17
17
grouped . set ( port . protocol , ports ) ;
18
18
}
19
19
return grouped ;
@@ -43,7 +43,7 @@ export namespace AttachedBoardsChangeEvent {
43
43
const visitedDetachedPorts : Port [ ] = [ ] ;
44
44
for ( const board of attached . boards ) {
45
45
const port = board . port
46
- ? ` on ${ Port . toString ( board . port , { useLabel : true } ) } `
46
+ ? ` on ${ Port . toString ( board . port ) } `
47
47
: '' ;
48
48
rows . push ( ` - Attached board: ${ Board . toString ( board ) } ${ port } ` ) ;
49
49
if ( board . port ) {
@@ -52,7 +52,7 @@ export namespace AttachedBoardsChangeEvent {
52
52
}
53
53
for ( const board of detached . boards ) {
54
54
const port = board . port
55
- ? ` from ${ Port . toString ( board . port , { useLabel : true } ) } `
55
+ ? ` from ${ Port . toString ( board . port ) } `
56
56
: '' ;
57
57
rows . push ( ` - Detached board: ${ Board . toString ( board ) } ${ port } ` ) ;
58
58
if ( board . port ) {
@@ -62,18 +62,14 @@ export namespace AttachedBoardsChangeEvent {
62
62
for ( const port of attached . ports ) {
63
63
if ( ! visitedAttachedPorts . find ( ( p ) => Port . sameAs ( port , p ) ) ) {
64
64
rows . push (
65
- ` - New port is available on ${ Port . toString ( port , {
66
- useLabel : true ,
67
- } ) } `
65
+ ` - New port is available on ${ Port . toString ( port ) } `
68
66
) ;
69
67
}
70
68
}
71
69
for ( const port of detached . ports ) {
72
70
if ( ! visitedDetachedPorts . find ( ( p ) => Port . sameAs ( port , p ) ) ) {
73
71
rows . push (
74
- ` - Port is no longer available on ${ Port . toString ( port , {
75
- useLabel : true ,
76
- } ) } `
72
+ ` - Port is no longer available on ${ Port . toString ( port ) } `
77
73
) ;
78
74
}
79
75
}
@@ -147,12 +143,14 @@ export interface BoardsService
147
143
}
148
144
149
145
export interface Port {
146
+ // id is the combination of address and protocol
147
+ // formatted like "<address>|<protocol>" used
148
+ // to univocally recognize a port
149
+ readonly id : string ;
150
150
readonly address : string ;
151
+ readonly addressLabel : string ;
151
152
readonly protocol : string ;
152
- /**
153
- * Optional label for the protocol. For example: `Serial Port (USB)`.
154
- */
155
- readonly label ?: string ;
153
+ readonly protocolLabel : string ;
156
154
}
157
155
export namespace Port {
158
156
export function is ( arg : any ) : arg is Port {
@@ -165,14 +163,8 @@ export namespace Port {
165
163
) ;
166
164
}
167
165
168
- export function toString (
169
- port : Port ,
170
- options : { useLabel : boolean } = { useLabel : false }
171
- ) : string {
172
- if ( options . useLabel && port . label ) {
173
- return `${ port . address } ${ port . label } ` ;
174
- }
175
- return port . address ;
166
+ export function toString ( port : Port ) : string {
167
+ return `${ port . addressLabel } ${ port . protocolLabel } ` ;
176
168
}
177
169
178
170
export function compare ( left : Port , right : Port ) : number {
@@ -192,29 +184,12 @@ export namespace Port {
192
184
return naturalCompare ( left . address ! , right . address ! ) ;
193
185
}
194
186
195
- export function equals (
187
+ export function sameAs (
196
188
left : Port | undefined ,
197
189
right : Port | undefined
198
190
) : boolean {
199
191
if ( left && right ) {
200
- return (
201
- left . address === right . address &&
202
- left . protocol === right . protocol &&
203
- ( left . label || '' ) === ( right . label || '' )
204
- ) ;
205
- }
206
- return left === right ;
207
- }
208
-
209
- export function sameAs (
210
- left : Port | undefined ,
211
- right : Port | string | undefined
212
- ) {
213
- if ( left && right ) {
214
- if ( typeof right === 'string' ) {
215
- return left . address === right ;
216
- }
217
- return left . address === right . address ;
192
+ return left . address === right . address && left . protocol === right . protocol ;
218
193
}
219
194
return false ;
220
195
}
0 commit comments