Skip to content

Commit

Permalink
cleanup ProgressLocationService
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTugarev committed Aug 8, 2019
1 parent f260643 commit f8cc4e4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
7 changes: 3 additions & 4 deletions packages/core/src/browser/frontend-application-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import { MimeService } from './mime-service';
import { ApplicationShellMouseTracker } from './shell/application-shell-mouse-tracker';
import { ViewContainer, ViewContainerIdentifier } from './view-container';
import { QuickViewService } from './quick-view-service';
import { ProgressLocationServiceImpl, ProgressLocationService } from './progress-location-service';
import { ProgressLocationService } from './progress-location-service';
import { ProgressClient } from '../common/progress-service-protocol';
import { ProgressService } from '../common/progress-service';

Expand Down Expand Up @@ -255,9 +255,8 @@ export const frontendApplicationModule = new ContainerModule((bind, unbind, isBo
bind(QuickViewService).toSelf().inSingletonScope();
bind(QuickOpenContribution).toService(QuickViewService);

bind(ProgressLocationServiceImpl).toSelf().inSingletonScope();
bind(ProgressLocationService).toService(ProgressLocationServiceImpl);
bind(ProgressClient).toService(ProgressLocationServiceImpl);
bind(ProgressLocationService).toSelf().inSingletonScope();
bind(ProgressClient).toService(ProgressLocationService);
bind(ProgressService).toSelf().inSingletonScope();
});

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/browser/progress-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { ProgressLocationService } from './progress-location-service';
import { ProgressLocationEvent } from './progress-location-service';
import { DisposableCollection, Disposable } from '../common';
import { Event } from '../common/event';

Expand All @@ -27,7 +27,7 @@ export class ProgressBar implements Disposable {

protected progressBar: HTMLDivElement;

constructor(protected options: ProgressBar.Options, onProgress: Event<ProgressLocationService.ProgressEvent>) {
constructor(protected options: ProgressBar.Options, onProgress: Event<ProgressLocationEvent>) {
this.progressBar = document.createElement('div');
this.progressBar.className = 'theia-progress-bar';
this.progressBar.style.display = 'none';
Expand All @@ -46,7 +46,7 @@ export class ProgressBar implements Disposable {
]);
}

protected onProgress(event: ProgressLocationService.ProgressEvent): void {
protected onProgress(event: ProgressLocationEvent): void {
if (this.toDispose.disposed) {
return;
}
Expand Down
40 changes: 18 additions & 22 deletions packages/core/src/browser/progress-location-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,27 @@ import { Deferred } from '../common/promise-util';
import { Event, Emitter } from '../common/event';
import throttle = require('lodash.throttle');

export const ProgressLocationService = Symbol('ProgressLocationService');
export interface ProgressLocationService {
onProgress(locationId: string): Event<ProgressLocationService.ProgressEvent>;
}
export namespace ProgressLocationService {
export interface ProgressEvent {
message?: string;
show: boolean;
}
export interface ProgressLocationEvent {
message?: string;
show: boolean;
}

@injectable()
export class ProgressLocationServiceImpl implements ProgressLocationService, ProgressClient {
export class ProgressLocationService implements ProgressClient {

protected emitters = new Map<string, Emitter<ProgressLocationService.ProgressEvent>>();
protected emitters = new Map<string, Emitter<ProgressLocationEvent>[]>();

onProgress(locationId: string): Event<ProgressLocationService.ProgressEvent> {
if (this.emitters.get(locationId)) {
throw new Error(`Progress listener for location "${locationId}" is already registered.`);
}
const emitter = new Emitter<ProgressLocationService.ProgressEvent>();
this.emitters.set(locationId, emitter);
onProgress(locationId: string): Event<ProgressLocationEvent> {
const emitter = this.addEmitter(locationId);
return emitter.event;
}
protected addEmitter(locationId: string): Emitter<ProgressLocationEvent> {
const emitter = new Emitter<ProgressLocationEvent>();
const list = this.emitters.get(locationId) || [];
list.push(emitter);
this.emitters.set(locationId, list);
return emitter;
}

protected readonly progressByLocation = new Map<string, Set<string>>();

Expand All @@ -71,12 +68,11 @@ export class ProgressLocationServiceImpl implements ProgressLocationService, Pro
this.fireEvent(locationId, show);
}
protected readonly fireEvent = throttle((locationId: string, show: boolean) => {
const emitter = this.emitters.get(locationId);
if (!emitter) {
console.warn(`Unknown location with id "${locationId}"`);
return;
let emitters = this.emitters.get(locationId);
if (!emitters) {
emitters = [ this.addEmitter(locationId) ];
}
emitter.fire({ show });
emitters.forEach(e => e.fire({ show }));
}, 250);

protected getLocationId(message: ProgressMessage): string {
Expand Down

0 comments on commit f8cc4e4

Please sign in to comment.