From b5b68e110c286dd6419e0a3296299c0188645e84 Mon Sep 17 00:00:00 2001 From: Simon Graband Date: Wed, 13 Sep 2023 15:22:09 +0200 Subject: [PATCH] Fix url not being updated properly Also fix a small issue with the preferences. Contributed on behalf of STMicroelectronics --- packages/base/src/lazy-tsp-client.ts | 19 +++++++++++++++---- .../preferences-frontend-contribution.ts | 8 +++++--- .../node/viewer-prototype-backend-module.ts | 3 +-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/base/src/lazy-tsp-client.ts b/packages/base/src/lazy-tsp-client.ts index 78927d0b3..3430a2815 100644 --- a/packages/base/src/lazy-tsp-client.ts +++ b/packages/base/src/lazy-tsp-client.ts @@ -15,19 +15,30 @@ export type LazyTspClient = { }; export type LazyTspClientFactory = typeof LazyTspClientFactory; -export function LazyTspClientFactory(url: Promise): ITspClient { +export function LazyTspClientFactory(provider: () => Promise): ITspClient { // All methods from the `HttpTspClient` are asynchronous. The `LazyTspClient` // will just delay each call to its methods by first awaiting for the // asynchronous `baseUrl` resolution to then get a valid `HttpTspClient`. - const tspClientPromise = url.then(baseUrl => new HttpTspClient(baseUrl)); + + // Save the current HttpTspClient and the URL used for it. + let tspClient: HttpTspClient; + let lastUrl: string; // eslint-disable-next-line no-null/no-null return new Proxy(Object.create(null), { get(target, property, _receiver) { let method = target[property]; if (!method) { target[property] = method = async (...args: any[]) => { - const tspClient = (await tspClientPromise) as any; - return tspClient[property](...args); + tspClient = await provider().then(baseUrl => { + // If the url has not been updated keep the same client. + if (lastUrl === baseUrl) { + return tspClient; + } + // If the url has changed save it and create a new client. + lastUrl = baseUrl; + return new HttpTspClient(baseUrl); + }); + return (tspClient as any)[property](...args); }; } return method; diff --git a/theia-extensions/viewer-prototype/src/browser/preferences-frontend-contribution.ts b/theia-extensions/viewer-prototype/src/browser/preferences-frontend-contribution.ts index d871dc41e..df10de5b8 100644 --- a/theia-extensions/viewer-prototype/src/browser/preferences-frontend-contribution.ts +++ b/theia-extensions/viewer-prototype/src/browser/preferences-frontend-contribution.ts @@ -14,9 +14,11 @@ export class PreferencesFrontendContribution implements FrontendApplicationContr this.tracePreferences.ready.then(() => { this.portPreferenceProxy.onPortPreferenceChanged(this.tracePreferences[TRACE_PORT]); this.tracePreferences.onPreferenceChanged(async event => { - const newValue = typeof event.newValue === 'string' ? parseInt(event.newValue) : event.newValue; - const oldValue = typeof event.oldValue === 'string' ? parseInt(event.oldValue) : event.oldValue; - this.portPreferenceProxy.onPortPreferenceChanged(newValue, oldValue, true); + if (event.preferenceName === TRACE_PORT) { + const newValue = typeof event.newValue === 'string' ? parseInt(event.newValue) : event.newValue; + const oldValue = typeof event.oldValue === 'string' ? parseInt(event.oldValue) : event.oldValue; + this.portPreferenceProxy.onPortPreferenceChanged(newValue, oldValue, true); + } }); }); } diff --git a/theia-extensions/viewer-prototype/src/node/viewer-prototype-backend-module.ts b/theia-extensions/viewer-prototype/src/node/viewer-prototype-backend-module.ts index 366468a13..1f98fe81e 100644 --- a/theia-extensions/viewer-prototype/src/node/viewer-prototype-backend-module.ts +++ b/theia-extensions/viewer-prototype/src/node/viewer-prototype-backend-module.ts @@ -45,8 +45,7 @@ export default new ContainerModule(bind => { new JsonRpcConnectionHandler(TRACE_SERVER_CLIENT, () => { const provider = ctx.container.get(TraceServerUrlProvider); const lazyTspClientFactory = ctx.container.get(LazyTspClientFactory); - const traceServerUrlPromise = provider.getTraceServerUrlPromise(); - return lazyTspClientFactory(traceServerUrlPromise); + return lazyTspClientFactory(() => provider.getTraceServerUrlPromise()); }) ) .inSingletonScope();