Small strict null checking pass on a few files (#4293)

* fix some null checking

* fix various null strict checks

* move location fo sql files in json

* fix compile and more unused properties

* formatting

* small formatting changes

* readd types

* add comments for angular components

* formatting

* remove any decl
This commit is contained in:
Anthony Dresser
2019-03-14 18:18:32 -07:00
committed by GitHub
parent 0bf0e795ca
commit 4014c1d0ab
29 changed files with 881 additions and 1171 deletions

View File

@@ -30,7 +30,7 @@ import { IDisposable } from 'vs/base/common/lifecycle';
`
})
export class BreadcrumbComponent implements OnInit, OnDestroy {
private menuItems: MenuItem[] = [];
protected menuItems: MenuItem[] = []; // used by angular template
private disposables: Array<IDisposable> = new Array();
constructor(

View File

@@ -13,11 +13,10 @@ export interface IButtonStyles extends vsIButtonStyles {
}
export class Button extends vsButton {
private buttonFocusOutline: Color;
private buttonFocusOutline?: Color;
constructor(container: HTMLElement, options?: IButtonOptions) {
super(container, options);
this.buttonFocusOutline = null;
this._register(DOM.addDisposableListener(this.element, DOM.EventType.FOCUS, () => {
this.element.style.outlineColor = this.buttonFocusOutline ? this.buttonFocusOutline.toString() : null;

View File

@@ -23,7 +23,7 @@ export interface ICheckboxStyles {
export class Checkbox extends Widget {
private _el: HTMLInputElement;
private _label: HTMLSpanElement;
private disabledCheckboxForeground: Color;
private disabledCheckboxForeground?: Color;
private _onChange = new Emitter<boolean>();
public readonly onChange: Event<boolean> = this._onChange.event;

View File

@@ -7,7 +7,6 @@
import 'vs/css!./media/dropdownList';
import * as DOM from 'vs/base/browser/dom';
import { Dropdown, IDropdownOptions } from 'vs/base/browser/ui/dropdown/dropdown';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { IDisposable } from 'vs/base/common/lifecycle';
import { Color } from 'vs/base/common/color';
import { IAction } from 'vs/base/common/actions';
@@ -15,10 +14,8 @@ import { EventType as GestureEventType } from 'vs/base/browser/touch';
import { List } from 'vs/base/browser/ui/list/listWidget';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
import { Builder } from 'sql/base/browser/builder';
import { Button } from 'sql/base/browser/ui/button/button';
import { attachButtonStyler } from 'sql/platform/theme/common/styler';
import { Button, IButtonStyles } from 'sql/base/browser/ui/button/button';
export interface IDropdownStyles {
backgroundColor?: Color;
@@ -28,51 +25,49 @@ export interface IDropdownStyles {
export class DropdownList extends Dropdown {
protected backgroundColor: Color;
protected foregroundColor: Color;
protected borderColor: Color;
protected backgroundColor?: Color;
protected foregroundColor?: Color;
protected borderColor?: Color;
private button?: Button;
constructor(
container: HTMLElement,
private _options: IDropdownOptions,
private _contentContainer: HTMLElement,
private _list: List<any>,
private _themeService: IThemeService,
private _action?: IAction,
action?: IAction,
) {
super(container, _options);
if (_action) {
let button = new Button(_contentContainer);
button.label = _action.label;
this.toDispose.push(DOM.addDisposableListener(button.element, DOM.EventType.CLICK, () => {
this._action.run();
if (action) {
this.button = new Button(_contentContainer);
this.button.label = action.label;
this.toDispose.push(DOM.addDisposableListener(this.button.element, DOM.EventType.CLICK, () => {
action.run();
this.hide();
}));
this.toDispose.push(DOM.addDisposableListener(button.element, DOM.EventType.KEY_DOWN, (e: KeyboardEvent) => {
this.toDispose.push(DOM.addDisposableListener(this.button.element, DOM.EventType.KEY_DOWN, (e: KeyboardEvent) => {
let event = new StandardKeyboardEvent(e);
if (event.equals(KeyCode.Enter)) {
e.stopPropagation();
this._action.run();
action.run();
this.hide();
}
}));
attachButtonStyler(button, this._themeService);
}
DOM.append(this.element, DOM.$('div.dropdown-icon'));
this.toDispose.push(new Builder(this.element).on([DOM.EventType.CLICK, DOM.EventType.MOUSE_DOWN, GestureEventType.Tap], (e: Event) => {
DOM.EventHelper.stop(e, true); // prevent default click behaviour to trigger
}).on([DOM.EventType.MOUSE_DOWN, GestureEventType.Tap], (e: Event) => {
// We want to show the context menu on dropdown so that as a user you can press and hold the
// mouse button, make a choice of action in the menu and release the mouse to trigger that
// action.
// Due to some weird bugs though, we delay showing the menu to unwind event stack
// (see https://github.com/Microsoft/vscode/issues/27648)
setTimeout(() => this.show(), 100);
}).on([DOM.EventType.KEY_DOWN], (e: KeyboardEvent) => {
let event = new StandardKeyboardEvent(e);
if (event.equals(KeyCode.Enter)) {
[DOM.EventType.CLICK, DOM.EventType.MOUSE_DOWN, GestureEventType.Tap].forEach(event => {
this._register(DOM.addDisposableListener(this.element, event, e => DOM.EventHelper.stop(e, true))); // prevent default click behaviour to trigger
});
[DOM.EventType.MOUSE_DOWN, GestureEventType.Tap].forEach(event => {
this._register(DOM.addDisposableListener(this.element, event, e => setTimeout(() => this.show(), 100)));
});
this._register(DOM.addStandardDisposableListener(this.element, DOM.EventType.KEY_DOWN, (e: StandardKeyboardEvent) => {
if (e.equals(KeyCode.Enter)) {
e.stopPropagation();
setTimeout(() => {
this.show();
@@ -96,7 +91,7 @@ export class DropdownList extends Dropdown {
protected renderContents(container: HTMLElement): IDisposable {
let div = DOM.append(container, this._contentContainer);
div.style.width = DOM.getTotalWidth(this.element) + 'px';
return null;
return { dispose: () => { } };
}
/**
@@ -126,11 +121,14 @@ export class DropdownList extends Dropdown {
}
}
public style(styles: IDropdownStyles): void {
public style(styles: IDropdownStyles & IButtonStyles): void {
this.backgroundColor = styles.backgroundColor;
this.foregroundColor = styles.foregroundColor;
this.borderColor = styles.borderColor;
this.applyStyles();
if (this.button) {
this.button.style(styles);
}
}
protected applyStyles(): void {
@@ -143,7 +141,7 @@ export class DropdownList extends Dropdown {
}
}
private applyStylesOnElement(element: HTMLElement, background: string, foreground: string, border: string): void {
private applyStylesOnElement(element: HTMLElement, background: string | null, foreground: string | null, border: string | null): void {
if (element) {
element.style.backgroundColor = background;
element.style.color = foreground;
@@ -153,4 +151,4 @@ export class DropdownList extends Dropdown {
element.style.borderColor = border;
}
}
}
}

View File

@@ -5,7 +5,6 @@
import { Action } from 'vs/base/common/actions';
import { TPromise } from 'vs/base/common/winjs.base';
import * as nls from 'vs/nls';
export class ToggleDropdownAction extends Action {
private static readonly ID = 'dropdownAction.toggle';

View File

@@ -15,7 +15,6 @@ import { InputBox, IInputBoxStyles } from 'sql/base/browser/ui/inputBox/inputBox
import { IMessage, MessageType } from 'vs/base/browser/ui/inputbox/inputBox';
import { IListStyles } from 'vs/base/browser/ui/list/listWidget';
import * as DOM from 'vs/base/browser/dom';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { Disposable } from 'vs/base/common/lifecycle';
import { Color } from 'vs/base/common/color';
import * as nls from 'vs/nls';
@@ -74,14 +73,6 @@ const defaults: IDropdownOptions = {
actionLabel: nls.localize('dropdownAction.toggle', "Toggle dropdown")
};
interface ListResource {
label: string;
}
interface TableTemplate {
label: HTMLElement;
}
export class Dropdown extends Disposable {
private $el: Builder;
private $input: Builder;
@@ -110,12 +101,12 @@ export class Dropdown extends Disposable {
constructor(
container: HTMLElement,
contextViewService: IContextViewProvider,
private _themeService: IThemeService,
opt?: IDropdownOptions
) {
super();
this._contextView = new ContextView(document.body);
this._options = mixin(opt, defaults, false) as IDropdownOptions;
this._options = opt || Object.create(null);
mixin(this._options, defaults, false) as IDropdownOptions;
this.$el = $('.monaco-dropdown').style('width', '100%').appendTo(container);
this.$input = $('.dropdown-input').style('width', '100%').appendTo(this.$el);
@@ -125,7 +116,7 @@ export class Dropdown extends Disposable {
this._showList();
this._tree.domFocus();
this._tree.focusFirst();
}, opt.actionLabel);
}, this._options.actionLabel);
this._input = new InputBox(this.$input.getHTMLElement(), contextViewService, {
validationOptions: {
@@ -258,18 +249,18 @@ export class Dropdown extends Disposable {
return p;
}
}, 0);
let height = filteredLength * this._renderer.getHeight(undefined, undefined) > this._options.maxHeight ? this._options.maxHeight : filteredLength * this._renderer.getHeight(undefined, undefined);
let height = filteredLength * this._renderer.getHeight() > this._options.maxHeight! ? this._options.maxHeight! : filteredLength * this._renderer.getHeight();
this.$treeContainer.style('height', height + 'px').style('width', DOM.getContentWidth(this.$input.getHTMLElement()) - 2 + 'px');
this._tree.layout(parseInt(this.$treeContainer.style('height')));
this._tree.refresh();
}
}
public set values(vals: string[]) {
public set values(vals: string[] | undefined) {
if (vals) {
this._filter.filterString = '';
this._dataSource.options = vals.map(i => { return { value: i }; });
let height = this._dataSource.options.length * 22 > this._options.maxHeight ? this._options.maxHeight : this._dataSource.options.length * 22;
let height = this._dataSource.options.length * 22 > this._options.maxHeight! ? this._options.maxHeight! : this._dataSource.options.length * 22;
this.$treeContainer.style('height', height + 'px').style('width', DOM.getContentWidth(this.$input.getHTMLElement()) - 2 + 'px');
this._tree.layout(parseInt(this.$treeContainer.style('height')));
this._tree.setInput(new DropdownModel());
@@ -297,13 +288,15 @@ export class Dropdown extends Disposable {
style(style: IListStyles & IInputBoxStyles & IDropdownStyles) {
this._tree.style(style);
this._input.style(style);
this.$treeContainer.style('background-color', style.contextBackground.toString());
if (style.contextBackground) {
this.$treeContainer.style('background-color', style.contextBackground.toString());
}
this.$treeContainer.style('outline', `1px solid ${style.contextBorder || this._options.contextBorder}`);
}
private _inputValidator(value: string): IMessage {
if (this._dataSource.options && !this._dataSource.options.find(i => i.value === value)) {
if (this._options.strictSelection) {
if (this._options.strictSelection && this._options.errorMessage) {
return {
content: this._options.errorMessage,
type: MessageType.ERROR

View File

@@ -26,11 +26,11 @@ export class DropdownModel {
}
export class DropdownRenderer implements tree.IRenderer {
public getHeight(tree: tree.ITree, element: Resource): number {
public getHeight(): number {
return 22;
}
public getTemplateId(tree: tree.ITree, element: Resource): string {
public getTemplateId(): string {
return '';
}

View File

@@ -49,7 +49,7 @@ export class EditableDropDown extends AngularDisposable implements OnInit, OnCha
ariaLabel: '',
actionLabel: ''
};
this._selectbox = new Dropdown(this._el.nativeElement, this.contextViewService, this.themeService, dropdownOptions);
this._selectbox = new Dropdown(this._el.nativeElement, this.contextViewService, dropdownOptions);
this._selectbox.values = this.options;
this._selectbox.value = this.selectedOption;

View File

@@ -20,12 +20,12 @@ export interface IInputBoxStyles extends vsIInputBoxStyles {
}
export class InputBox extends vsInputBox {
private enabledInputBackground: Color;
private enabledInputForeground: Color;
private enabledInputBorder: Color;
private disabledInputBackground: Color;
private disabledInputForeground: Color;
private disabledInputBorder: Color;
private enabledInputBackground?: Color;
private enabledInputForeground?: Color;
private enabledInputBorder?: Color;
private disabledInputBackground?: Color;
private disabledInputForeground?: Color;
private disabledInputBorder?: Color;
private _lastLoseFocusValue: string;
@@ -41,8 +41,6 @@ export class InputBox extends vsInputBox {
this.enabledInputForeground = this.inputForeground;
this.enabledInputBorder = this.inputBorder;
this.disabledInputBackground = Color.transparent;
this.disabledInputForeground = null;
this.disabledInputBorder = null;
this._lastLoseFocusValue = this.value;
let self = this;
@@ -126,4 +124,4 @@ export class InputBox extends vsInputBox {
this.inputForeground = enabled ? this.enabledInputForeground : this.disabledInputForeground;
this.inputBorder = enabled ? this.enabledInputBorder : this.disabledInputBorder;
}
}
}

View File

@@ -13,7 +13,6 @@ import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { IContextViewProvider, AnchorAlignment } from 'vs/base/browser/ui/contextview/contextview';
import { RenderOptions, renderFormattedText, renderText } from 'vs/base/browser/htmlContentRenderer';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
const $ = dom.$;
@@ -33,27 +32,26 @@ export interface IListBoxStyles {
* Extends SelectBox to allow multiple selection and adding/remove items dynamically
*/
export class ListBox extends SelectBox {
private enabledSelectBackground: Color;
private enabledSelectForeground: Color;
private enabledSelectBorder: Color;
private disabledSelectBackground: Color;
private disabledSelectForeground: Color;
private disabledSelectBorder: Color;
private enabledSelectBackground?: Color;
private enabledSelectForeground?: Color;
private enabledSelectBorder?: Color;
private disabledSelectBackground?: Color;
private disabledSelectForeground?: Color;
private disabledSelectBorder?: Color;
private inputValidationInfoBorder: Color;
private inputValidationInfoBackground: Color;
private inputValidationWarningBorder: Color;
private inputValidationWarningBackground: Color;
private inputValidationErrorBorder: Color;
private inputValidationErrorBackground: Color;
private inputValidationInfoBorder?: Color;
private inputValidationInfoBackground?: Color;
private inputValidationWarningBorder?: Color;
private inputValidationWarningBackground?: Color;
private inputValidationErrorBorder?: Color;
private inputValidationErrorBackground?: Color;
private message: IMessage;
private message?: IMessage;
private contextViewProvider: IContextViewProvider;
private isValid: boolean;
constructor(
options: string[],
selectedOption: string,
contextViewProvider: IContextViewProvider,
private _clipboardService: IClipboardService) {
@@ -73,8 +71,6 @@ export class ListBox extends SelectBox {
this.enabledSelectForeground = this.selectForeground;
this.enabledSelectBorder = this.selectBorder;
this.disabledSelectBackground = Color.transparent;
this.disabledSelectForeground = null;
this.disabledSelectBorder = null;
this.inputValidationInfoBorder = defaultOpts.inputValidationInfoBorder;
this.inputValidationInfoBackground = defaultOpts.inputValidationInfoBackground;
@@ -112,7 +108,7 @@ export class ListBox extends SelectBox {
if (this.isValid) {
this.selectElement.style.border = `1px solid ${this.selectBorder}`;
} else {
} else if (this.message) {
const styles = this.stylesForType(this.message.type);
this.selectElement.style.border = styles.border ? `1px solid ${styles.border}` : null;
}
@@ -123,7 +119,7 @@ export class ListBox extends SelectBox {
}
public get selectedOptions(): string[] {
let selected = [];
let selected: string[] = [];
for (let i = 0; i < this.selectElement.selectedOptions.length; i++) {
selected.push(this.selectElement.selectedOptions[i].innerHTML);
}
@@ -136,7 +132,7 @@ export class ListBox extends SelectBox {
// Remove selected options
public remove(): void {
let indexes = [];
let indexes: number[] = [];
for (let i = 0; i < this.selectElement.selectedOptions.length; i++) {
indexes.push(this.selectElement.selectedOptions[i].index);
}
@@ -219,24 +215,26 @@ export class ListBox extends SelectBox {
className: 'monaco-inputbox-message'
};
let spanElement: HTMLElement = (this.message.formatContent
? renderFormattedText(this.message.content, renderOptions)
: renderText(this.message.content, renderOptions)) as any;
dom.addClass(spanElement, this.classForType(this.message.type));
if (this.message) {
let spanElement: HTMLElement = (this.message.formatContent
? renderFormattedText(this.message.content, renderOptions)
: renderText(this.message.content, renderOptions)) as any;
dom.addClass(spanElement, this.classForType(this.message.type));
const styles = this.stylesForType(this.message.type);
spanElement.style.backgroundColor = styles.background ? styles.background.toString() : null;
spanElement.style.border = styles.border ? `1px solid ${styles.border}` : null;
const styles = this.stylesForType(this.message.type);
spanElement.style.backgroundColor = styles.background ? styles.background.toString() : null;
spanElement.style.border = styles.border ? `1px solid ${styles.border}` : null;
dom.append(div, spanElement);
dom.append(div, spanElement);
}
return null;
return { dispose: () => { } };
},
layout: layout
});
}
private classForType(type: MessageType): string {
private classForType(type?: MessageType): string {
switch (type) {
case MessageType.INFO: return 'info';
case MessageType.WARNING: return 'warning';
@@ -244,11 +242,11 @@ export class ListBox extends SelectBox {
}
}
private stylesForType(type: MessageType): { border: Color; background: Color } {
private stylesForType(type?: MessageType): { border?: Color; background?: Color } {
switch (type) {
case MessageType.INFO: return { border: this.inputValidationInfoBorder, background: this.inputValidationInfoBackground };
case MessageType.WARNING: return { border: this.inputValidationWarningBorder, background: this.inputValidationWarningBackground };
default: return { border: this.inputValidationErrorBorder, background: this.inputValidationErrorBackground };
}
}
}
}

View File

@@ -81,11 +81,11 @@ export class PanelComponent extends Disposable {
private _actionbar: ActionBar;
private _mru: TabComponent[];
protected ScrollbarVisibility = ScrollbarVisibility;
protected NavigationBarLayout = NavigationBarLayout;
protected ScrollbarVisibility = ScrollbarVisibility; // used by angular template
protected NavigationBarLayout = NavigationBarLayout; // used by angular template
@ViewChild('panelActionbar', { read: ElementRef }) private _actionbarRef: ElementRef;
constructor( @Inject(forwardRef(() => NgZone)) private _zone: NgZone) {
constructor(@Inject(forwardRef(() => NgZone)) private _zone: NgZone) {
super();
}

View File

@@ -1,79 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { ITask, createCancelablePromise, CancelablePromise } from 'vs/base/common/async';
import { Promise, TPromise, ValueCallback } from 'vs/base/common/winjs.base';
/**
* Bases on vscode Delayer, however, it works by only running the task if it gets
* a specified number of requests in a specified time
*
* Ex. Useful encapsulation of handling double click from click listeners
*/
export class MultipleRequestDelayer<T> {
private timeout: NodeJS.Timer;
private completionPromise: CancelablePromise<any>;
private onSuccess: ValueCallback;
private requests: number = 0;
private task: ITask<T>;
constructor(public delay: number, private maxRequests: number = 2) {
this.timeout = null;
this.completionPromise = null;
this.onSuccess = null;
}
trigger(task: ITask<T>): TPromise<T> {
this.cancelTimeout();
this.task = task;
if (++this.requests > this.maxRequests - 1) {
this.requests = 0;
this.onSuccess(null);
return this.completionPromise;
}
if (!this.completionPromise) {
this.completionPromise = createCancelablePromise<T>(() => new Promise(resolve => this.onSuccess = resolve).then(() => {
this.completionPromise = null;
this.onSuccess = null;
const task = this.task;
this.task = null;
return task();
}));
}
this.timeout = setTimeout(() => {
this.timeout = null;
this.requests = 0;
}, this.delay);
return this.completionPromise;
}
isTriggered(): boolean {
return this.timeout !== null;
}
cancel(): void {
this.cancelTimeout();
if (this.completionPromise) {
this.completionPromise.cancel();
this.completionPromise = null;
}
}
private cancelTimeout(): void {
if (this.timeout !== null) {
clearTimeout(this.timeout);
this.timeout = null;
}
}
}

View File

@@ -9,12 +9,16 @@
* Alterable version of the vs memorize function; to unmemoize use unmemoize
*/
export function memoize(target: any, key: string, descriptor: any) {
let fnKey: string = null;
let fn: Function = null;
let fnKey: string | null = null;
let fn: Function | null = null;
if (typeof descriptor.value === 'function') {
fnKey = 'value';
fn = descriptor.value;
if (fn!.length !== 0) {
console.warn('Memoize should only be used in functions with zero parameters');
}
} else if (typeof descriptor.get === 'function') {
fnKey = 'get';
fn = descriptor.get;
@@ -26,13 +30,13 @@ export function memoize(target: any, key: string, descriptor: any) {
const memoizeKey = `$memoize$${key}`;
descriptor[fnKey] = function (...args: any[]) {
descriptor[fnKey!] = function (...args: any[]) {
if (!this.hasOwnProperty(memoizeKey)) {
Object.defineProperty(this, memoizeKey, {
configurable: true,
enumerable: false,
writable: false,
value: fn.apply(this, args)
value: fn!.apply(this, args)
});
}
@@ -45,4 +49,4 @@ export function unmemoize(target: Object, key: string) {
if (target.hasOwnProperty(memoizeKey)) {
delete target[memoizeKey];
}
}
}

View File

@@ -1,89 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { Emitter, Event } from 'vs/base/common/event';
import { IDisposable } from 'vs/base/common/lifecycle';
/**
* Implementation of vs/base/common/event/echo that is clearable
* Similar to `buffer` but it buffers indefinitely and repeats
* the buffered events to every new listener.
*/
export function echo<T>(event: Event<T>, nextTick = false, buffer: T[] = []): { clear: () => void; event: Event<T> } {
buffer = buffer.slice();
event(e => {
buffer.push(e);
emitter.fire(e);
});
const flush = (listener: (e: T) => any, thisArgs?: any) => buffer.forEach(e => listener.call(thisArgs, e));
const clear = () => buffer = [];
const emitter = new Emitter<T>({
onListenerDidAdd(emitter, listener: (e: T) => any, thisArgs?: any) {
if (nextTick) {
setTimeout(() => flush(listener, thisArgs));
} else {
flush(listener, thisArgs);
}
}
});
return {
event: emitter.event,
clear
};
}
/**
* Implementation of vs/base/common/event/debounceEvent that is clearable
*/
export function debounceEvent<T>(event: Event<T>, merger: (last: T, event: T) => T, delay?: number, leading?: boolean): { clear: () => void; event: Event<T> };
export function debounceEvent<I, O>(event: Event<I>, merger: (last: O, event: I) => O, delay?: number, leading?: boolean): { clear: () => void; event: Event<O> };
export function debounceEvent<I, O>(event: Event<I>, merger: (last: O, event: I) => O, delay: number = 100, leading = false): { clear: () => void; event: Event<O> } {
let subscription: IDisposable;
let output: O = undefined;
let handle: any = undefined;
let numDebouncedCalls = 0;
const clear = () => output = undefined;
const emitter = new Emitter<O>({
onFirstListenerAdd() {
subscription = event(cur => {
numDebouncedCalls++;
output = merger(output, cur);
if (leading && !handle) {
emitter.fire(output);
}
clearTimeout(handle);
handle = setTimeout(() => {
let _output = output;
output = undefined;
handle = undefined;
if (!leading || numDebouncedCalls > 1) {
emitter.fire(_output);
}
numDebouncedCalls = 0;
}, delay);
});
},
onLastListenerRemove() {
subscription.dispose();
}
});
return {
event: emitter.event,
clear
};
}

View File

@@ -12,7 +12,7 @@ export class EmitterEvent {
public readonly type: string;
public readonly data: any;
constructor(eventType: string = null, data: any = null) {
constructor(eventType: string, data: any) {
this.type = eventType;
this.data = data;
}
@@ -46,9 +46,9 @@ export class EventEmitter implements IEventEmitter {
protected _bulkListeners: ListenerCallback[];
private _collectedEvents: EmitterEvent[];
private _deferredCnt: number;
private _allowedEventTypes: { [eventType: string]: boolean; };
private _allowedEventTypes: { [eventType: string]: boolean; } | null;
constructor(allowedEventTypes: string[] = null) {
constructor(allowedEventTypes?: string[]) {
this._listeners = {};
this._bulkListeners = [];
this._collectedEvents = [];
@@ -86,7 +86,8 @@ export class EventEmitter implements IEventEmitter {
this._listeners[eventType] = [listener];
}
let bound = this;
let bound: this | null = this;
let _listener: ListenerCallback | null = listener;
return {
dispose: () => {
if (!bound) {
@@ -94,11 +95,11 @@ export class EventEmitter implements IEventEmitter {
return;
}
bound._removeListener(eventType, listener);
bound._removeListener(eventType, _listener!);
// Prevent leakers from holding on to the event emitter
bound = null;
listener = null;
_listener = null;
}
};
}
@@ -212,10 +213,10 @@ export class EventEmitter implements IEventEmitter {
}
}
public deferredEmit<T>(callback: () => T): T {
public deferredEmit<T>(callback: () => T): T | undefined {
this.beginDeferredEmit();
let result: T = safeInvokeNoArg<T>(callback);
let result: T | undefined = safeInvokeNoArg<T>(callback);
this.endDeferredEmit();
@@ -251,7 +252,7 @@ export class OrderGuaranteeEventEmitter extends EventEmitter {
private _emitQueue: EmitQueueElement[];
constructor() {
super(null);
super();
this._emitQueue = [];
}
@@ -276,12 +277,14 @@ export class OrderGuaranteeEventEmitter extends EventEmitter {
while (this._emitQueue.length > 0) {
let queueElement = this._emitQueue.shift();
safeInvoke1Arg(queueElement.target, queueElement.arg);
if (queueElement) {
safeInvoke1Arg(queueElement.target, queueElement.arg);
}
}
}
}
function safeInvokeNoArg<T>(func: Function): T {
function safeInvokeNoArg<T>(func: Function): T | undefined {
try {
return func();
} catch (e) {

View File

@@ -5,113 +5,6 @@
'use strict';
// --- trie'ish datastructure
class Node<E> {
element?: E;
readonly children = new Map<string, Node<E>>();
}
/**
* A trie map that allows for fast look up when keys are substrings
* to the actual search keys (dir/subdir-problem).
*/
export class TrieMap<E> {
static PathSplitter = (s: string) => s.split(/[\\/]/).filter(s => !!s);
private readonly _splitter: (s: string) => string[];
private _root = new Node<E>();
constructor(splitter: (s: string) => string[] = TrieMap.PathSplitter) {
this._splitter = s => splitter(s).filter(s => Boolean(s));
}
insert(path: string, element: E): void {
const parts = this._splitter(path);
let i = 0;
// find insertion node
let node = this._root;
for (; i < parts.length; i++) {
let child = node.children.get(parts[i]);
if (child) {
node = child;
continue;
}
break;
}
// create new nodes
let newNode: Node<E>;
for (; i < parts.length; i++) {
newNode = new Node<E>();
node.children.set(parts[i], newNode);
node = newNode;
}
node.element = element;
}
lookUp(path: string): E {
const parts = this._splitter(path);
let { children } = this._root;
let node: Node<E>;
for (const part of parts) {
node = children.get(part);
if (!node) {
return undefined;
}
children = node.children;
}
return node.element;
}
findSubstr(path: string): E {
const parts = this._splitter(path);
let lastNode: Node<E>;
let { children } = this._root;
for (const part of parts) {
const node = children.get(part);
if (!node) {
break;
}
if (node.element) {
lastNode = node;
}
children = node.children;
}
// return the last matching node
// that had an element
if (lastNode) {
return lastNode.element;
}
return undefined;
}
findSuperstr(path: string): TrieMap<E> {
const parts = this._splitter(path);
let { children } = this._root;
let node: Node<E>;
for (const part of parts) {
node = children.get(part);
if (!node) {
return undefined;
}
children = node.children;
}
const result = new TrieMap<E>(this._splitter);
result._root = node;
return result;
}
}
export function toObject<V>(map: Map<string, V>): { [key: string]: V } {
if (map) {
let rt: { [key: string]: V } = Object.create(null);