Skip to content

Commit

Permalink
Replacing a few instances of IDisposable[]
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed May 28, 2019
1 parent 9592467 commit 30d1114
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 72 deletions.
20 changes: 9 additions & 11 deletions src/vs/base/browser/ui/menu/menu.ts
Expand Up @@ -12,7 +12,7 @@ import { ResolvedKeybinding, KeyCode } from 'vs/base/common/keyCodes';
import { addClass, EventType, EventHelper, EventLike, removeTabIndexAndUpdateFocus, isAncestor, hasClass, addDisposableListener, removeClass, append, $, addClasses, removeClasses } from 'vs/base/browser/dom';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { RunOnceScheduler } from 'vs/base/common/async';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { Color } from 'vs/base/common/color';
import { DomScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { ScrollbarVisibility, ScrollEvent } from 'vs/base/common/scrollable';
Expand Down Expand Up @@ -71,15 +71,14 @@ interface ISubMenuData {

export class Menu extends ActionBar {
private mnemonics: Map<string, Array<BaseMenuActionViewItem>>;
private menuDisposables: IDisposable[];
private readonly menuDisposables: DisposableStore;
private scrollableElement: DomScrollableElement;
private menuElement: HTMLElement;
private scrollTopHold: number | undefined;

private readonly _onScroll: Emitter<void>;

constructor(container: HTMLElement, actions: IAction[], options: IMenuOptions = {}) {

addClass(container, 'monaco-menu-container');
container.setAttribute('role', 'presentation');
const menuElement = document.createElement('div');
Expand All @@ -103,7 +102,7 @@ export class Menu extends ActionBar {

this.actionsList.tabIndex = 0;

this.menuDisposables = [];
this.menuDisposables = this._register(new DisposableStore());

addDisposableListener(menuElement, EventType.KEY_DOWN, (e) => {
const event = new StandardKeyboardEvent(e);
Expand Down Expand Up @@ -217,9 +216,9 @@ export class Menu extends ActionBar {

menuElement.style.maxHeight = `${Math.max(10, window.innerHeight - container.getBoundingClientRect().top - 30)}px`;

this.scrollableElement.onScroll(() => {
this.menuDisposables.push(this.scrollableElement.onScroll(() => {
this._onScroll.fire();
}, this, this.menuDisposables);
}, this));

this._register(addDisposableListener(this.menuElement, EventType.SCROLL, (e: ScrollEvent) => {
if (this.scrollTopHold !== undefined) {
Expand Down Expand Up @@ -575,7 +574,7 @@ class SubmenuMenuActionViewItem extends BaseMenuActionViewItem {
private mysubmenu: Menu | null;
private submenuContainer: HTMLElement | undefined;
private submenuIndicator: HTMLElement;
private submenuDisposables: IDisposable[] = [];
private readonly submenuDisposables = this._register(new DisposableStore());
private mouseOver: boolean;
private showScheduler: RunOnceScheduler;
private hideScheduler: RunOnceScheduler;
Expand Down Expand Up @@ -675,7 +674,7 @@ class SubmenuMenuActionViewItem extends BaseMenuActionViewItem {
this.parentData.submenu = undefined;

if (this.submenuContainer) {
this.submenuDisposables = dispose(this.submenuDisposables);
this.submenuDisposables.clear();
this.submenuContainer = undefined;
}
}
Expand Down Expand Up @@ -720,7 +719,7 @@ class SubmenuMenuActionViewItem extends BaseMenuActionViewItem {
this.parentData.submenu = undefined;
}

this.submenuDisposables = dispose(this.submenuDisposables);
this.submenuDisposables.clear();
this.submenuContainer = undefined;
}
}));
Expand All @@ -741,7 +740,7 @@ class SubmenuMenuActionViewItem extends BaseMenuActionViewItem {
this.parentData.submenu = undefined;
}

this.submenuDisposables = dispose(this.submenuDisposables);
this.submenuDisposables.clear();
this.submenuContainer = undefined;
}));

Expand Down Expand Up @@ -781,7 +780,6 @@ class SubmenuMenuActionViewItem extends BaseMenuActionViewItem {
}

if (this.submenuContainer) {
this.submenuDisposables = dispose(this.submenuDisposables);
this.submenuContainer = undefined;
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/vs/base/browser/ui/sash/sash.ts
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import 'vs/css!./sash';
import { IDisposable, dispose, Disposable } from 'vs/base/common/lifecycle';
import { IDisposable, dispose, Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { isIPad } from 'vs/base/browser/browser';
import { isMacintosh } from 'vs/base/common/platform';
import * as types from 'vs/base/common/types';
Expand Down Expand Up @@ -95,14 +95,14 @@ export class Sash extends Disposable {

linkedSash: Sash | undefined = undefined;

private orthogonalStartSashDisposables: IDisposable[] = [];
private readonly orthogonalStartSashDisposables = this._register(new DisposableStore());
private _orthogonalStartSash: Sash | undefined;
get orthogonalStartSash(): Sash | undefined { return this._orthogonalStartSash; }
set orthogonalStartSash(sash: Sash | undefined) {
this.orthogonalStartSashDisposables = dispose(this.orthogonalStartSashDisposables);
this.orthogonalStartSashDisposables.clear();

if (sash) {
sash.onDidEnablementChange(this.onOrthogonalStartSashEnablementChange, this, this.orthogonalStartSashDisposables);
this.orthogonalStartSashDisposables.push(sash.onDidEnablementChange(this.onOrthogonalStartSashEnablementChange, this));
this.onOrthogonalStartSashEnablementChange(sash.state);
} else {
this.onOrthogonalStartSashEnablementChange(SashState.Disabled);
Expand All @@ -111,14 +111,14 @@ export class Sash extends Disposable {
this._orthogonalStartSash = sash;
}

private orthogonalEndSashDisposables: IDisposable[] = [];
private readonly orthogonalEndSashDisposables = this._register(new DisposableStore());
private _orthogonalEndSash: Sash | undefined;
get orthogonalEndSash(): Sash | undefined { return this._orthogonalEndSash; }
set orthogonalEndSash(sash: Sash | undefined) {
this.orthogonalEndSashDisposables = dispose(this.orthogonalEndSashDisposables);
this.orthogonalEndSashDisposables.clear();

if (sash) {
sash.onDidEnablementChange(this.onOrthogonalEndSashEnablementChange, this, this.orthogonalEndSashDisposables);
this.orthogonalEndSashDisposables.push(sash.onDidEnablementChange(this.onOrthogonalEndSashEnablementChange, this));
this.onOrthogonalEndSashEnablementChange(sash.state);
} else {
this.onOrthogonalEndSashEnablementChange(SashState.Disabled);
Expand Down Expand Up @@ -384,9 +384,6 @@ export class Sash extends Disposable {
dispose(): void {
super.dispose();

this.orthogonalStartSashDisposables = dispose(this.orthogonalStartSashDisposables);
this.orthogonalEndSashDisposables = dispose(this.orthogonalEndSashDisposables);

if (this.el && this.el.parentElement) {
this.el.parentElement.removeChild(this.el);
}
Expand Down
9 changes: 5 additions & 4 deletions src/vs/editor/contrib/codeAction/test/codeAction.test.ts
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { Range } from 'vs/editor/common/core/range';
import { TextModel } from 'vs/editor/common/model/textModel';
Expand All @@ -18,7 +18,7 @@ suite('CodeAction', () => {
let langId = new LanguageIdentifier('fooLang', 17);
let uri = URI.parse('untitled:path');
let model: TextModel;
let disposables: IDisposable[] = [];
const disposables = new DisposableStore();
let testData = {
diagnostics: {
abc: {
Expand Down Expand Up @@ -79,12 +79,13 @@ suite('CodeAction', () => {
};

setup(function () {
disposables.clear();
model = TextModel.createFromString('test1\ntest2\ntest3', undefined, langId, uri);
disposables = [model];
disposables.push(model);
});

teardown(function () {
dispose(disposables);
disposables.clear();
});

test('CodeActions are sorted by type, #38623', async function () {
Expand Down
8 changes: 4 additions & 4 deletions src/vs/editor/contrib/codeAction/test/codeActionModel.test.ts
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { Selection } from 'vs/editor/common/core/selection';
Expand All @@ -26,18 +26,18 @@ suite('CodeAction', () => {
let model: TextModel;
let markerService: MarkerService;
let editor: ICodeEditor;
let disposables: IDisposable[];
const disposables = new DisposableStore();

setup(() => {
disposables = [];
disposables.clear();
markerService = new MarkerService();
model = TextModel.createFromString('foobar foo bar\nfarboo far boo', undefined, languageIdentifier, uri);
editor = createTestCodeEditor({ model: model });
editor.setPosition({ lineNumber: 1, column: 1 });
});

teardown(() => {
dispose(disposables);
disposables.clear();
editor.dispose();
model.dispose();
markerService.dispose();
Expand Down
23 changes: 11 additions & 12 deletions src/vs/editor/contrib/dnd/dnd.ts
Expand Up @@ -5,7 +5,7 @@

import 'vs/css!./dnd';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { Disposable } from 'vs/base/common/lifecycle';
import { isMacintosh } from 'vs/base/common/platform';
import { KeyCode } from 'vs/base/common/keyCodes';
import { ICodeEditor, IEditorMouseEvent, IMouseTarget, MouseTargetType } from 'vs/editor/browser/editorBrowser';
Expand All @@ -28,12 +28,11 @@ function hasTriggerModifier(e: IKeyboardEvent | IMouseEvent): boolean {
}
}

export class DragAndDropController implements editorCommon.IEditorContribution {
export class DragAndDropController extends Disposable implements editorCommon.IEditorContribution {

private static readonly ID = 'editor.contrib.dragAndDrop';

private readonly _editor: ICodeEditor;
private _toUnhook: IDisposable[];
private _dragSelection: Selection | null;
private _dndDecorationIds: string[];
private _mouseDown: boolean;
Expand All @@ -45,15 +44,15 @@ export class DragAndDropController implements editorCommon.IEditorContribution {
}

constructor(editor: ICodeEditor) {
super();
this._editor = editor;
this._toUnhook = [];
this._toUnhook.push(this._editor.onMouseDown((e: IEditorMouseEvent) => this._onEditorMouseDown(e)));
this._toUnhook.push(this._editor.onMouseUp((e: IEditorMouseEvent) => this._onEditorMouseUp(e)));
this._toUnhook.push(this._editor.onMouseDrag((e: IEditorMouseEvent) => this._onEditorMouseDrag(e)));
this._toUnhook.push(this._editor.onMouseDrop((e: IEditorMouseEvent) => this._onEditorMouseDrop(e)));
this._toUnhook.push(this._editor.onKeyDown((e: IKeyboardEvent) => this.onEditorKeyDown(e)));
this._toUnhook.push(this._editor.onKeyUp((e: IKeyboardEvent) => this.onEditorKeyUp(e)));
this._toUnhook.push(this._editor.onDidBlurEditorWidget(() => this.onEditorBlur()));
this._register(this._editor.onMouseDown((e: IEditorMouseEvent) => this._onEditorMouseDown(e)));
this._register(this._editor.onMouseUp((e: IEditorMouseEvent) => this._onEditorMouseUp(e)));
this._register(this._editor.onMouseDrag((e: IEditorMouseEvent) => this._onEditorMouseDrag(e)));
this._register(this._editor.onMouseDrop((e: IEditorMouseEvent) => this._onEditorMouseDrop(e)));
this._register(this._editor.onKeyDown((e: IKeyboardEvent) => this.onEditorKeyDown(e)));
this._register(this._editor.onKeyUp((e: IKeyboardEvent) => this.onEditorKeyUp(e)));
this._register(this._editor.onDidBlurEditorWidget(() => this.onEditorBlur()));
this._dndDecorationIds = [];
this._mouseDown = false;
this._modifierPressed = false;
Expand Down Expand Up @@ -228,7 +227,7 @@ export class DragAndDropController implements editorCommon.IEditorContribution {
this._dragSelection = null;
this._mouseDown = false;
this._modifierPressed = false;
this._toUnhook = dispose(this._toUnhook);
super.dispose();
}
}

Expand Down
23 changes: 8 additions & 15 deletions src/vs/workbench/contrib/extensions/common/extensionsUtils.ts
Expand Up @@ -7,7 +7,7 @@ import * as arrays from 'vs/base/common/arrays';
import { localize } from 'vs/nls';
import { Event } from 'vs/base/common/event';
import { onUnexpectedError } from 'vs/base/common/errors';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { Disposable } from 'vs/base/common/lifecycle';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IExtensionManagementService, ILocalExtension, IExtensionEnablementService, IExtensionTipsService, IExtensionIdentifier, EnablementState, InstallOperation } from 'vs/platform/extensionManagement/common/extensionManagement';
import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
Expand All @@ -22,9 +22,7 @@ export interface IExtensionStatus {
globallyEnabled: boolean;
}

export class KeymapExtensions implements IWorkbenchContribution {

private disposables: IDisposable[] = [];
export class KeymapExtensions extends Disposable implements IWorkbenchContribution {

constructor(
@IInstantiationService private readonly instantiationService: IInstantiationService,
Expand All @@ -34,13 +32,12 @@ export class KeymapExtensions implements IWorkbenchContribution {
@INotificationService private readonly notificationService: INotificationService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
) {
this.disposables.push(
lifecycleService.onShutdown(() => this.dispose()),
instantiationService.invokeFunction(onExtensionChanged)((identifiers => {
Promise.all(identifiers.map(identifier => this.checkForOtherKeymaps(identifier)))
.then(undefined, onUnexpectedError);
}))
);
super();
this._register(lifecycleService.onShutdown(() => this.dispose()));
this._register(instantiationService.invokeFunction(onExtensionChanged)((identifiers => {
Promise.all(identifiers.map(identifier => this.checkForOtherKeymaps(identifier)))
.then(undefined, onUnexpectedError);
})));
}

private checkForOtherKeymaps(extensionIdentifier: IExtensionIdentifier): Promise<void> {
Expand Down Expand Up @@ -87,10 +84,6 @@ export class KeymapExtensions implements IWorkbenchContribution {
}]
);
}

dispose(): void {
this.disposables = dispose(this.disposables);
}
}

export function onExtensionChanged(accessor: ServicesAccessor): Event<IExtensionIdentifier[]> {
Expand Down
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { Action } from 'vs/base/common/actions';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { dispose, IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { getIconClasses } from 'vs/editor/common/services/getIconClasses';
import { IModelService } from 'vs/editor/common/services/modelService';
Expand Down Expand Up @@ -164,7 +164,7 @@ export class OpenWorkspaceSettingsAction extends Action {
static readonly ID = 'workbench.action.openWorkspaceSettings';
static readonly LABEL = nls.localize('openWorkspaceSettings', "Open Workspace Settings");

private disposables: IDisposable[] = [];
private readonly disposables = new DisposableStore();

constructor(
id: string,
Expand All @@ -174,7 +174,7 @@ export class OpenWorkspaceSettingsAction extends Action {
) {
super(id, label);
this.update();
this.workspaceContextService.onDidChangeWorkbenchState(() => this.update(), this, this.disposables);
this.disposables.push(this.workspaceContextService.onDidChangeWorkbenchState(() => this.update(), this));
}

private update(): void {
Expand All @@ -186,7 +186,7 @@ export class OpenWorkspaceSettingsAction extends Action {
}

dispose(): void {
this.disposables = dispose(this.disposables);
this.disposables.dispose();
super.dispose();
}
}
Expand Down
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { dispose, IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { isLinux } from 'vs/base/common/platform';
import { isEqual } from 'vs/base/common/resources';
import { endsWith } from 'vs/base/common/strings';
Expand Down Expand Up @@ -129,14 +129,14 @@ export class PreferencesContribution implements IWorkbenchContribution {
const modelContent = JSON.stringify(schema);
const languageSelection = this.modeService.create('jsonc');
const model = this.modelService.createModel(modelContent, languageSelection, uri);
const disposables: IDisposable[] = [];
const disposables = new DisposableStore();
disposables.push(schemaRegistry.onDidChangeSchema(schemaUri => {
if (schemaUri === uri.toString()) {
schema = schemaRegistry.getSchemaContributions().schemas[uri.toString()];
model.setValue(JSON.stringify(schema));
}
}));
disposables.push(model.onWillDispose(() => dispose(disposables)));
disposables.push(model.onWillDispose(() => disposables.dispose()));

return model;
}
Expand Down

0 comments on commit 30d1114

Please sign in to comment.