Skip to content

Commit

Permalink
fixup! Introduce window.withProgress API endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Vinokur <ivinokur@redhat.com>
  • Loading branch information
vinokurig committed Nov 28, 2018
1 parent 12f7e86 commit 3e22837
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
15 changes: 10 additions & 5 deletions packages/core/src/common/message-service-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ export interface Progress {
}

export interface ProgressUpdate {
readonly value?: string;
readonly increment?: number;
readonly message?: string;
readonly progressRate?: ProgressRate;
}

export interface ProgressRate {
done: number;
total: number;
}

@injectable()
Expand Down Expand Up @@ -103,7 +108,7 @@ export class MessageClient {
*
* To be implemented by an extension, e.g. by the messages extension.
*/
reportProgress(progressId: string, update: ProgressUpdate): Promise<void> {
reportProgress(progressId: string, update: ProgressUpdate, message: ProgressMessage, cancellationToken: CancellationToken): Promise<void> {
return Promise.resolve(undefined);
}
}
Expand All @@ -125,9 +130,9 @@ export class DispatchingMessageClient extends MessageClient {
));
}

reportProgress(progressId: string, update: ProgressUpdate): Promise<void> {
reportProgress(progressId: string, update: ProgressUpdate, message: ProgressMessage, cancellationToken: CancellationToken): Promise<void> {
return Promise.race([...this.clients].map(client =>
client.reportProgress(progressId, update)
client.reportProgress(progressId, update, message, cancellationToken)
));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/message-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class MessageService {
const id = this.newProgressId();
const cancellationSource = new CancellationTokenSource();
const report = (update: ProgressUpdate) => {
this.client.reportProgress(id, update);
this.client.reportProgress(id, update, message, cancellationSource.token);
};
let clientMessage = message;
if (ProgressMessage.isCancelable(message)) {
Expand Down
11 changes: 8 additions & 3 deletions packages/messages/src/browser/notifications-message-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class NotificationsMessageClient extends MessageClient {
return this.show(message);
}

showProgress(progressId: string, message: ProgressMessage, cancellationToken: CancellationToken): Promise<string | undefined> {
showProgress(progressId: string, message: ProgressMessage, cancellationToken: CancellationToken, update?: ProgressUpdate): Promise<string | undefined> {
const messageArguments = { ...message, type: MessageType.Progress, options: { ...(message.options || {}), timeout: 0 } };
if (this.visibleProgressNotifications.has(progressId)) {
throw new Error('Cannot show new progress with already existing id.');
Expand All @@ -48,6 +48,9 @@ export class NotificationsMessageClient extends MessageClient {
}));
this.visibleProgressNotifications.set(progressId, progressNotification);
progressNotification.show();
if (update) {
progressNotification.update(update);
}
const cancel = () => {
if (message.options && message.options.cancelable) {
resolve(ProgressMessage.Cancel);
Expand All @@ -62,10 +65,12 @@ export class NotificationsMessageClient extends MessageClient {
});
}

async reportProgress(progressId: string, update: ProgressUpdate): Promise<void> {
async reportProgress(progressId: string, update: ProgressUpdate, message: ProgressMessage, cancellationToken: CancellationToken): Promise<void> {
const notification = this.visibleProgressNotifications.get(progressId);
if (notification) {
notification.update({ message: update.value, increment: update.increment });
notification.update({ message: update.message, progressRate: update.progressRate });
} else {
this.showProgress(progressId, message, cancellationToken, { message: update.message, progressRate: update.progressRate });
}
}

Expand Down
14 changes: 6 additions & 8 deletions packages/messages/src/browser/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { ProgressRate } from '@theia/core/src/common';

export const NOTIFICATIONS_CONTAINER = 'theia-NotificationsContainer';
export const NOTIFICATION = 'theia-Notification';
Expand Down Expand Up @@ -42,7 +43,7 @@ export interface Notification {
export interface ProgressNotification {
show(): void;
close(): void;
update(item: { message?: string, increment?: number }): void;
update(item: { message?: string, progressRate?: ProgressRate }): void;
}

export class Notifications {
Expand Down Expand Up @@ -134,7 +135,6 @@ export class Notifications {
}

class ProgressNotificationImpl implements ProgressNotification {
private increment: number = 0;
private readonly node: Node;
private readonly container: Element;
private readonly properties: NotificationProperties;
Expand Down Expand Up @@ -167,16 +167,14 @@ class ProgressNotificationImpl implements ProgressNotification {
}
}

update(item: { message?: string, increment?: number }): void {
update(item: { message?: string, progressRate?: ProgressRate }): void {
const textElement = document.getElementById('notification-text-' + this.properties.id);
if (textElement) {
if (item.increment) {
this.increment = this.increment + item.increment;
this.increment = this.increment > 100 ? 100 : this.increment;

if (item.progressRate) {
const progressElement = document.getElementById('notification-progress-' + this.properties.id);
if (progressElement) {
progressElement.innerText = this.increment + '%';
const progressRate = item.progressRate;
progressElement.innerText = `${Math.floor(progressRate.done / progressRate.total * 100)}%`;
}
}
textElement.innerText = this.properties.text + (item.message ? ': ' + item.message : '');
Expand Down
23 changes: 18 additions & 5 deletions packages/plugin-ext/src/main/browser/notification-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export class NotificationMainImpl implements NotificationMain {

private readonly proxy: NotificationExt;
private readonly messageService: MessageService;
private readonly progress = new Map<string, Progress>();
private readonly progressMap = new Map<string, Progress>();
private readonly incrementMap = new Map<string, number>();

constructor(rpc: RPCProtocol, container: interfaces.Container) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.NOTIFICATION_EXT);
Expand All @@ -36,21 +37,33 @@ export class NotificationMainImpl implements NotificationMain {
const onDidClose = async () => this.proxy.$onCancel(await deferredId.promise);
const progress = await this.messageService.showProgress({ text: message, options: { cancelable: true } }, onDidClose);
deferredId.resolve(progress.id);
this.progress.set(progress.id, progress);
this.progressMap.set(progress.id, progress);
this.incrementMap.set(progress.id, 0);
return progress.id;
}

$stopProgress(id: string): void {
const progress = this.progress.get(id);
const progress = this.progressMap.get(id);
if (progress) {
progress.cancel();
this.progressMap.delete(id);
this.incrementMap.delete(id);
}
}

$updateProgress(id: string, item: { message?: string, increment?: number }): void {
const progress = this.progress.get(id);
const progress = this.progressMap.get(id);
let done: number | undefined;
if (item.increment) {
const increment = this.incrementMap.get(id);
if (increment !== undefined) {
done = increment + item.increment;
done = done > 100 ? 100 : done;
this.incrementMap.set(id, done);
}
}
if (progress) {
progress.report({ value: item.message, increment: item.increment });
progress.report({ message: item.message, progressRate: done ? { done, total: 100 } : undefined });
}
}
}

0 comments on commit 3e22837

Please sign in to comment.