Skip to content

Commit 8ffcbd9

Browse files
committed
Replace timeout handling with disposableTimeout
- Use disposableTimeout instead of manual timeout management - Replace MutableDisposable pattern for SearchAddon highlight timeout - Replace SearchLineCache timeout with MutableDisposable approach - Clean up timeout disposal logic in both classes
1 parent 15d9082 commit 8ffcbd9

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

addons/addon-search/src/SearchAddon.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { Terminal, IDisposable, ITerminalAddon, IDecoration } from '@xterm/
77
import type { SearchAddon as ISearchApi, ISearchOptions, ISearchDecorationOptions } from '@xterm/addon-search';
88
import { Emitter, Event } from 'vs/base/common/event';
99
import { Disposable, dispose, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
10+
import { disposableTimeout } from 'vs/base/common/async';
1011
import { SearchLineCache } from './SearchLineCache';
1112

1213
interface IInternalSearchOptions {
@@ -34,8 +35,6 @@ export interface ISearchResult {
3435
size: number;
3536
}
3637

37-
38-
3938
interface IHighlight extends IDisposable {
4039
decoration: IDecoration;
4140
match: ISearchResult;
@@ -57,8 +56,6 @@ const enum Constants {
5756
*/
5857
NON_WORD_CHARACTERS = ' ~!@#$%^&*()+`-=[]{}|\\;:"\',./<>?',
5958

60-
61-
6259
/**
6360
* Default maximum number of search results to highlight simultaneously. This limit prevents
6461
* performance degradation when searching for very common terms that would result in excessive
@@ -73,10 +70,10 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
7370
private _highlightedLines: Set<number> = new Set();
7471
private _highlightDecorations: IHighlight[] = [];
7572
private _searchResultsWithHighlight: ISearchResult[] = [];
76-
private _selectedDecoration: MutableDisposable<IMultiHighlight> = this._register(new MutableDisposable());
73+
private _selectedDecoration = this._register(new MutableDisposable<IMultiHighlight>());
7774
private _highlightLimit: number;
7875
private _lastSearchOptions: ISearchOptions | undefined;
79-
private _highlightTimeout: number | undefined;
76+
private _highlightTimeout = this._register(new MutableDisposable<IDisposable>());
8077
private _lineCache = this._register(new MutableDisposable<SearchLineCache>());
8178

8279
private readonly _onDidChangeResults = this._register(new Emitter<ISearchResultChangeEvent>());
@@ -97,11 +94,9 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
9794
}
9895

9996
private _updateMatches(): void {
100-
if (this._highlightTimeout) {
101-
window.clearTimeout(this._highlightTimeout);
102-
}
97+
this._highlightTimeout.clear();
10398
if (this._cachedSearchTerm && this._lastSearchOptions?.decorations) {
104-
this._highlightTimeout = setTimeout(() => {
99+
this._highlightTimeout.value = disposableTimeout(() => {
105100
const term = this._cachedSearchTerm;
106101
this._cachedSearchTerm = undefined;
107102
this.findPrevious(term!, { ...this._lastSearchOptions, incremental: true }, { noScroll: true });

addons/addon-search/src/SearchLineCache.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import type { Terminal } from '@xterm/xterm';
77
import { combinedDisposable, Disposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
8+
import { disposableTimeout } from 'vs/base/common/async';
89

910
export type LineCacheEntry = [
1011
/**
@@ -35,7 +36,7 @@ export class SearchLineCache extends Disposable {
3536
* _linesCache is also invalidated when the terminal cursor moves.
3637
*/
3738
private _linesCache: LineCacheEntry[] | undefined;
38-
private _linesCacheTimeoutId = 0;
39+
private _linesCacheTimeout = this._register(new MutableDisposable());
3940
private _linesCacheDisposables = this._register(new MutableDisposable());
4041

4142
constructor(private _terminal: Terminal) {
@@ -56,17 +57,13 @@ export class SearchLineCache extends Disposable {
5657
);
5758
}
5859

59-
window.clearTimeout(this._linesCacheTimeoutId);
60-
this._linesCacheTimeoutId = window.setTimeout(() => this._destroyLinesCache(), Constants.LINES_CACHE_TIME_TO_LIVE);
60+
this._linesCacheTimeout.value = disposableTimeout(() => this._destroyLinesCache(), Constants.LINES_CACHE_TIME_TO_LIVE);
6161
}
6262

6363
private _destroyLinesCache(): void {
6464
this._linesCache = undefined;
6565
this._linesCacheDisposables.clear();
66-
if (this._linesCacheTimeoutId) {
67-
window.clearTimeout(this._linesCacheTimeoutId);
68-
this._linesCacheTimeoutId = 0;
69-
}
66+
this._linesCacheTimeout.clear();
7067
}
7168

7269
public getLineFromCache(row: number): LineCacheEntry | undefined {

0 commit comments

Comments
 (0)