Strict null check RenderDebouncer

This commit is contained in:
Daniel Imms
2019-04-28 02:03:41 +00:00
parent 4c3082c924
commit ebfa57b71c
2 changed files with 22 additions and 19 deletions
+20 -18
View File
@@ -3,40 +3,37 @@
* @license MIT * @license MIT
*/ */
import { IDisposable } from 'xterm'; import { IDisposable } from '../common/Types';
/** /**
* Debounces calls to render terminal rows using animation frames. * Debounces calls to render terminal rows using animation frames.
*/ */
export class RenderDebouncer implements IDisposable { export class RenderDebouncer implements IDisposable {
private _rowStart: number; private _rowStart: number | undefined;
private _rowEnd: number; private _rowEnd: number | undefined;
private _rowCount: number; private _rowCount: number | undefined;
private _animationFrame: number = null; private _animationFrame: number | undefined;
constructor( constructor(
private _callback: (start: number, end: number) => void private _renderCallback: (start: number, end: number) => void
) { ) {
} }
public dispose(): void { public dispose(): void {
if (this._animationFrame) { if (this._animationFrame) {
window.cancelAnimationFrame(this._animationFrame); window.cancelAnimationFrame(this._animationFrame);
this._animationFrame = null; this._animationFrame = undefined;
} }
} }
public refresh(rowStart: number, rowEnd: number, rowCount: number): void { public refresh(rowStart: number, rowEnd: number, rowCount: number): void {
this._rowCount = rowCount; this._rowCount = rowCount;
// Get the min/max row start/end for the arg values // Get the min/max row start/end for the arg values
rowStart = rowStart !== null && rowStart !== undefined ? rowStart : 0; rowStart = rowStart !== undefined ? rowStart : 0;
rowEnd = rowEnd !== null && rowEnd !== undefined ? rowEnd : this._rowCount - 1; rowEnd = rowEnd !== undefined ? rowEnd : this._rowCount - 1;
// Check whether the row start/end values have already been set
const isRowStartSet = this._rowStart !== undefined && this._rowStart !== null;
const isRowEndSet = this._rowEnd !== undefined && this._rowEnd !== null;
// Set the properties to the updated values // Set the properties to the updated values
this._rowStart = isRowStartSet ? Math.min(this._rowStart, rowStart) : rowStart; this._rowStart = this._rowStart !== undefined ? Math.min(this._rowStart, rowStart) : rowStart;
this._rowEnd = isRowEndSet ? Math.max(this._rowEnd, rowEnd) : rowEnd; this._rowEnd = this._rowEnd !== undefined ? Math.max(this._rowEnd, rowEnd) : rowEnd;
if (this._animationFrame) { if (this._animationFrame) {
return; return;
@@ -46,16 +43,21 @@ export class RenderDebouncer implements IDisposable {
} }
private _innerRefresh(): void { private _innerRefresh(): void {
// Make sure values are set
if (this._rowStart === undefined || this._rowEnd === undefined || this._rowCount === undefined) {
return;
}
// Clamp values // Clamp values
this._rowStart = Math.max(this._rowStart, 0); this._rowStart = Math.max(this._rowStart, 0);
this._rowEnd = Math.min(this._rowEnd, this._rowCount - 1); this._rowEnd = Math.min(this._rowEnd, this._rowCount - 1);
// Run render callback // Run render callback
this._callback(this._rowStart, this._rowEnd); this._renderCallback(this._rowStart, this._rowEnd);
// Reset debouncer // Reset debouncer
this._rowStart = null; this._rowStart = undefined;
this._rowEnd = null; this._rowEnd = undefined;
this._animationFrame = null; this._animationFrame = undefined;
} }
} }
+2 -1
View File
@@ -11,7 +11,8 @@
] ]
}, },
"include": [ "include": [
"./Lifecycle.ts" "./Lifecycle.ts",
"./RenderDebouncer.ts"
], ],
"references": [ "references": [
{ "path": "../common" } { "path": "../common" }