Skip to content

Commit 87f54f2

Browse files
author
Alberto Iannaccone
committed
refactor ide updater dialog: clean-up code and rename events
1 parent bfecd88 commit 87f54f2

File tree

5 files changed

+77
-79
lines changed

5 files changed

+77
-79
lines changed

Diff for: arduino-ide-extension/src/browser/dialogs/ide-updater/ide-updater-component.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import ReactMarkdown from 'react-markdown';
66
import { ProgressInfo, UpdateInfo } from '../../../common/protocol/ide-updater';
77
import ProgressBar from '../../components/ProgressBar';
88

9-
export type UpdateProgress = {
9+
export interface UpdateProgress {
1010
progressInfo?: ProgressInfo | undefined;
1111
downloadFinished?: boolean;
1212
downloadStarted?: boolean;
1313
error?: Error;
14-
};
14+
}
1515

16-
export type IDEUpdaterComponentProps = {
16+
export interface IDEUpdaterComponentProps {
1717
updateInfo: UpdateInfo;
1818
updateProgress: UpdateProgress;
19-
};
19+
}
2020

2121
export const IDEUpdaterComponent = ({
2222
updateInfo,

Diff for: arduino-ide-extension/src/browser/dialogs/ide-updater/ide-updater-dialog.tsx

+27-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import * as React from '@theia/core/shared/react';
2-
import { inject, injectable } from '@theia/core/shared/inversify';
2+
import {
3+
inject,
4+
injectable,
5+
postConstruct,
6+
} from '@theia/core/shared/inversify';
37
import { DialogProps } from '@theia/core/lib/browser/dialogs';
48
import { AbstractDialog } from '../../theia/dialogs/dialogs';
59
import { Widget } from '@theia/core/shared/@phosphor/widgets';
610
import { Message } from '@theia/core/shared/@phosphor/messaging';
711
import { ReactWidget } from '@theia/core/lib/browser/widgets/react-widget';
8-
import { Disposable, nls } from '@theia/core';
12+
import { nls } from '@theia/core';
913
import { IDEUpdaterComponent, UpdateProgress } from './ide-updater-component';
1014
import {
1115
IDEUpdater,
@@ -29,7 +33,7 @@ export class IDEUpdaterDialogWidget extends ReactWidget {
2933
this.update();
3034
}
3135

32-
setUpdateProgress(updateProgress: UpdateProgress): void {
36+
mergeUpdateProgress(updateProgress: UpdateProgress): void {
3337
this._updateProgress = { ...this._updateProgress, ...updateProgress };
3438
this.update();
3539
}
@@ -57,9 +61,6 @@ export class IDEUpdaterDialogProps extends DialogProps {}
5761

5862
@injectable()
5963
export class IDEUpdaterDialog extends AbstractDialog<UpdateInfo> {
60-
private onError: Disposable;
61-
private onDownloadProgressChanged: Disposable;
62-
onDownloadFinished: Disposable;
6364
@inject(IDEUpdaterDialogWidget)
6465
private readonly widget: IDEUpdaterDialogWidget;
6566

@@ -90,32 +91,19 @@ export class IDEUpdaterDialog extends AbstractDialog<UpdateInfo> {
9091
this.acceptButton = undefined;
9192
}
9293

93-
private init(): void {
94-
this.widget.setUpdateProgress({
95-
progressInfo: undefined,
96-
downloadStarted: false,
97-
downloadFinished: false,
98-
error: undefined,
94+
@postConstruct()
95+
protected init(): void {
96+
this.updaterClient.onUpdaterDidFail((error) => {
97+
this.appendErrorButtons();
98+
this.widget.mergeUpdateProgress({ error });
99+
});
100+
this.updaterClient.onDownloadProgressDidChange((progressInfo) => {
101+
this.widget.mergeUpdateProgress({ progressInfo });
102+
});
103+
this.updaterClient.onDownloadDidFinish(() => {
104+
this.appendInstallButtons();
105+
this.widget.mergeUpdateProgress({ downloadFinished: true });
99106
});
100-
if (!this.onError) {
101-
this.onError = this.updaterClient.onError((error) => {
102-
this.appendErrorButtons();
103-
this.widget.setUpdateProgress({ error });
104-
});
105-
}
106-
if (!this.onDownloadProgressChanged) {
107-
this.onDownloadProgressChanged =
108-
this.updaterClient.onDownloadProgressChanged((progressInfo) => {
109-
this.widget.setUpdateProgress({ progressInfo });
110-
});
111-
}
112-
if (!this.onDownloadFinished) {
113-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
114-
this.onDownloadFinished = this.updaterClient.onDownloadFinished((_) => {
115-
this.appendInstallButtons();
116-
this.widget.setUpdateProgress({ downloadFinished: true });
117-
});
118-
}
119107
}
120108

121109
get value(): UpdateInfo {
@@ -217,23 +205,28 @@ export class IDEUpdaterDialog extends AbstractDialog<UpdateInfo> {
217205
}
218206

219207
private startDownload(): void {
220-
this.widget.setUpdateProgress({
208+
this.widget.mergeUpdateProgress({
221209
downloadStarted: true,
222210
});
223211
this.clearButtons();
224212
this.updater.downloadUpdate();
225213
}
226214

227215
private closeAndInstall() {
228-
this.updater.quitAndInstall.bind(this);
216+
this.updater.quitAndInstall();
229217
this.close();
230218
}
231219

232220
override async open(
233221
data: UpdateInfo | undefined = undefined
234222
): Promise<UpdateInfo | undefined> {
235223
if (data && data.version) {
236-
this.init();
224+
this.widget.mergeUpdateProgress({
225+
progressInfo: undefined,
226+
downloadStarted: false,
227+
downloadFinished: false,
228+
error: undefined,
229+
});
237230
this.widget.setUpdateInfo(data);
238231
return super.open();
239232
}

Diff for: arduino-ide-extension/src/browser/ide-updater/ide-updater-client-impl.ts

+29-22
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,43 @@ import { IDEUpdaterClient } from '../../common/protocol/ide-updater';
55

66
@injectable()
77
export class IDEUpdaterClientImpl implements IDEUpdaterClient {
8-
protected readonly onErrorEmitter = new Emitter<Error>();
9-
protected readonly onCheckingForUpdateEmitter = new Emitter<void>();
10-
protected readonly onUpdateAvailableEmitter = new Emitter<UpdateInfo>();
11-
protected readonly onUpdateNotAvailableEmitter = new Emitter<UpdateInfo>();
12-
protected readonly onDownloadProgressEmitter = new Emitter<ProgressInfo>();
13-
protected readonly onDownloadFinishedEmitter = new Emitter<UpdateInfo>();
8+
protected readonly onUpdaterDidFailEmitter = new Emitter<Error>();
9+
protected readonly onUpdaterDidCheckForUpdateEmitter = new Emitter<void>();
10+
protected readonly onUpdaterDidFindUpdateAvailableEmitter =
11+
new Emitter<UpdateInfo>();
12+
protected readonly onUpdaterDidNotFindUpdateAvailableEmitter =
13+
new Emitter<UpdateInfo>();
14+
protected readonly onDownloadProgressDidChangeEmitter =
15+
new Emitter<ProgressInfo>();
16+
protected readonly onDownloadDidFinishEmitter = new Emitter<UpdateInfo>();
1417

15-
readonly onError = this.onErrorEmitter.event;
16-
readonly onCheckingForUpdate = this.onCheckingForUpdateEmitter.event;
17-
readonly onUpdateAvailable = this.onUpdateAvailableEmitter.event;
18-
readonly onUpdateNotAvailable = this.onUpdateNotAvailableEmitter.event;
19-
readonly onDownloadProgressChanged = this.onDownloadProgressEmitter.event;
20-
readonly onDownloadFinished = this.onDownloadFinishedEmitter.event;
18+
readonly onUpdaterDidFail = this.onUpdaterDidFailEmitter.event;
19+
readonly onUpdaterDidCheckForUpdate =
20+
this.onUpdaterDidCheckForUpdateEmitter.event;
21+
readonly onUpdaterDidFindUpdateAvailable =
22+
this.onUpdaterDidFindUpdateAvailableEmitter.event;
23+
readonly onUpdaterDidNotFindUpdateAvailable =
24+
this.onUpdaterDidNotFindUpdateAvailableEmitter.event;
25+
readonly onDownloadProgressDidChange =
26+
this.onDownloadProgressDidChangeEmitter.event;
27+
readonly onDownloadDidFinish = this.onDownloadDidFinishEmitter.event;
2128

22-
notifyError(message: Error): void {
23-
this.onErrorEmitter.fire(message);
29+
notifyUpdaterFailed(message: Error): void {
30+
this.onUpdaterDidFailEmitter.fire(message);
2431
}
25-
notifyCheckingForUpdate(message: void): void {
26-
this.onCheckingForUpdateEmitter.fire(message);
32+
notifyCheckedForUpdate(message: void): void {
33+
this.onUpdaterDidCheckForUpdateEmitter.fire(message);
2734
}
28-
notifyUpdateAvailable(message: UpdateInfo): void {
29-
this.onUpdateAvailableEmitter.fire(message);
35+
notifyUpdateAvailableFound(message: UpdateInfo): void {
36+
this.onUpdaterDidFindUpdateAvailableEmitter.fire(message);
3037
}
31-
notifyUpdateNotAvailable(message: UpdateInfo): void {
32-
this.onUpdateNotAvailableEmitter.fire(message);
38+
notifyUpdateAvailableNotFound(message: UpdateInfo): void {
39+
this.onUpdaterDidNotFindUpdateAvailableEmitter.fire(message);
3340
}
3441
notifyDownloadProgressChanged(message: ProgressInfo): void {
35-
this.onDownloadProgressEmitter.fire(message);
42+
this.onDownloadProgressDidChangeEmitter.fire(message);
3643
}
3744
notifyDownloadFinished(message: UpdateInfo): void {
38-
this.onDownloadFinishedEmitter.fire(message);
45+
this.onDownloadDidFinishEmitter.fire(message);
3946
}
4047
}

Diff for: arduino-ide-extension/src/common/protocol/ide-updater.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ export interface IDEUpdater extends JsonRpcServer<IDEUpdaterClient> {
5656

5757
export const IDEUpdaterClient = Symbol('IDEUpdaterClient');
5858
export interface IDEUpdaterClient {
59-
onError: Event<Error>;
60-
onCheckingForUpdate: Event<void>;
61-
onUpdateAvailable: Event<UpdateInfo>;
62-
onUpdateNotAvailable: Event<UpdateInfo>;
63-
onDownloadProgressChanged: Event<ProgressInfo>;
64-
onDownloadFinished: Event<UpdateInfo>;
65-
notifyError(message: Error): void;
66-
notifyCheckingForUpdate(message: void): void;
67-
notifyUpdateAvailable(message: UpdateInfo): void;
68-
notifyUpdateNotAvailable(message: UpdateInfo): void;
59+
onUpdaterDidFail: Event<Error>;
60+
onUpdaterDidCheckForUpdate: Event<void>;
61+
onUpdaterDidFindUpdateAvailable: Event<UpdateInfo>;
62+
onUpdaterDidNotFindUpdateAvailable: Event<UpdateInfo>;
63+
onDownloadProgressDidChange: Event<ProgressInfo>;
64+
onDownloadDidFinish: Event<UpdateInfo>;
65+
notifyUpdaterFailed(message: Error): void;
66+
notifyCheckedForUpdate(message: void): void;
67+
notifyUpdateAvailableFound(message: UpdateInfo): void;
68+
notifyUpdateAvailableNotFound(message: UpdateInfo): void;
6969
notifyDownloadProgressChanged(message: ProgressInfo): void;
7070
notifyDownloadFinished(message: UpdateInfo): void;
7171
}

Diff for: arduino-ide-extension/src/electron-main/ide-updater/ide-updater-impl.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export class IDEUpdaterImpl implements IDEUpdater {
1919

2020
constructor() {
2121
this.updater.on('checking-for-update', (e) => {
22-
this.clients.forEach((c) => c.notifyCheckingForUpdate(e));
22+
this.clients.forEach((c) => c.notifyCheckedForUpdate(e));
2323
});
2424
this.updater.on('update-available', (e) => {
25-
this.clients.forEach((c) => c.notifyUpdateAvailable(e));
25+
this.clients.forEach((c) => c.notifyUpdateAvailableFound(e));
2626
});
2727
this.updater.on('update-not-available', (e) => {
28-
this.clients.forEach((c) => c.notifyUpdateNotAvailable(e));
28+
this.clients.forEach((c) => c.notifyUpdateAvailableNotFound(e));
2929
});
3030
this.updater.on('download-progress', (e) => {
3131
this.clients.forEach((c) => c.notifyDownloadProgressChanged(e));
@@ -34,7 +34,7 @@ export class IDEUpdaterImpl implements IDEUpdater {
3434
this.clients.forEach((c) => c.notifyDownloadFinished(e));
3535
});
3636
this.updater.on('error', (e) => {
37-
this.clients.forEach((c) => c.notifyError(e));
37+
this.clients.forEach((c) => c.notifyUpdaterFailed(e));
3838
});
3939
}
4040

@@ -58,10 +58,8 @@ export class IDEUpdaterImpl implements IDEUpdater {
5858
this.isAlreadyChecked = true;
5959
}
6060

61-
const {
62-
updateInfo,
63-
cancellationToken,
64-
} = await this.updater.checkForUpdates();
61+
const { updateInfo, cancellationToken } =
62+
await this.updater.checkForUpdates();
6563

6664
this.cancellationToken = cancellationToken;
6765
if (this.updater.currentVersion.compare(updateInfo.version) === -1) {
@@ -104,7 +102,7 @@ export class IDEUpdaterImpl implements IDEUpdater {
104102
await this.updater.downloadUpdate(this.cancellationToken);
105103
} catch (e) {
106104
if (e.message === 'cancelled') return;
107-
this.clients.forEach((c) => c.notifyError(e));
105+
this.clients.forEach((c) => c.notifyUpdaterFailed(e));
108106
}
109107
}
110108

0 commit comments

Comments
 (0)