Skip to content

Commit

Permalink
Don't Use Async with TPromise in VSCode Codebase
Browse files Browse the repository at this point in the history
Fixes microsoft#30216

**Bug**
While compiling vscode with TS 2.4, there were around 10 errors reported about async functions that return a `TPromise`.

**Fix**
I'm checking with the TS team to see if these errors are expected or not. Until then, I believe we need to stop using `TPromise` with async functions. This change changes all instances that use `async` to instead use `then`.
  • Loading branch information
mjbvz committed Jul 6, 2017
1 parent 3858a18 commit ce02132
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 84 deletions.
52 changes: 27 additions & 25 deletions src/vs/platform/extensionManagement/node/extensionGalleryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,34 +355,36 @@ export class ExtensionGalleryService implements IExtensionGalleryService {
});
}

private async queryGallery(query: Query): TPromise<{ galleryExtensions: IRawGalleryExtension[], total: number; }> {
const commonHeaders = await this.commonHTTPHeaders;
const data = JSON.stringify(query.raw);
const headers = assign({}, commonHeaders, {
'Content-Type': 'application/json',
'Accept': 'application/json;api-version=3.0-preview.1',
'Accept-Encoding': 'gzip',
'Content-Length': data.length
});

const context = await this.requestService.request({
type: 'POST',
url: this.api('/extensionquery'),
data,
headers
});
private queryGallery(query: Query): TPromise<{ galleryExtensions: IRawGalleryExtension[], total: number; }> {
return this.commonHTTPHeaders.then(commonHeaders => {
const data = JSON.stringify(query.raw);
const headers = assign({}, commonHeaders, {
'Content-Type': 'application/json',
'Accept': 'application/json;api-version=3.0-preview.1',
'Accept-Encoding': 'gzip',
'Content-Length': data.length
});

if (context.res.statusCode >= 400 && context.res.statusCode < 500) {
return { galleryExtensions: [], total: 0 };
}
return this.requestService.request({
type: 'POST',
url: this.api('/extensionquery'),
data,
headers
});
}).then((context) => {
if (context.res.statusCode >= 400 && context.res.statusCode < 500) {
return TPromise.as({ galleryExtensions: [], total: 0 });
}

const result = await asJson<IRawGalleryQueryResult>(context);
const r = result.results[0];
const galleryExtensions = r.extensions;
const resultCount = r.resultMetadata && r.resultMetadata.filter(m => m.metadataType === 'ResultCount')[0];
const total = resultCount && resultCount.metadataItems.filter(i => i.name === 'TotalCount')[0].count || 0;
return asJson<IRawGalleryQueryResult>(context).then(result => {
const r = result.results[0];
const galleryExtensions = r.extensions;
const resultCount = r.resultMetadata && r.resultMetadata.filter(m => m.metadataType === 'ResultCount')[0];
const total = resultCount && resultCount.metadataItems.filter(i => i.name === 'TotalCount')[0].count || 0;

return { galleryExtensions, total };
return { galleryExtensions, total };
});
});
}

download(extension: IGalleryExtension): TPromise<string> {
Expand Down
5 changes: 2 additions & 3 deletions src/vs/workbench/electron-browser/extensionHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,8 @@ export class ExtensionHostProcessWorker {

// Unexpected termination
if (!this.isExtensionDevelopmentHost) {
const openDevTools = new Action('openDevTools', nls.localize('devTools', "Developer Tools"), '', true, async (): TPromise<boolean> => {
await this.windowService.openDevTools();
return false;
const openDevTools = new Action('openDevTools', nls.localize('devTools', "Developer Tools"), '', true, (): TPromise<boolean> => {
return this.windowService.openDevTools().then(() => false);
});

this.messageService.show(Severity.Error, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,23 @@ export class ExtensionsViewlet extends ComposedViewsViewlet implements IExtensio
.done(null, err => this.onError(err));
}

private async doSearch(): TPromise<any> {
private doSearch(): TPromise<any> {
const value = this.searchBox.value || '';
this.searchExtensionsContextKey.set(!!value);
this.searchInstalledExtensionsContextKey.set(InstalledExtensionsView.isInsalledExtensionsQuery(value));
this.searchRecommendedExtensionsContextKey.set(RecommendedExtensionsView.isRecommendedExtensionsQuery(value));

await this.updateViews(!!value);
return this.updateViews(!!value);
}

protected async updateViews(showAll?: boolean): TPromise<IView[]> {
const created = await super.updateViews();
const toShow = showAll ? this.views : created;
if (toShow.length) {
await this.progress(TPromise.join(toShow.map(view => (<ExtensionsListView>view).show(this.searchBox.value))));
}
return created;
protected updateViews(showAll?: boolean): TPromise<IView[]> {
return super.updateViews().then(created => {
const toShow = showAll ? this.views : created;
if (toShow.length) {
return this.progress(TPromise.join(toShow.map(view => (<ExtensionsListView>view).show(this.searchBox.value)))).then(() => created);
}
return created;
});
}

private count(): number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ export class ExtensionsListView extends CollapsibleView {
this.list.layout(size);
}

async show(query: string): TPromise<IPagedModel<IExtension>> {
const model = await this.query(query);
this.setModel(model);
return model;
show(query: string): TPromise<IPagedModel<IExtension>> {
return this.query(query).then(model => {
this.setModel(model);
return model;
});
}

select(): void {
Expand Down Expand Up @@ -150,7 +151,7 @@ export class ExtensionsListView extends CollapsibleView {
return this.list.length;
}

private async query(value: string): TPromise<IPagedModel<IExtension>> {
private query(value: string): TPromise<IPagedModel<IExtension>> {
const query = Query.parse(value);

let options: IQueryOptions = {
Expand All @@ -167,52 +168,55 @@ export class ExtensionsListView extends CollapsibleView {
// Show installed extensions
value = value ? value.replace(/@installed/g, '').replace(/@sort:(\w+)(-\w*)?/g, '').trim().toLowerCase() : '';

let result = await this.extensionsWorkbenchService.queryLocal();

switch (options.sortBy) {
case SortBy.InstallCount:
result = result.sort((e1, e2) => e2.installCount - e1.installCount);
break;
case SortBy.AverageRating:
result = result.sort((e1, e2) => e2.rating - e1.rating);
break;
default:
result = result.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName));
break;
}
return this.extensionsWorkbenchService.queryLocal().then(result => {

switch (options.sortBy) {
case SortBy.InstallCount:
result = result.sort((e1, e2) => e2.installCount - e1.installCount);
break;
case SortBy.AverageRating:
result = result.sort((e1, e2) => e2.rating - e1.rating);
break;
default:
result = result.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName));
break;
}

if (options.sortOrder === SortOrder.Descending) {
result = result.reverse();
}
if (options.sortOrder === SortOrder.Descending) {
result = result.reverse();
}

result = result
.filter(e => e.type === LocalExtensionType.User && e.name.toLowerCase().indexOf(value) > -1);
result = result
.filter(e => e.type === LocalExtensionType.User && e.name.toLowerCase().indexOf(value) > -1);

return new PagedModel(result);
return new PagedModel(result);
});
}

if (/@outdated/i.test(value)) {
value = value.replace(/@outdated/g, '').trim().toLowerCase();

const local = await this.extensionsWorkbenchService.queryLocal();
const result = local
.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName))
.filter(extension => extension.outdated && extension.name.toLowerCase().indexOf(value) > -1);
return this.extensionsWorkbenchService.queryLocal().then(local => {
const result = local
.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName))
.filter(extension => extension.outdated && extension.name.toLowerCase().indexOf(value) > -1);

return new PagedModel(result);
return new PagedModel(result);
});
}

if (/@disabled/i.test(value)) {
value = value.replace(/@disabled/g, '').trim().toLowerCase();

const local = await this.extensionsWorkbenchService.queryLocal();
const runningExtensions = await this.extensionService.getExtensions();

const result = local
.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName))
.filter(e => runningExtensions.every(r => !areSameExtensions(r, e)) && e.name.toLowerCase().indexOf(value) > -1);
return this.extensionsWorkbenchService.queryLocal().then(local => {
return this.extensionService.getExtensions().then(runningExtensions => {
const result = local
.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName))
.filter(e => runningExtensions.every(r => !areSameExtensions(r, e)) && e.name.toLowerCase().indexOf(value) > -1);

return new PagedModel(result);
return new PagedModel(result);
});
});
}

if (ExtensionsListView.isWorkspaceRecommendedExtensionsQuery(query.value)) {
Expand Down Expand Up @@ -259,10 +263,12 @@ export class ExtensionsListView extends CollapsibleView {

pagerPromises.push(this.extensionsWorkbenchService.queryGallery(options));

const pagers = await TPromise.join(pagerPromises);
const pager = pagers.length === 2 ? mergePagers(pagers[0], pagers[1]) : pagers[0];

return new PagedModel(pager);
return TPromise.join(pagerPromises).then(pagers => {
const pager = pagers.length === 2 ? mergePagers(pagers[0], pagers[1]) : pagers[0];

return new PagedModel(pager);
});
}

private getAllRecommendationsModel(query: Query, options: IQueryOptions): TPromise<IPagedModel<IExtension>> {
Expand Down Expand Up @@ -426,7 +432,7 @@ export class InstalledExtensionsView extends ExtensionsListView {
|| ExtensionsListView.isDisabledExtensionsQuery(query);
}

async show(query: string): TPromise<IPagedModel<IExtension>> {
show(query: string): TPromise<IPagedModel<IExtension>> {
if (InstalledExtensionsView.isInsalledExtensionsQuery(query)) {
return super.show(query);
}
Expand All @@ -444,7 +450,7 @@ export class RecommendedExtensionsView extends ExtensionsListView {
|| ExtensionsListView.isWorkspaceRecommendedExtensionsQuery(query);
}

async show(query: string): TPromise<IPagedModel<IExtension>> {
show(query: string): TPromise<IPagedModel<IExtension>> {
if (RecommendedExtensionsView.isRecommendedExtensionsQuery(query)) {
return super.show(query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ suite('Workbench - TextModelResolverService', () => {
let waitForIt = new TPromise(c => resolveModel = c);

const disposable = accessor.textModelResolverService.registerTextModelContentProvider('test', {
provideTextContent: async (resource: URI): TPromise<IModel> => {
await waitForIt;

let modelContent = 'Hello Test';
let mode = accessor.modeService.getOrCreateMode('json');
return accessor.modelService.createModel(modelContent, mode, resource);
provideTextContent: (resource: URI): TPromise<IModel> => {
return waitForIt.then(() => {
let modelContent = 'Hello Test';
let mode = accessor.modeService.getOrCreateMode('json');
return accessor.modelService.createModel(modelContent, mode, resource);
});
}
});

Expand Down

0 comments on commit ce02132

Please sign in to comment.