Skip to content

Commit 4f35ddd

Browse files
author
Benjamin Pasero
committed
get rid of EventEmitter for editor inputs, models and workbench components
1 parent ae95c24 commit 4f35ddd

17 files changed

Lines changed: 98 additions & 74 deletions

File tree

src/vs/platform/editor/common/editor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import URI from 'vs/base/common/uri';
88
import {TPromise} from 'vs/base/common/winjs.base';
9-
import {IEventEmitter} from 'vs/base/common/eventEmitter';
109
import {createDecorator} from 'vs/platform/instantiation/common/instantiation';
10+
import Event from 'vs/base/common/event';
1111

1212
export const IEditorService = createDecorator<IEditorService>('editorService');
1313

@@ -26,12 +26,17 @@ export interface IEditorService {
2626
resolveEditorModel(input: IResourceInput, refresh?: boolean): TPromise<ITextEditorModel>;
2727
}
2828

29-
export interface IEditorModel extends IEventEmitter {
29+
export interface IEditorModel {
3030

3131
/**
3232
* Loads the model.
3333
*/
3434
load(): TPromise<IEditorModel>;
35+
36+
/**
37+
* Dispose associated resources
38+
*/
39+
dispose(): void;
3540
}
3641

3742
export interface ITextEditorModel extends IEditorModel {
@@ -125,7 +130,9 @@ export enum Direction {
125130
RIGHT
126131
}
127132

128-
export interface IEditorInput extends IEventEmitter {
133+
export interface IEditorInput {
134+
135+
onDispose: Event<void>;
129136

130137
/**
131138
* Returns the display name of this input.

src/vs/workbench/browser/composite.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
1212
import {AsyncDescriptor} from 'vs/platform/instantiation/common/descriptors';
1313
import {IComposite} from 'vs/workbench/common/composite';
1414
import {IEditorControl} from 'vs/platform/editor/common/editor';
15-
16-
/**
17-
* Internal composite events to communicate with composite container.
18-
*/
19-
export const EventType = {
20-
INTERNAL_COMPOSITE_TITLE_AREA_UPDATE: 'internalCompositeTitleAreaUpdate'
21-
};
15+
import Event, {Emitter} from 'vs/base/common/event';
2216

2317
/**
2418
* Composites are layed out in the sidebar and panel part of the workbench. At a time only one composite
@@ -34,6 +28,7 @@ export abstract class Composite extends WorkbenchComponent implements IComposite
3428
private _telemetryData: any = {};
3529
private visible: boolean;
3630
private parent: Builder;
31+
private _onTitleAreaUpdate: Emitter<void>;
3732

3833
protected actionRunner: IActionRunner;
3934

@@ -44,6 +39,7 @@ export abstract class Composite extends WorkbenchComponent implements IComposite
4439
super(id);
4540

4641
this.visible = false;
42+
this._onTitleAreaUpdate = new Emitter<void>();
4743
}
4844

4945
public getTitle(): string {
@@ -58,6 +54,10 @@ export abstract class Composite extends WorkbenchComponent implements IComposite
5854
return this._telemetryData;
5955
}
6056

57+
public get onTitleAreaUpdate(): Event<void> {
58+
return this._onTitleAreaUpdate.event;
59+
}
60+
6161
/**
6262
* Note: Clients should not call this method, the workbench calls this
6363
* method. Calling it otherwise may result in unexpected behavior.
@@ -179,7 +179,7 @@ export abstract class Composite extends WorkbenchComponent implements IComposite
179179
* gets visible.
180180
*/
181181
protected updateTitleArea(): void {
182-
this.emit(EventType.INTERNAL_COMPOSITE_TITLE_AREA_UPDATE, this.getId());
182+
this._onTitleAreaUpdate.fire();
183183
}
184184

185185
/**
@@ -195,6 +195,12 @@ export abstract class Composite extends WorkbenchComponent implements IComposite
195195
public getControl(): IEditorControl {
196196
return null;
197197
}
198+
199+
public dispose(): void {
200+
this._onTitleAreaUpdate.dispose();
201+
202+
super.dispose();
203+
}
198204
}
199205

200206
/**

src/vs/workbench/browser/parts/compositePart.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {Action, IAction} from 'vs/base/common/actions';
2424
import {Part} from 'vs/workbench/browser/part';
2525
import {Composite, CompositeRegistry} from 'vs/workbench/browser/composite';
2626
import {IComposite} from 'vs/workbench/common/composite';
27-
import {EventType as CompositeEventType} from 'vs/workbench/browser/composite';
2827
import {WorkbenchProgressService} from 'vs/workbench/services/progress/browser/progressService';
2928
import {IPartService} from 'vs/workbench/services/part/common/partService';
3029
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
@@ -182,7 +181,7 @@ export abstract class CompositePart<T extends Composite> extends Part {
182181
this.instantiatedComposits.push(composite);
183182

184183
// Register to title area update events from the composite
185-
this.instantiatedCompositeListeners.push(composite.addListener2(CompositeEventType.INTERNAL_COMPOSITE_TITLE_AREA_UPDATE, compositeId => this.onTitleAreaUpdate(compositeId)));
184+
this.instantiatedCompositeListeners.push(composite.onTitleAreaUpdate(() => this.onTitleAreaUpdate(composite.getId())));
186185

187186
// Remove from Promises Cache since Loaded
188187
delete this.compositeLoaderPromises[id];

src/vs/workbench/common/component.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import {EventEmitter, IEventEmitter} from 'vs/base/common/eventEmitter';
87
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
98
import {Scope, Memento} from 'vs/workbench/common/memento';
109
import {IStorageService} from 'vs/platform/storage/common/storage';
@@ -14,7 +13,7 @@ import {IStorageService} from 'vs/platform/storage/common/storage';
1413
* Provides some convinience methods to participate in the workbench lifecycle (dispose, shutdown) and
1514
* loading and saving settings through memento.
1615
*/
17-
export interface IWorkbenchComponent extends IDisposable, IEventEmitter {
16+
export interface IWorkbenchComponent extends IDisposable {
1817

1918
/**
2019
* The unique identifier of this component.
@@ -55,14 +54,12 @@ export interface IWorkbenchComponent extends IDisposable, IEventEmitter {
5554
dispose(): void;
5655
}
5756

58-
export class WorkbenchComponent extends EventEmitter implements IWorkbenchComponent {
57+
export class WorkbenchComponent implements IWorkbenchComponent {
5958
private _toUnbind: IDisposable[];
6059
private id: string;
6160
private componentMemento: Memento;
6261

6362
constructor(id: string) {
64-
super();
65-
6663
this._toUnbind = [];
6764
this.id = id;
6865
this.componentMemento = new Memento(this.id);
@@ -92,7 +89,5 @@ export class WorkbenchComponent extends EventEmitter implements IWorkbenchCompon
9289

9390
public dispose(): void {
9491
this._toUnbind = dispose(this._toUnbind);
95-
96-
super.dispose();
9792
}
9893
}

src/vs/workbench/common/editor.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
'use strict';
66

77
import {TPromise} from 'vs/base/common/winjs.base';
8-
import {EventEmitter} from 'vs/base/common/eventEmitter';
98
import Event, {Emitter} from 'vs/base/common/event';
109
import types = require('vs/base/common/types');
1110
import URI from 'vs/base/common/uri';
@@ -120,16 +119,16 @@ export interface IEditorInputFactory {
120119
* Editor inputs are lightweight objects that can be passed to the workbench API to open inside the editor part.
121120
* Each editor input is mapped to an editor that is capable of opening it through the Platform facade.
122121
*/
123-
export abstract class EditorInput extends EventEmitter implements IEditorInput {
124-
122+
export abstract class EditorInput implements IEditorInput {
123+
private _onDispose: Emitter<void>;
125124
protected _onDidChangeDirty: Emitter<void>;
126-
125+
127126
private disposed: boolean;
128127

129128
constructor() {
130-
super();
131-
132129
this._onDidChangeDirty = new Emitter<void>();
130+
this._onDispose = new Emitter<void>();
131+
133132
this.disposed = false;
134133
}
135134

@@ -140,6 +139,13 @@ export abstract class EditorInput extends EventEmitter implements IEditorInput {
140139
return this._onDidChangeDirty.event;
141140
}
142141

142+
/**
143+
* Fired when the model gets disposed.
144+
*/
145+
public get onDispose(): Event<void> {
146+
return this._onDispose.event;
147+
}
148+
143149
/**
144150
* Returns the name of this input that can be shown to the user. Examples include showing the name of the input
145151
* above the editor area when the input is shown.
@@ -237,11 +243,11 @@ export abstract class EditorInput extends EventEmitter implements IEditorInput {
237243
* resolving the editor input.
238244
*/
239245
public dispose(): void {
240-
this._onDidChangeDirty.dispose();
241246
this.disposed = true;
242-
this.emit('dispose');
247+
this._onDispose.fire();
243248

244-
super.dispose();
249+
this._onDidChangeDirty.dispose();
250+
this._onDispose.dispose();
245251
}
246252

247253
/**
@@ -399,7 +405,19 @@ export interface ITextEditorModel extends IEditorModel {
399405
* connects to the disk to retrieve content and may allow for saving it back or reverting it. Editor models
400406
* are typically cached for some while because they are expensive to construct.
401407
*/
402-
export class EditorModel extends EventEmitter implements IEditorModel {
408+
export class EditorModel implements IEditorModel {
409+
private _onDispose: Emitter<void>;
410+
411+
constructor() {
412+
this._onDispose = new Emitter<void>();
413+
}
414+
415+
/**
416+
* Fired when the model gets disposed.
417+
*/
418+
public get onDispose(): Event<void> {
419+
return this._onDispose.event;
420+
}
403421

404422
/**
405423
* Causes this model to load returning a promise when loading is completed.
@@ -419,9 +437,8 @@ export class EditorModel extends EventEmitter implements IEditorModel {
419437
* Subclasses should implement to free resources that have been claimed through loading.
420438
*/
421439
public dispose(): void {
422-
this.emit('dispose');
423-
424-
super.dispose();
440+
this._onDispose.fire();
441+
this._onDispose.dispose();
425442
}
426443
}
427444

src/vs/workbench/common/editor/diffEditorInput.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import nls = require('vs/nls');
88
import {TPromise} from 'vs/base/common/winjs.base';
99
import types = require('vs/base/common/types');
10+
import {once} from 'vs/base/common/event';
1011
import URI from 'vs/base/common/uri';
1112
import {getPathLabel, IWorkspaceProvider} from 'vs/base/common/labels';
1213
import {isBinaryMime} from 'vs/base/common/mime';
13-
import {EventType} from 'vs/base/common/events';
1414
import {EditorModel, IFileEditorInput, EditorInput, BaseDiffEditorInput} from 'vs/workbench/common/editor';
1515
import {BaseTextEditorModel} from 'vs/workbench/common/editor/textEditorModel';
1616
import {DiffEditorModel} from 'vs/workbench/common/editor/diffEditorModel';
@@ -46,13 +46,15 @@ export class DiffEditorInput extends BaseDiffEditorInput {
4646
private registerListeners(): void {
4747

4848
// When the original or modified input gets disposed, dispose this diff editor input
49-
this._toUnbind.push(this.originalInput.addListener2(EventType.DISPOSE, () => {
49+
const onceOriginalDisposed = once(this.originalInput.onDispose);
50+
this._toUnbind.push(onceOriginalDisposed(() => {
5051
if (!this.isDisposed()) {
5152
this.dispose();
5253
}
5354
}));
5455

55-
this._toUnbind.push(this.modifiedInput.addListener2(EventType.DISPOSE, () => {
56+
const onceModifiedDisposed = once(this.modifiedInput.onDispose);
57+
this._toUnbind.push(onceModifiedDisposed(() => {
5658
if (!this.isDisposed()) {
5759
this.dispose();
5860
}

src/vs/workbench/common/editor/editorStacksModel.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77

8-
import Event, {Emitter} from 'vs/base/common/event';
8+
import Event, {Emitter, once} from 'vs/base/common/event';
99
import {IEditorRegistry, Extensions, EditorInput, getUntitledOrFileResource, IEditorStacksModel, IEditorGroup, IEditorIdentifier, IGroupEvent, GroupIdentifier, IStacksModelChangeEvent, IWorkbenchEditorConfiguration, EditorOpenPositioning} from 'vs/workbench/common/editor';
1010
import URI from 'vs/base/common/uri';
1111
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
@@ -296,7 +296,8 @@ export class EditorGroup implements IEditorGroup {
296296
const unbind: IDisposable[] = [];
297297

298298
// Re-emit disposal of editor input as our own event
299-
unbind.push(editor.addOneTimeDisposableListener('dispose', () => {
299+
const onceDispose = once(editor.onDispose);
300+
unbind.push(onceDispose(() => {
300301
if (this.indexOf(editor) >= 0) {
301302
this._onEditorDisposed.fire(editor);
302303
}

src/vs/workbench/common/editor/resourceEditorInput.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {EditorModel, EditorInput} from 'vs/workbench/common/editor';
1010
import {ResourceEditorModel} from 'vs/workbench/common/editor/resourceEditorModel';
1111
import {IModel} from 'vs/editor/common/editorCommon';
1212
import URI from 'vs/base/common/uri';
13-
import {EventType} from 'vs/base/common/events';
1413
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
1514
import {IModelService} from 'vs/editor/common/services/modelService';
1615
import {IDisposable} from 'vs/base/common/lifecycle';
@@ -42,7 +41,7 @@ export class ResourceEditorInput extends EditorInput {
4241
} else {
4342
array.unshift(provider);
4443
}
45-
44+
4645
return {
4746
dispose() {
4847
let array = ResourceEditorInput.registry[scheme];
@@ -158,7 +157,7 @@ export class ResourceEditorInput extends EditorInput {
158157
// Otherwise Create Model and handle dispose event
159158
return ResourceEditorInput.getOrCreateModel(this.modelService, this.resource).then(() => {
160159
let model = this.instantiationService.createInstance(ResourceEditorModel, this.resource);
161-
const unbind = model.addListener2(EventType.DISPOSE, () => {
160+
const unbind = model.onDispose(() => {
162161
this.cachedModel = null; // make sure we do not dispose model again
163162
unbind.dispose();
164163
this.dispose();

src/vs/workbench/electron-browser/workbench.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {IOptions} from 'vs/workbench/common/options';
2323
import {IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions} from 'vs/workbench/common/contributions';
2424
import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor';
2525
import {IEditorRegistry, Extensions as EditorExtensions, TextEditorOptions, EditorInput, EditorOptions} from 'vs/workbench/common/editor';
26-
import {Part} from 'vs/workbench/browser/part';
2726
import {HistoryService} from 'vs/workbench/services/history/browser/history';
2827
import {ActivitybarPart} from 'vs/workbench/browser/parts/activitybar/activitybarPart';
2928
import {EditorPart} from 'vs/workbench/browser/parts/editor/editorPart';
@@ -211,9 +210,6 @@ export class Workbench implements IPartService {
211210
// Workbench Layout
212211
this.createWorkbenchLayout();
213212

214-
// Register Emitters
215-
this.registerEmitters();
216-
217213
// Load composits and editors in parallel
218214
const compositeAndEditorPromises: TPromise<any>[] = [];
219215

@@ -649,19 +645,6 @@ export class Workbench implements IPartService {
649645
this.toShutdown.forEach(s => s.shutdown());
650646
}
651647

652-
private registerEmitters(): void {
653-
654-
// Part Emitters
655-
this.hookPartListeners(this.activitybarPart);
656-
this.hookPartListeners(this.editorPart);
657-
this.hookPartListeners(this.sidebarPart);
658-
this.hookPartListeners(this.panelPart);
659-
}
660-
661-
private hookPartListeners(part: Part): void {
662-
this.toDispose.push(this.eventService.addEmitter2(part, part.getId()));
663-
}
664-
665648
private registerListeners(): void {
666649

667650
// Listen to editor changes

src/vs/workbench/parts/files/common/editors/textFileEditorModelManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class TextFileEditorModelManager implements ITextFileEditorModelManager {
192192

193193
// store in cache but remove when model gets disposed
194194
this.mapResourcePathToModel[resource.toString()] = model;
195-
this.mapResourceToDisposeListener[resource.toString()] = model.addListener2('dispose', () => this.remove(resource));
195+
this.mapResourceToDisposeListener[resource.toString()] = model.onDispose(() => this.remove(resource));
196196
}
197197

198198
public remove(resource: URI): void {

0 commit comments

Comments
 (0)