Skip to content

Commit

Permalink
ovsx-client: introduce async provider
Browse files Browse the repository at this point in the history
The commit fixes an issue where previously the backend would never
properly read the cli arguments in time to create the ovsx-client. This
meant that while the api-version was respected when searching in the
view, it was not when installing.

The change removes the use of `OVSXAsyncClient` and instead uses an
`async` provider of `OVSXClient` which will probably be set with the
correct api version and url when ready.

Signed-off-by: vince-fugnitto <[email protected]>
  • Loading branch information
vince-fugnitto committed Oct 26, 2021
1 parent 2c63c96 commit 9502b0d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 69 deletions.
4 changes: 4 additions & 0 deletions dev-packages/ovsx-client/src/ovsx-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface OVSXClientOptions {
apiVersion: string
apiUrl: string
}

export const OVSXClientProvider = Symbol('OVSXClientProvider');
export type OVSXClientProvider = () => Promise<OVSXClient>;

export class OVSXClient {

constructor(readonly options: OVSXClientOptions) { }
Expand Down
39 changes: 0 additions & 39 deletions packages/vsx-registry/src/browser/ovsx-async-client.ts

This file was deleted.

18 changes: 10 additions & 8 deletions packages/vsx-registry/src/browser/vsx-extensions-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ import { WorkspaceService } from '@theia/workspace/lib/browser';
import { RecommendedExtensions } from './recommended-extensions/recommended-extensions-preference-contribution';
import URI from '@theia/core/lib/common/uri';
import { VSXResponseError, VSXSearchParam } from '@theia/ovsx-client/lib/ovsx-types';
import { OVSXAsyncClient } from './ovsx-async-client';
import { OVSXClientProvider } from '@theia/ovsx-client';

@injectable()
export class VSXExtensionsModel {

protected readonly onDidChangeEmitter = new Emitter<void>();
readonly onDidChange = this.onDidChangeEmitter.event;

@inject(OVSXAsyncClient)
protected client: OVSXAsyncClient;
@inject(OVSXClientProvider)
protected clientProvider: OVSXClientProvider;

@inject(HostedPluginSupport)
protected readonly pluginSupport: HostedPluginSupport;
Expand All @@ -63,7 +63,6 @@ export class VSXExtensionsModel {

@postConstruct()
protected async init(): Promise<void> {
await this.client.ready;
await Promise.all([
this.initInstalled(),
this.initSearchResult(),
Expand Down Expand Up @@ -167,14 +166,15 @@ export class VSXExtensionsModel {
}, 150);
protected doUpdateSearchResult(param: VSXSearchParam, token: CancellationToken): Promise<void> {
return this.doChange(async () => {
const result = await this.client.search(param);
const client = await this.clientProvider();
const result = await client.search(param);
if (token.isCancellationRequested) {
return;
}
const searchResult = new Set<string>();
for (const data of result.extensions) {
const id = data.namespace.toLowerCase() + '.' + data.name.toLowerCase();
const extension = this.client.getLatestCompatibleVersion(data);
const extension = client.getLatestCompatibleVersion(data);
if (!extension) {
continue;
}
Expand Down Expand Up @@ -260,7 +260,8 @@ export class VSXExtensionsModel {
}
if (extension.readmeUrl) {
try {
const rawReadme = await this.client.fetchText(extension.readmeUrl);
const client = await this.clientProvider();
const rawReadme = await client.fetchText(extension.readmeUrl);
const readme = this.compileReadme(rawReadme);
extension.update({ readme });
} catch (e) {
Expand Down Expand Up @@ -292,7 +293,8 @@ export class VSXExtensionsModel {
if (!this.shouldRefresh(extension)) {
return extension;
}
const data = await this.client.getLatestCompatibleExtensionVersion(id);
const client = await this.clientProvider();
const data = await client.getLatestCompatibleExtensionVersion(id);
if (!data) {
return;
}
Expand Down
17 changes: 7 additions & 10 deletions packages/vsx-registry/src/browser/vsx-registry-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ import { VSXEnvironment } from '../common/vsx-environment';
import { VSXExtensionsSearchModel } from './vsx-extensions-search-model';
import { bindExtensionPreferences } from './recommended-extensions/recommended-extensions-preference-contribution';
import { bindPreferenceProviderOverrides } from './recommended-extensions/preference-provider-overrides';
import { OVSXAsyncClient } from './ovsx-async-client';
import { OVSXClient, OVSXClientProvider } from '@theia/ovsx-client';

export default new ContainerModule((bind, unbind) => {
bind(VSXEnvironment).toSelf().inSingletonScope();
bind(OVSXAsyncClient).toDynamicValue(ctx => {
bind(OVSXClientProvider).toProvider<OVSXClient>(ctx => () => new Promise<OVSXClient>(async resolve => {
const vsxEnvironment = ctx.container.get(VSXEnvironment);
return new OVSXAsyncClient(Promise.all([
vsxEnvironment.getVscodeApiVersion(),
vsxEnvironment.getRegistryApiUri()
]).then(([apiVersion, apiUri]) => ({
apiVersion,
apiUrl: apiUri.toString()
})));
}).inSingletonScope();
const apiVersion = await vsxEnvironment.getVscodeApiVersion();
const apiUrl = (await vsxEnvironment.getRegistryApiUri()).toString();
const client: OVSXClient = new OVSXClient({ apiVersion, apiUrl });
resolve(client);
}));

bind(VSXExtension).toSelf();
bind(VSXExtensionFactory).toFactory(ctx => (option: VSXExtensionOptions) => {
Expand Down
9 changes: 5 additions & 4 deletions packages/vsx-registry/src/node/vsx-extension-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import { injectable, inject } from '@theia/core/shared/inversify';
import URI from '@theia/core/lib/common/uri';
import { PluginDeployerResolver, PluginDeployerResolverContext } from '@theia/plugin-ext/lib/common/plugin-protocol';
import { VSXExtensionUri } from '../common/vsx-extension-uri';
import { OVSXClient } from '@theia/ovsx-client/lib/ovsx-client';
import { OVSXClientProvider } from '@theia/ovsx-client';

@injectable()
export class VSXExtensionResolver implements PluginDeployerResolver {

@inject(OVSXClient)
protected client: OVSXClient;
@inject(OVSXClientProvider)
protected clientProvider: OVSXClientProvider;

protected readonly downloadPath: string;

Expand All @@ -49,7 +49,8 @@ export class VSXExtensionResolver implements PluginDeployerResolver {
return;
}
console.log(`[${id}]: trying to resolve latest version...`);
const extension = await this.client.getLatestCompatibleExtensionVersion(id);
const client = await this.clientProvider();
const extension = await client.getLatestCompatibleExtensionVersion(id);
if (!extension) {
return;
}
Expand Down
17 changes: 9 additions & 8 deletions packages/vsx-registry/src/node/vsx-registry-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@
import { ContainerModule } from '@theia/core/shared/inversify';
import { VSXExtensionResolver } from './vsx-extension-resolver';
import { PluginDeployerResolver } from '@theia/plugin-ext/lib/common/plugin-protocol';
import { VSCODE_DEFAULT_API_VERSION, VSX_REGISTRY_URL_DEFAULT } from '@theia/plugin-ext-vscode/lib/common/plugin-vscode-types';
import { OVSXClient } from '@theia/ovsx-client/lib/ovsx-client';
import { OVSXClientProvider } from '@theia/ovsx-client';
import { VSXEnvironment } from '../common/vsx-environment';

export default new ContainerModule(bind => {
bind(OVSXClient).toConstantValue(new OVSXClient({
apiVersion: process.env['VSCODE_API_VERSION'] || VSCODE_DEFAULT_API_VERSION,
apiUrl: resolveRegistryUrl()
bind(VSXEnvironment).toSelf().inSingletonScope();
bind(OVSXClientProvider).toProvider<OVSXClient>(ctx => () => new Promise<OVSXClient>(async resolve => {
const vsxEnvironment = ctx.container.get(VSXEnvironment);
const apiVersion = await vsxEnvironment.getVscodeApiVersion();
const apiUrl = (await vsxEnvironment.getRegistryApiUri()).toString();
const client: OVSXClient = new OVSXClient({ apiVersion, apiUrl });
resolve(client);
}));
bind(VSXExtensionResolver).toSelf().inSingletonScope();
bind(PluginDeployerResolver).toService(VSXExtensionResolver);
});

function resolveRegistryUrl(): string {
return process.env['VSX_REGISTRY_URL'] || VSX_REGISTRY_URL_DEFAULT;
}

0 comments on commit 9502b0d

Please sign in to comment.