xterm
[VSoRC/.git] / node_modules / xterm / src / browser / RenderDebouncer.ts
1 /**
2  * Copyright (c) 2018 The xterm.js authors. All rights reserved.
3  * @license MIT
4  */
5
6 import { IDisposable } from 'common/Types';
7
8 /**
9  * Debounces calls to render terminal rows using animation frames.
10  */
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;
16
17   constructor(
18     private _renderCallback: (start: number, end: number) => void
19   ) {
20   }
21
22   public dispose(): void {
23     if (this._animationFrame) {
24       window.cancelAnimationFrame(this._animationFrame);
25       this._animationFrame = undefined;
26     }
27   }
28
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;
37
38     if (this._animationFrame) {
39       return;
40     }
41
42     this._animationFrame = window.requestAnimationFrame(() => this._innerRefresh());
43   }
44
45   private _innerRefresh(): void {
46     // Make sure values are set
47     if (this._rowStart === undefined || this._rowEnd === undefined || this._rowCount === undefined) {
48       return;
49     }
50
51     // Clamp values
52     this._rowStart = Math.max(this._rowStart, 0);
53     this._rowEnd = Math.min(this._rowEnd, this._rowCount - 1);
54
55     // Run render callback
56     this._renderCallback(this._rowStart, this._rowEnd);
57
58     // Reset debouncer
59     this._rowStart = undefined;
60     this._rowEnd = undefined;
61     this._animationFrame = undefined;
62   }
63 }