@@ -5,7 +5,7 @@ import { ArduinoComponent } from './arduino-component';
5
5
6
6
export type AvailablePorts = Record < string , [ Port , Array < Board > ] > ;
7
7
export namespace AvailablePorts {
8
- export function byProtocol (
8
+ export function groupByProtocol (
9
9
availablePorts : AvailablePorts
10
10
) : Map < string , AvailablePorts > {
11
11
const grouped = new Map < string , AvailablePorts > ( ) ;
@@ -20,6 +20,21 @@ export namespace AvailablePorts {
20
20
}
21
21
return grouped ;
22
22
}
23
+ export function split (
24
+ state : AvailablePorts
25
+ ) : Readonly < { boards : Board [ ] ; ports : Port [ ] } > {
26
+ const availablePorts : Port [ ] = [ ] ;
27
+ const attachedBoards : Board [ ] = [ ] ;
28
+ for ( const key of Object . keys ( state ) ) {
29
+ const [ port , boards ] = state [ key ] ;
30
+ availablePorts . push ( port ) ;
31
+ attachedBoards . push ( ...boards ) ;
32
+ }
33
+ return {
34
+ boards : attachedBoards ,
35
+ ports : availablePorts ,
36
+ } ;
37
+ }
23
38
}
24
39
25
40
export interface AttachedBoardsChangeEvent {
@@ -117,16 +132,6 @@ export const BoardsService = Symbol('BoardsService');
117
132
export interface BoardsService
118
133
extends Installable < BoardsPackage > ,
119
134
Searchable < BoardsPackage > {
120
- /**
121
- * Deprecated. `getState` should be used to correctly map a board with a port.
122
- * @deprecated
123
- */
124
- getAttachedBoards ( ) : Promise < Board [ ] > ;
125
- /**
126
- * Deprecated. `getState` should be used to correctly map a board with a port.
127
- * @deprecated
128
- */
129
- getAvailablePorts ( ) : Promise < Port [ ] > ;
130
135
getState ( ) : Promise < AvailablePorts > ;
131
136
getBoardDetails ( options : { fqbn : string } ) : Promise < BoardDetails | undefined > ;
132
137
getBoardPackage ( options : { id : string } ) : Promise < BoardsPackage | undefined > ;
@@ -141,28 +146,55 @@ export interface BoardsService
141
146
}
142
147
143
148
export interface Port {
144
- // id is the combination of address and protocol
145
- // formatted like "<address>|<protocol>" used
146
- // to uniquely recognize a port
147
- readonly id : string ;
148
149
readonly address : string ;
149
150
readonly addressLabel : string ;
150
151
readonly protocol : string ;
151
152
readonly protocolLabel : string ;
153
+ readonly properties ?: Record < string , string > ;
152
154
}
153
155
export namespace Port {
154
- export function is ( arg : any ) : arg is Port {
155
- return (
156
- ! ! arg &&
157
- 'address' in arg &&
158
- typeof arg [ 'address' ] === 'string' &&
159
- 'protocol' in arg &&
160
- typeof arg [ 'protocol' ] === 'string'
161
- ) ;
156
+ export type Properties = Record < string , string > ;
157
+ export namespace Properties {
158
+ export function create (
159
+ properties : [ string , string ] [ ] | undefined
160
+ ) : Properties {
161
+ if ( ! properties ) {
162
+ return { } ;
163
+ }
164
+ return properties . reduce ( ( acc , curr ) => {
165
+ const [ key , value ] = curr ;
166
+ acc [ key ] = value ;
167
+ return acc ;
168
+ } , { } as Record < string , string > ) ;
169
+ }
170
+ }
171
+ export function is ( arg : unknown ) : arg is Port {
172
+ if ( typeof arg === 'object' ) {
173
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
174
+ const object = arg as any ;
175
+ return (
176
+ 'address' in object &&
177
+ typeof object [ 'address' ] === 'string' &&
178
+ 'addressLabel' in object &&
179
+ typeof object [ 'addressLabel' ] === 'string' &&
180
+ 'protocol' in object &&
181
+ typeof object [ 'protocol' ] === 'string' &&
182
+ 'protocolLabel' in object &&
183
+ typeof object [ 'protocolLabel' ] === 'string'
184
+ ) ;
185
+ }
186
+ return false ;
187
+ }
188
+
189
+ /**
190
+ * Key is the combination of address and protocol formatted like `'${address}|${protocol}'` used to uniquely identify a port.
191
+ */
192
+ export function keyOf ( { address, protocol } : Port ) : string {
193
+ return `${ address } |${ protocol } ` ;
162
194
}
163
195
164
- export function toString ( port : Port ) : string {
165
- return `${ port . addressLabel } ${ port . protocolLabel } ` ;
196
+ export function toString ( { addressLabel , protocolLabel } : Port ) : string {
197
+ return `${ addressLabel } ${ protocolLabel } ` ;
166
198
}
167
199
168
200
export function compare ( left : Port , right : Port ) : number {
0 commit comments