Skip to content

Commit 3c5beb9

Browse files
committed
debt - avoid winjs.promise, add cancelation token
1 parent 8a0e53a commit 3c5beb9

4 files changed

Lines changed: 17 additions & 15 deletions

File tree

src/vs/editor/contrib/colorPicker/color.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,30 @@ import { registerLanguageCommand } from 'vs/editor/browser/editorExtensions';
1313
import { Range, IRange } from 'vs/editor/common/core/range';
1414
import { illegalArgument } from 'vs/base/common/errors';
1515
import { IModelService } from 'vs/editor/common/services/modelService';
16+
import { CancellationToken } from 'vs/base/common/cancellation';
1617

1718

1819
export interface IColorData {
1920
colorInfo: IColorInformation;
2021
provider: DocumentColorProvider;
2122
}
2223

23-
export function getColors(model: ITextModel): TPromise<IColorData[]> {
24+
export function getColors(model: ITextModel, token: CancellationToken): Promise<IColorData[]> {
2425
const colors: IColorData[] = [];
2526
const providers = ColorProviderRegistry.ordered(model).reverse();
26-
const promises = providers.map(provider => asWinJsPromise(token => provider.provideDocumentColors(model, token)).then(result => {
27+
const promises = providers.map(provider => Promise.resolve(provider.provideDocumentColors(model, token)).then(result => {
2728
if (Array.isArray(result)) {
2829
for (let colorInfo of result) {
2930
colors.push({ colorInfo, provider });
3031
}
3132
}
3233
}));
3334

34-
return TPromise.join(promises).then(() => colors);
35+
return Promise.all(promises).then(() => colors);
3536
}
3637

37-
export function getColorPresentations(model: ITextModel, colorInfo: IColorInformation, provider: DocumentColorProvider): TPromise<IColorPresentation[]> {
38-
return asWinJsPromise(token => provider.provideColorPresentations(model, colorInfo, token));
38+
export function getColorPresentations(model: ITextModel, colorInfo: IColorInformation, provider: DocumentColorProvider, token: CancellationToken): Promise<IColorPresentation[]> {
39+
return Promise.resolve(provider.provideColorPresentations(model, colorInfo, token));
3940
}
4041

4142
registerLanguageCommand('_executeDocumentColorProvider', function (accessor, args) {

src/vs/editor/contrib/colorPicker/colorDetector.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import { RGBA } from 'vs/base/common/color';
77
import { hash } from 'vs/base/common/hash';
88
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
9-
import { TPromise } from 'vs/base/common/winjs.base';
109
import { IEditorContribution } from 'vs/editor/common/editorCommon';
1110
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
1211
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
@@ -17,7 +16,7 @@ import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService
1716
import { getColors, IColorData } from 'vs/editor/contrib/colorPicker/color';
1817
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
1918
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
20-
import { TimeoutTimer } from 'vs/base/common/async';
19+
import { TimeoutTimer, CancelablePromise, createCancelablePromise } from 'vs/base/common/async';
2120

2221
const MAX_DECORATORS = 500;
2322

@@ -29,7 +28,7 @@ export class ColorDetector implements IEditorContribution {
2928

3029
private _globalToDispose: IDisposable[] = [];
3130
private _localToDispose: IDisposable[] = [];
32-
private _computePromise: TPromise<void>;
31+
private _computePromise: CancelablePromise<void>;
3332
private _timeoutTimer: TimeoutTimer;
3433

3534
private _decorationsIds: string[] = [];
@@ -128,10 +127,12 @@ export class ColorDetector implements IEditorContribution {
128127
}
129128

130129
private beginCompute(): void {
131-
this._computePromise = getColors(this._editor.getModel()).then(colorInfos => {
132-
this.updateDecorations(colorInfos);
133-
this.updateColorDecorators(colorInfos);
134-
this._computePromise = null;
130+
this._computePromise = createCancelablePromise(token => {
131+
return getColors(this._editor.getModel(), token).then(colorInfos => {
132+
this.updateDecorations(colorInfos);
133+
this.updateColorDecorators(colorInfos);
134+
this._computePromise = null;
135+
});
135136
});
136137
}
137138

src/vs/editor/contrib/hover/modesContentHover.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ export class ModesContentHoverWidget extends ContentHoverWidget {
339339
const model = new ColorPickerModel(color, [], 0);
340340
const widget = new ColorPickerWidget(fragment, model, this._editor.getConfiguration().pixelRatio, this._themeService);
341341

342-
getColorPresentations(editorModel, colorInfo, msg.provider).then(colorPresentations => {
342+
getColorPresentations(editorModel, colorInfo, msg.provider, CancellationToken.None).then(colorPresentations => {
343343
model.colorPresentations = colorPresentations;
344344
const originalText = this._editor.getModel().getValueInRange(msg.range);
345345
model.guessColorPresentation(color, originalText);
@@ -381,7 +381,7 @@ export class ModesContentHoverWidget extends ContentHoverWidget {
381381
blue: color.rgba.b / 255,
382382
alpha: color.rgba.a
383383
}
384-
}, msg.provider).then((colorPresentations) => {
384+
}, msg.provider, CancellationToken.None).then((colorPresentations) => {
385385
model.colorPresentations = colorPresentations;
386386
});
387387
};

src/vs/workbench/test/electron-browser/api/extHostLanguageFeatures.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ suite('ExtHostLanguageFeatures', function () {
11991199
}));
12001200

12011201
return rpcProtocol.sync().then(() => {
1202-
return getColors(model).then(value => {
1202+
return getColors(model, CancellationToken.None).then(value => {
12031203
assert.equal(value.length, 1);
12041204
let [first] = value;
12051205

0 commit comments

Comments
 (0)