1
1
import { injectable , inject , named } from 'inversify' ;
2
2
import { ILogger } from '@theia/core/lib/common/logger' ;
3
+ import { deepClone } from '@theia/core/lib/common/objects' ;
3
4
import { MaybePromise } from '@theia/core/lib/common/types' ;
4
5
import { Event , Emitter } from '@theia/core/lib/common/event' ;
5
- import { deepClone , notEmpty } from '@theia/core/lib/common/objects' ;
6
6
import { FrontendApplicationContribution , LocalStorageService } from '@theia/core/lib/browser' ;
7
- import { BoardsService , ConfigOption , Installable , BoardDetails } from '../../common/protocol ' ;
7
+ import { notEmpty } from '../../common/utils ' ;
8
8
import { BoardsServiceClientImpl } from './boards-service-client-impl' ;
9
+ import { BoardsService , ConfigOption , Installable , BoardDetails , Programmer } from '../../common/protocol' ;
9
10
10
11
@injectable ( )
11
- export class BoardsConfigStore implements FrontendApplicationContribution {
12
+ export class BoardsDataStore implements FrontendApplicationContribution {
12
13
13
14
@inject ( ILogger )
14
15
@named ( 'store' )
@@ -60,39 +61,59 @@ export class BoardsConfigStore implements FrontendApplicationContribution {
60
61
fqbn : string ,
61
62
boardsPackageVersion : MaybePromise < Installable . Version | undefined > = this . getBoardsPackageVersion ( fqbn ) ) : Promise < string > {
62
63
63
- const configOptions = await this . getConfig ( fqbn , boardsPackageVersion ) ;
64
+ const { configOptions } = await this . getData ( fqbn , boardsPackageVersion ) ;
64
65
return ConfigOption . decorate ( fqbn , configOptions ) ;
65
66
}
66
67
67
- async getConfig (
68
+ async getData (
68
69
fqbn : string ,
69
- boardsPackageVersion : MaybePromise < Installable . Version | undefined > = this . getBoardsPackageVersion ( fqbn ) ) : Promise < ConfigOption [ ] > {
70
+ boardsPackageVersion : MaybePromise < Installable . Version | undefined > = this . getBoardsPackageVersion ( fqbn ) ) : Promise < BoardsDataStore . Data > {
70
71
71
72
const version = await boardsPackageVersion ;
72
73
if ( ! version ) {
73
- return [ ] ;
74
+ return BoardsDataStore . Data . EMPTY ;
74
75
}
75
76
const key = this . getStorageKey ( fqbn , version ) ;
76
- let configOptions = await this . storageService . getData < ConfigOption [ ] | undefined > ( key , undefined ) ;
77
- if ( configOptions ) {
78
- return configOptions ;
77
+ let data = await this . storageService . getData < BoardsDataStore . Data | undefined > ( key , undefined ) ;
78
+ if ( data ) {
79
+ if ( data . programmers !== undefined ) { // to be backward compatible. We did not save the `programmers` into the `localStorage`.
80
+ return data ;
81
+ }
79
82
}
80
83
81
- const details = await this . getBoardDetailsSafe ( fqbn ) ;
82
- if ( ! details ) {
83
- return [ ] ;
84
+ const boardDetails = await this . getBoardDetailsSafe ( fqbn ) ;
85
+ if ( ! boardDetails ) {
86
+ return BoardsDataStore . Data . EMPTY ;
84
87
}
85
88
86
- configOptions = details . configOptions ;
87
- await this . storageService . setData ( key , configOptions ) ;
88
- return configOptions ;
89
+ data = { configOptions : boardDetails . configOptions , programmers : boardDetails . programmers } ;
90
+ await this . storageService . setData ( key , data ) ;
91
+ return data ;
92
+ }
93
+
94
+ async selectProgrammer (
95
+ { fqbn, programmer } : { fqbn : string , programmer : Programmer } ,
96
+ boardsPackageVersion : MaybePromise < Installable . Version | undefined > = this . getBoardsPackageVersion ( fqbn ) ) : Promise < boolean > {
97
+
98
+ const { configOptions, programmers } = deepClone ( await this . getData ( fqbn , boardsPackageVersion ) ) ;
99
+ if ( ! programmers . find ( p => Programmer . equals ( programmer , p ) ) ) {
100
+ return false ;
101
+ }
102
+
103
+ const version = await boardsPackageVersion ;
104
+ if ( ! version ) {
105
+ return false ;
106
+ }
107
+ await this . setData ( { fqbn, data : { configOptions, programmers } , version } ) ;
108
+ this . fireChanged ( ) ;
109
+ return true ;
89
110
}
90
111
91
- async setSelected (
112
+ async selectConfigOption (
92
113
{ fqbn, option, selectedValue } : { fqbn : string , option : string , selectedValue : string } ,
93
114
boardsPackageVersion : MaybePromise < Installable . Version | undefined > = this . getBoardsPackageVersion ( fqbn ) ) : Promise < boolean > {
94
115
95
- const configOptions = deepClone ( await this . getConfig ( fqbn , boardsPackageVersion ) ) ;
116
+ const { configOptions, programmers } = deepClone ( await this . getData ( fqbn , boardsPackageVersion ) ) ;
96
117
const configOption = configOptions . find ( c => c . option === option ) ;
97
118
if ( ! configOption ) {
98
119
return false ;
@@ -113,16 +134,16 @@ export class BoardsConfigStore implements FrontendApplicationContribution {
113
134
if ( ! version ) {
114
135
return false ;
115
136
}
116
- await this . setConfig ( { fqbn, configOptions, version } ) ;
137
+ await this . setData ( { fqbn, data : { configOptions, programmers } , version } ) ;
117
138
this . fireChanged ( ) ;
118
139
return true ;
119
140
}
120
141
121
- protected async setConfig (
122
- { fqbn, configOptions , version } : { fqbn : string , configOptions : ConfigOption [ ] , version : Installable . Version } ) : Promise < void > {
142
+ protected async setData (
143
+ { fqbn, data , version } : { fqbn : string , data : BoardsDataStore . Data , version : Installable . Version } ) : Promise < void > {
123
144
124
145
const key = this . getStorageKey ( fqbn , version ) ;
125
- return this . storageService . setData ( key , configOptions ) ;
146
+ return this . storageService . setData ( key , data ) ;
126
147
}
127
148
128
149
protected getStorageKey ( fqbn : string , version : Installable . Version ) : string {
@@ -159,3 +180,16 @@ export class BoardsConfigStore implements FrontendApplicationContribution {
159
180
}
160
181
161
182
}
183
+
184
+ export namespace BoardsDataStore {
185
+ export interface Data {
186
+ readonly configOptions : ConfigOption [ ] ;
187
+ readonly programmers : Programmer [ ] ;
188
+ }
189
+ export namespace Data {
190
+ export const EMPTY : Data = {
191
+ configOptions : [ ] ,
192
+ programmers : [ ]
193
+ } ;
194
+ }
195
+ }
0 commit comments