| 1 |
/** |
| 2 |
* Copyright (c) 2018 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { IRenderDebouncerWithCallback } from 'browser/Types'; |
| 7 |
import { ICoreBrowserService } from 'browser/services/Services'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Debounces calls to render terminal rows using animation frames. |
| 11 |
*/ |
| 12 |
export class RenderDebouncer implements IRenderDebouncerWithCallback { |
| 13 |
private _rowStart: number | undefined; |
| 14 |
private _rowEnd: number | undefined; |
| 15 |
private _rowCount: number | undefined; |
| 16 |
private _animationFrame: number | undefined; |
| 17 |
private _refreshCallbacks: FrameRequestCallback[] = []; |
| 18 |
|
| 19 |
constructor( |
| 20 |
private _renderCallback: (start: number, end: number) => void, |
| 21 |
private readonly _coreBrowserService: ICoreBrowserService |
| 22 |
) { |
| 23 |
} |
| 24 |
|
| 25 |
public dispose(): void { |
| 26 |
if (this._animationFrame) { |
| 27 |
this._coreBrowserService.window.cancelAnimationFrame(this._animationFrame); |
| 28 |
this._animationFrame = undefined; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
public addRefreshCallback(callback: FrameRequestCallback): number { |
| 33 |
this._refreshCallbacks.push(callback); |
| 34 |
if (!this._animationFrame) { |
| 35 |
this._animationFrame = this._coreBrowserService.window.requestAnimationFrame(() => this._innerRefresh()); |
| 36 |
} |
| 37 |
return this._animationFrame; |
| 38 |
} |
| 39 |
|
| 40 |
public refresh(rowStart: number | undefined, rowEnd: number | undefined, rowCount: number): void { |
| 41 |
this._rowCount = rowCount; |
| 42 |
// Get the min/max row start/end for the arg values |
| 43 |
rowStart = rowStart !== undefined ? rowStart : 0; |
| 44 |
rowEnd = rowEnd !== undefined ? rowEnd : this._rowCount - 1; |
| 45 |
// Set the properties to the updated values |
| 46 |
this._rowStart = this._rowStart !== undefined ? Math.min(this._rowStart, rowStart) : rowStart; |
| 47 |
this._rowEnd = this._rowEnd !== undefined ? Math.max(this._rowEnd, rowEnd) : rowEnd; |
| 48 |
|
| 49 |
if (this._animationFrame) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
this._animationFrame = this._coreBrowserService.window.requestAnimationFrame(() => this._innerRefresh()); |
| 54 |
} |
| 55 |
|
| 56 |
private _innerRefresh(): void { |
| 57 |
this._animationFrame = undefined; |
| 58 |
|
| 59 |
// Make sure values are set |
| 60 |
if (this._rowStart === undefined || this._rowEnd === undefined || this._rowCount === undefined) { |
| 61 |
this._runRefreshCallbacks(); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// Clamp values |
| 66 |
const start = Math.max(this._rowStart, 0); |
| 67 |
const end = Math.min(this._rowEnd, this._rowCount - 1); |
| 68 |
|
| 69 |
// Reset debouncer (this happens before render callback as the render could trigger it again) |
| 70 |
this._rowStart = undefined; |
| 71 |
this._rowEnd = undefined; |
| 72 |
|
| 73 |
// Run render callback |
| 74 |
this._renderCallback(start, end); |
| 75 |
this._runRefreshCallbacks(); |
| 76 |
} |
| 77 |
|
| 78 |
private _runRefreshCallbacks(): void { |
| 79 |
for (const callback of this._refreshCallbacks) { |
| 80 |
callback(0); |
| 81 |
} |
| 82 |
this._refreshCallbacks = []; |
| 83 |
} |
| 84 |
} |