-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathboards-service-client-impl.ts
71 lines (57 loc) · 2.77 KB
/
boards-service-client-impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { injectable, inject, postConstruct } from 'inversify';
import { Emitter, ILogger } from '@theia/core';
import { BoardsServiceClient, AttachedBoardsChangeEvent, BoardInstalledEvent, AttachedSerialBoard } from '../../common/protocol/boards-service';
import { BoardsConfig } from './boards-config';
import { LocalStorageService } from '@theia/core/lib/browser';
@injectable()
export class BoardsServiceClientImpl implements BoardsServiceClient {
@inject(ILogger)
protected logger: ILogger;
@inject(LocalStorageService)
protected storageService: LocalStorageService;
protected readonly onAttachedBoardsChangedEmitter = new Emitter<AttachedBoardsChangeEvent>();
protected readonly onBoardInstalledEmitter = new Emitter<BoardInstalledEvent>();
protected readonly onSelectedBoardsConfigChangedEmitter = new Emitter<BoardsConfig.Config>();
protected _boardsConfig: BoardsConfig.Config = {};
readonly onBoardsChanged = this.onAttachedBoardsChangedEmitter.event;
readonly onBoardInstalled = this.onBoardInstalledEmitter.event;
readonly onBoardsConfigChanged = this.onSelectedBoardsConfigChangedEmitter.event;
@postConstruct()
protected init(): void {
this.loadState();
}
notifyAttachedBoardsChanged(event: AttachedBoardsChangeEvent): void {
this.logger.info('Attached boards changed: ', JSON.stringify(event));
const { boards } = event.newState;
const { selectedPort, selectedBoard } = this.boardsConfig;
this.onAttachedBoardsChangedEmitter.fire(event);
// Dynamically unset the port if there is not corresponding attached boards for it.
if (!!selectedPort && boards.filter(AttachedSerialBoard.is).map(({ port }) => port).indexOf(selectedPort) === -1) {
this.boardsConfig = {
selectedBoard,
selectedPort: undefined
};
}
}
notifyBoardInstalled(event: BoardInstalledEvent): void {
this.logger.info('Board installed: ', JSON.stringify(event));
this.onBoardInstalledEmitter.fire(event);
}
set boardsConfig(config: BoardsConfig.Config) {
this.logger.info('Board config changed: ', JSON.stringify(config));
this._boardsConfig = config;
this.saveState().then(() => this.onSelectedBoardsConfigChangedEmitter.fire(this._boardsConfig));
}
get boardsConfig(): BoardsConfig.Config {
return this._boardsConfig;
}
protected saveState(): Promise<void> {
return this.storageService.setData('boards-config', this.boardsConfig);
}
protected async loadState(): Promise<void> {
const boardsConfig = await this.storageService.getData<BoardsConfig.Config>('boards-config');
if (boardsConfig) {
this.boardsConfig = boardsConfig;
}
}
}