提交 97640c99 编写于 作者: A Alex Dima

Remove usage of ctor cancel in TPromise (#56137)

上级 c4ea51ab
......@@ -14,7 +14,7 @@ import { LineTokens, IViewLineTokens } from 'vs/editor/common/core/lineTokens';
import * as strings from 'vs/base/common/strings';
import { IStandaloneThemeService } from 'vs/editor/standalone/common/standaloneThemeService';
import { ViewLineRenderingData } from 'vs/editor/common/viewModel/viewModel';
import { timeout } from 'vs/base/common/async';
import { TimeoutTimer } from 'vs/base/common/async';
export interface IColorizerOptions {
tabSize?: number;
......@@ -46,25 +46,6 @@ export class Colorizer {
return this.colorize(modeService, text, mimeType, options).then(render, (err) => console.error(err));
}
private static _tokenizationSupportChangedPromise(language: string): TPromise<void> {
let listener: IDisposable = null;
let stopListening = () => {
if (listener) {
listener.dispose();
listener = null;
}
};
return new TPromise<void>((c, e) => {
listener = TokenizationRegistry.onDidChange((e) => {
if (e.changedLanguages.indexOf(language) >= 0) {
stopListening();
c(void 0);
}
});
}, stopListening);
}
public static colorize(modeService: IModeService, text: string, mimeType: string, options: IColorizerOptions): TPromise<string> {
if (strings.startsWithUTF8BOM(text)) {
text = text.substr(1);
......@@ -85,13 +66,34 @@ export class Colorizer {
return TPromise.as(_colorize(lines, options.tabSize, tokenizationSupport));
}
// wait 500ms for mode to load, then give up
return TPromise.any([this._tokenizationSupportChangedPromise(language), timeout(500)]).then(_ => {
let tokenizationSupport = TokenizationRegistry.get(language);
if (tokenizationSupport) {
return _colorize(lines, options.tabSize, tokenizationSupport);
}
return _fakeColorize(lines, options.tabSize);
return new TPromise<string>((resolve, reject) => {
let listener: IDisposable = null;
let timeout: TimeoutTimer = null;
const execute = () => {
if (listener) {
listener.dispose();
listener = null;
}
if (timeout) {
timeout.dispose();
timeout = null;
}
const tokenizationSupport = TokenizationRegistry.get(language);
if (tokenizationSupport) {
return resolve(_colorize(lines, options.tabSize, tokenizationSupport));
}
return resolve(_fakeColorize(lines, options.tabSize));
};
// wait 500ms for mode to load, then give up
timeout = new TimeoutTimer();
timeout.cancelAndSet(execute, 500);
listener = TokenizationRegistry.onDidChange((e) => {
if (e.changedLanguages.indexOf(language) >= 0) {
execute();
}
});
});
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册