提交 48bef528 编写于 作者: J Joao Moreno

improve query interface

上级 bb273906
......@@ -304,3 +304,8 @@ export function safeStringify(obj: any): string {
return value;
});
}
export function getOrDefault<T,R>(obj: T, fn: (obj: T) => R, defaultValue: R = null): R {
const result = fn(obj);
return typeof result === 'undefined' ? defaultValue : result;
}
\ No newline at end of file
......@@ -45,10 +45,21 @@ export interface IExtension extends IExtensionManifest {
export const IExtensionsService = createDecorator<IExtensionsService>('extensionsService');
export const IGalleryService = createDecorator<IGalleryService>('galleryService');
export interface IQueryOptions {
text?: string;
pageNumber?: number;
pageSize?: number;
}
export interface IQueryResult {
extensions: IExtension[];
total: number;
}
export interface IGalleryService {
serviceId: ServiceIdentifier<any>;
isEnabled(): boolean;
query(text?: string): TPromise<{ extensions: IExtension[]; total: number; }>;
query(options?: IQueryOptions): TPromise<IQueryResult>;
}
export interface IExtensionsService {
......
......@@ -4,9 +4,9 @@
*--------------------------------------------------------------------------------------------*/
import { TPromise } from 'vs/base/common/winjs.base';
import { IGalleryService, IExtension, IGalleryVersion } from 'vs/workbench/parts/extensions/common/extensions';
import { IGalleryService, IGalleryVersion, IQueryOptions, IQueryResult } from 'vs/workbench/parts/extensions/common/extensions';
import { IXHRResponse } from 'vs/base/common/http';
import { assign } from 'vs/base/common/objects';
import { assign, getOrDefault } from 'vs/base/common/objects';
import { IRequestService } from 'vs/platform/request/common/request';
import { IWorkspaceContextService } from 'vs/workbench/services/workspace/common/contextService';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
......@@ -47,8 +47,6 @@ function getInstallCount(statistics: IGalleryExtensionStatistics[]): number {
return result ? result.value : 0;
}
// const FIVE_MINUTES = 1000 * 60 * 5;
export class GalleryService implements IGalleryService {
serviceId = IGalleryService;
......@@ -74,12 +72,12 @@ export class GalleryService implements IGalleryService {
return !!this.extensionsGalleryUrl;
}
query(text: string = ''): TPromise<{ extensions: IExtension[]; total: number; }> {
query(options: IQueryOptions = {}): TPromise<IQueryResult> {
if (!this.isEnabled()) {
return TPromise.wrapError(new Error('No extension gallery service configured.'));
}
return this.queryGallery(text)
return this.queryGallery(options)
.then(r => JSON.parse(r.responseText).results[0])
.then<{ galleryExtensions: IGalleryExtension[]; total: number; }>(r => {
const galleryExtensions = r.extensions;
......@@ -122,14 +120,19 @@ export class GalleryService implements IGalleryService {
});
}
private queryGallery(text: string): TPromise<IXHRResponse> {
private queryGallery(options: IQueryOptions): TPromise<IXHRResponse> {
const text = getOrDefault(options, o => o.text, '');
const pageNumber = getOrDefault(options, o => o.pageNumber, 1);
const pageSize = getOrDefault(options, o => o.pageNumber, 10);
const data = JSON.stringify({
filters: [{
criteria:[
{ filterType: 8, value: 'Microsoft.VisualStudio.Code' },
{ filterType: 10, value: text }
],
pageSize: 10
pageNumber,
pageSize
}],
flags: 0x1 | 0x4 | 0x80 | 0x100
});
......
......@@ -437,9 +437,9 @@ export class GalleryExtensionsHandler extends QuickOpenHandler {
return nls.localize('galleryExtensionsHandlerAriaLabel', "Type to narrow down the list of extensions from the gallery");
}
getResults(input: string): TPromise<IModel<IExtensionEntry>> {
return this.delayer.trigger(() => TPromise.join<any>([this.galleryService.query(input), this.extensionsService.getInstalled()]))
.then(result => this.instantiationService.createInstance(GalleryExtensionsModel, input, result[0].extensions, result[1]));
getResults(text: string): TPromise<IModel<IExtensionEntry>> {
return this.delayer.trigger(() => TPromise.join<any>([this.galleryService.query({ text }), this.extensionsService.getInstalled()]))
.then(result => this.instantiationService.createInstance(GalleryExtensionsModel, text, result[0].extensions, result[1]));
}
getEmptyLabel(input: string): string {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册