@@ -5,6 +5,7 @@ import { scope } from '@platform/env/scope';
5
5
import { Logger } from '@platform/log' ;
6
6
import { InstalledPluginDesc , InvalidPluginDesc , PluginDescription } from './desc' ;
7
7
import { bridge } from '@service/bridge' ;
8
+ import { error } from '@platform/log/utils' ;
8
9
9
10
export class Provider {
10
11
protected log : Logger ;
@@ -22,17 +23,27 @@ export class Provider {
22
23
load : Subject < void > ;
23
24
state : Subject < void > ;
24
25
selected : Subject < string > ;
26
+ // true - add starting; false - finished
27
+ add : Subject < boolean > ;
28
+ // true - add starting; false - finished
29
+ remove : Subject < boolean > ;
25
30
} > = new Subjects ( {
26
31
load : new Subject < void > ( ) ,
27
32
state : new Subject < void > ( ) ,
28
33
selected : new Subject < string > ( ) ,
34
+ add : new Subject < boolean > ( ) ,
35
+ remove : new Subject < boolean > ( ) ,
29
36
} ) ;
30
37
public selected : PluginDescription | undefined ;
31
38
public state : {
32
39
loading : boolean ;
40
+ adding : boolean ;
41
+ removing : boolean ;
33
42
error : string | undefined ;
34
43
} = {
35
44
loading : false ,
45
+ adding : false ,
46
+ removing : false ,
36
47
error : undefined ,
37
48
} ;
38
49
@@ -126,9 +137,56 @@ export class Provider {
126
137
this . subjects . get ( ) . selected . emit ( path ) ;
127
138
}
128
139
public addPlugin ( pluginPath : string ) : Promise < void > {
129
- return plugins . addPlugin ( pluginPath ) ;
140
+ if ( this . isBusy ( ) ) {
141
+ return Promise . reject (
142
+ new Error (
143
+ `Cannot add plugin, because previous plugin operation is still in progress.` ,
144
+ ) ,
145
+ ) ;
146
+ }
147
+ this . state . adding = true ;
148
+ this . subjects . get ( ) . add . emit ( this . state . adding ) ;
149
+ return plugins . addPlugin ( pluginPath ) . finally ( ( ) => {
150
+ this . state . adding = false ;
151
+ this . subjects . get ( ) . add . emit ( this . state . adding ) ;
152
+ } ) ;
153
+ }
154
+ public async removePlugin ( pluginPath : string ) : Promise < void > {
155
+ if ( this . isBusy ( ) ) {
156
+ return Promise . reject (
157
+ new Error (
158
+ `Cannot add plugin, because previous plugin operation is still in progress.` ,
159
+ ) ,
160
+ ) ;
161
+ }
162
+ this . state . removing = true ;
163
+ this . subjects . get ( ) . remove . emit ( this . state . removing ) ;
164
+ try {
165
+ await plugins . removePlugin ( pluginPath ) ;
166
+ await this . load ( true ) ;
167
+ const selected = this . selected ;
168
+ if ( selected !== undefined ) {
169
+ if (
170
+ [
171
+ ...this . plugins . installed ,
172
+ ...this . plugins . available ,
173
+ ...this . plugins . invalid ,
174
+ ] . find ( ( pl ) => pl . entity . dir_path === selected . getPath ( ) ) !== undefined
175
+ ) {
176
+ this . selected = undefined ;
177
+ }
178
+ }
179
+ this . state . removing = false ;
180
+ this . subjects . get ( ) . remove . emit ( this . state . removing ) ;
181
+ this . subjects . get ( ) . state . emit ( ) ;
182
+ return Promise . resolve ( ) ;
183
+ } catch ( err ) {
184
+ this . state . removing = false ;
185
+ this . subjects . get ( ) . remove . emit ( this . state . removing ) ;
186
+ return Promise . reject ( new Error ( error ( err ) ) ) ;
187
+ }
130
188
}
131
- public removePlugin ( pluginPath : string ) : Promise < void > {
132
- return plugins . removePlugin ( pluginPath ) ;
189
+ public isBusy ( ) : boolean {
190
+ return this . state . adding || this . state . loading || this . state . removing ;
133
191
}
134
192
}
0 commit comments