Skip to content

Commit d3aa113

Browse files
committed
Go back to using setImmediate for background tokenization (microsoft#138887)
1 parent fe719cd commit d3aa113

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/vs/base/common/platform.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface INodeProcess {
3939
platform: string;
4040
arch: string;
4141
env: IProcessEnvironment;
42+
nextTick?: (callback: (...args: any[]) => void) => void;
4243
versions?: {
4344
electron?: string;
4445
};
@@ -189,6 +190,10 @@ export const locale = _locale;
189190
*/
190191
export const translationsConfigFile = _translationsConfigFile;
191192

193+
interface ISetImmediate {
194+
(callback: (...args: unknown[]) => void): void;
195+
}
196+
192197
/**
193198
* See https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#:~:text=than%204%2C%20then-,set%20timeout%20to%204,-.
194199
*
@@ -227,6 +232,20 @@ export const setTimeout0 = (() => {
227232
return (callback: () => void) => setTimeout(callback);
228233
})();
229234

235+
export const setImmediate: ISetImmediate = (function defineSetImmediate() {
236+
if (globals.setImmediate) {
237+
return globals.setImmediate.bind(globals);
238+
}
239+
if (typeof globals.postMessage === 'function' && !globals.importScripts) {
240+
return setTimeout0;
241+
}
242+
if (typeof nodeProcess?.nextTick === 'function') {
243+
return nodeProcess.nextTick.bind(nodeProcess);
244+
}
245+
const _promise = Promise.resolve();
246+
return (callback: (...args: unknown[]) => void) => _promise.then(callback);
247+
})();
248+
230249
export const enum OperatingSystem {
231250
Windows = 1,
232251
Macintosh = 2,

src/vs/editor/common/model/textModelTokens.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { TextModel } from 'vs/editor/common/model/textModel';
1515
import { Disposable } from 'vs/base/common/lifecycle';
1616
import { StopWatch } from 'vs/base/common/stopwatch';
1717
import { MultilineTokensBuilder, countEOL } from 'vs/editor/common/model/tokensStore';
18-
import { runWhenIdle, IdleDeadline } from 'vs/base/common/async';
18+
import { setImmediate } from 'vs/base/common/platform';
1919

2020
const enum Constants {
2121
CHEAP_TOKENIZATION_LENGTH_LIMIT = 2048
@@ -262,19 +262,19 @@ export class TextModelTokenization extends Disposable {
262262
}
263263

264264
this._isScheduled = true;
265-
runWhenIdle((deadline) => {
265+
setImmediate(() => {
266266
this._isScheduled = false;
267267

268268
if (this._isDisposed) {
269269
// disposed in the meantime
270270
return;
271271
}
272272

273-
this._revalidateTokensNow(deadline);
273+
this._revalidateTokensNow();
274274
});
275275
}
276276

277-
private _revalidateTokensNow(deadline: IdleDeadline): void {
277+
private _revalidateTokensNow(): void {
278278
const textModelLastLineNumber = this._textModel.getLineCount();
279279

280280
const MAX_ALLOWED_TIME = 1;
@@ -293,7 +293,7 @@ export class TextModelTokenization extends Disposable {
293293
if (tokenizedLineNumber >= textModelLastLineNumber) {
294294
break;
295295
}
296-
} while (this._hasLinesToTokenize() && deadline.timeRemaining() > 0);
296+
} while (this._hasLinesToTokenize());
297297

298298
this._beginBackgroundTokenization();
299299
this._textModel.setTokens(builder.tokens, !this._hasLinesToTokenize());

0 commit comments

Comments
 (0)