| 1 |
/** |
| 2 |
* Copyright (c) 2018 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
const RENDER_DEBOUNCE_THRESHOLD_MS = 1000; // 1 Second |
| 7 |
|
| 8 |
import { IRenderDebouncer } from 'browser/Types'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Debounces calls to update screen readers to update at most once configurable interval of time. |
| 12 |
*/ |
| 13 |
export class TimeBasedDebouncer implements IRenderDebouncer { |
| 14 |
private _rowStart: number | undefined; |
| 15 |
private _rowEnd: number | undefined; |
| 16 |
private _rowCount: number | undefined; |
| 17 |
|
| 18 |
// The last moment that the Terminal was refreshed at |
| 19 |
private _lastRefreshMs = 0; |
| 20 |
// Whether a trailing refresh should be triggered due to a refresh request that was throttled |
| 21 |
private _additionalRefreshRequested = false; |
| 22 |
|
| 23 |
private _refreshTimeoutID: number | undefined; |
| 24 |
|
| 25 |
constructor( |
| 26 |
private _renderCallback: (start: number, end: number) => void, |
| 27 |
private readonly _debounceThresholdMS = RENDER_DEBOUNCE_THRESHOLD_MS |
| 28 |
) { |
| 29 |
} |
| 30 |
|
| 31 |
public dispose(): void { |
| 32 |
if (this._refreshTimeoutID) { |
| 33 |
clearTimeout(this._refreshTimeoutID); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public refresh(rowStart: number | undefined, rowEnd: number | undefined, rowCount: number): void { |
| 38 |
this._rowCount = rowCount; |
| 39 |
// Get the min/max row start/end for the arg values |
| 40 |
rowStart = rowStart !== undefined ? rowStart : 0; |
| 41 |
rowEnd = rowEnd !== undefined ? rowEnd : this._rowCount - 1; |
| 42 |
// Set the properties to the updated values |
| 43 |
this._rowStart = this._rowStart !== undefined ? Math.min(this._rowStart, rowStart) : rowStart; |
| 44 |
this._rowEnd = this._rowEnd !== undefined ? Math.max(this._rowEnd, rowEnd) : rowEnd; |
| 45 |
|
| 46 |
// Only refresh if the time since last refresh is above a threshold, otherwise wait for |
| 47 |
// enough time to pass before refreshing again. |
| 48 |
const refreshRequestTime: number = performance.now(); |
| 49 |
if (refreshRequestTime - this._lastRefreshMs >= this._debounceThresholdMS) { |
| 50 |
// Enough time has lapsed since the last refresh; refresh immediately |
| 51 |
this._lastRefreshMs = refreshRequestTime; |
| 52 |
this._innerRefresh(); |
| 53 |
} else if (!this._additionalRefreshRequested) { |
| 54 |
// This is the first additional request throttled; set up trailing refresh |
| 55 |
const elapsed = refreshRequestTime - this._lastRefreshMs; |
| 56 |
const waitPeriodBeforeTrailingRefresh = this._debounceThresholdMS - elapsed; |
| 57 |
this._additionalRefreshRequested = true; |
| 58 |
|
| 59 |
this._refreshTimeoutID = window.setTimeout(() => { |
| 60 |
this._lastRefreshMs = performance.now(); |
| 61 |
this._innerRefresh(); |
| 62 |
this._additionalRefreshRequested = false; |
| 63 |
this._refreshTimeoutID = undefined; // No longer need to clear the timeout |
| 64 |
}, waitPeriodBeforeTrailingRefresh); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
private _innerRefresh(): void { |
| 69 |
// Make sure values are set |
| 70 |
if (this._rowStart === undefined || this._rowEnd === undefined || this._rowCount === undefined) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Clamp values |
| 75 |
const start = Math.max(this._rowStart, 0); |
| 76 |
const end = Math.min(this._rowEnd, this._rowCount - 1); |
| 77 |
|
| 78 |
// Reset debouncer (this happens before render callback as the render could trigger it again) |
| 79 |
this._rowStart = undefined; |
| 80 |
this._rowEnd = undefined; |
| 81 |
|
| 82 |
// Run render callback |
| 83 |
this._renderCallback(start, end); |
| 84 |
} |
| 85 |
} |
| 86 |
|