2 * Copyright (c) 2018 The xterm.js authors. All rights reserved.
6 import { IDisposable } from 'common/Types';
9 * Debounces calls to render terminal rows using animation frames.
11 export class RenderDebouncer implements IDisposable {
12 private _rowStart: number | undefined;
13 private _rowEnd: number | undefined;
14 private _rowCount: number | undefined;
15 private _animationFrame: number | undefined;
18 private _renderCallback: (start: number, end: number) => void
22 public dispose(): void {
23 if (this._animationFrame) {
24 window.cancelAnimationFrame(this._animationFrame);
25 this._animationFrame = undefined;
29 public refresh(rowStart: number, rowEnd: number, rowCount: number): void {
30 this._rowCount = rowCount;
31 // Get the min/max row start/end for the arg values
32 rowStart = rowStart !== undefined ? rowStart : 0;
33 rowEnd = rowEnd !== undefined ? rowEnd : this._rowCount - 1;
34 // Set the properties to the updated values
35 this._rowStart = this._rowStart !== undefined ? Math.min(this._rowStart, rowStart) : rowStart;
36 this._rowEnd = this._rowEnd !== undefined ? Math.max(this._rowEnd, rowEnd) : rowEnd;
38 if (this._animationFrame) {
42 this._animationFrame = window.requestAnimationFrame(() => this._innerRefresh());
45 private _innerRefresh(): void {
46 // Make sure values are set
47 if (this._rowStart === undefined || this._rowEnd === undefined || this._rowCount === undefined) {
52 this._rowStart = Math.max(this._rowStart, 0);
53 this._rowEnd = Math.min(this._rowEnd, this._rowCount - 1);
55 // Run render callback
56 this._renderCallback(this._rowStart, this._rowEnd);
59 this._rowStart = undefined;
60 this._rowEnd = undefined;
61 this._animationFrame = undefined;