Skip to content

Commit

Permalink
Fix strict null checks in ./common
Browse files Browse the repository at this point in the history
Part of #1319
  • Loading branch information
Tyriar committed Sep 4, 2018
1 parent fb72f25 commit 7c1b1e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/common/CircularList.ts
Expand Up @@ -11,7 +11,7 @@ import { ICircularList } from './Types';
* overriding values at the start of the list.
*/
export class CircularList<T> extends EventEmitter implements ICircularList<T> {
protected _array: T[];
protected _array: (T | undefined)[];
private _startIndex: number;
private _length: number;

Expand All @@ -36,7 +36,7 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {

// Reconstruct array, starting at index 0. Only transfer values from the
// indexes 0 to length.
const newArray = new Array<T>(newMaxLength);
const newArray = new Array<T | undefined>(newMaxLength);
for (let i = 0; i < Math.min(newMaxLength, this.length); i++) {
newArray[i] = this._array[this._getCyclicIndex(i)];
}
Expand Down Expand Up @@ -66,7 +66,7 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
* @param index The index of the value to get.
* @return The value corresponding to the index.
*/
public get(index: number): T {
public get(index: number): T | undefined {
return this._array[this._getCyclicIndex(index)];
}

Expand All @@ -78,7 +78,7 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
* @param index The index to set.
* @param value The value to set.
*/
public set(index: number, value: T): void {
public set(index: number, value: T | undefined): void {
this._array[this._getCyclicIndex(index)] = value;
}

Expand All @@ -104,7 +104,7 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
* Removes and returns the last value on the list.
* @return The popped value.
*/
public pop(): T {
public pop(): T | undefined {
return this._array[this._getCyclicIndex(this._length-- - 1)];
}

Expand Down
5 changes: 3 additions & 2 deletions src/common/EventEmitter.ts
Expand Up @@ -30,14 +30,15 @@ export class EventEmitter extends Disposable implements IEventEmitter, IDisposab
public addDisposableListener(type: string, handler: XtermListener): IDisposable {
// TODO: Rename addDisposableEventListener to more easily disambiguate from Dom listener
this.on(type, handler);
let disposed = false;
return {
dispose: () => {
if (!handler) {
if (disposed) {
// Already disposed
return;
}
this.off(type, handler);
handler = null;
disposed = true;
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/Types.ts
Expand Up @@ -25,10 +25,10 @@ export interface ICircularList<T> extends IEventEmitter {
length: number;
maxLength: number;

get(index: number): T;
get(index: number): T | undefined;
set(index: number, value: T): void;
push(value: T): void;
pop(): T;
pop(): T | undefined;
splice(start: number, deleteCount: number, ...items: T[]): void;
trimStart(count: number): void;
shiftElements(start: number, count: number, offset: number): void;
Expand Down

0 comments on commit 7c1b1e2

Please sign in to comment.