Skip to content

Commit

Permalink
Fix url not being updated properly
Browse files Browse the repository at this point in the history
Also fix a small issue with the preferences.

Contributed on behalf of STMicroelectronics
  • Loading branch information
sgraband committed Sep 13, 2023
1 parent ea0a4a0 commit b5b68e1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
19 changes: 15 additions & 4 deletions packages/base/src/lazy-tsp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,30 @@ export type LazyTspClient = {
};

export type LazyTspClientFactory = typeof LazyTspClientFactory;
export function LazyTspClientFactory(url: Promise<string>): ITspClient {
export function LazyTspClientFactory(provider: () => Promise<string>): 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export default new ContainerModule(bind => {
new JsonRpcConnectionHandler(TRACE_SERVER_CLIENT, () => {
const provider = ctx.container.get<TraceServerUrlProvider>(TraceServerUrlProvider);
const lazyTspClientFactory = ctx.container.get<LazyTspClientFactory>(LazyTspClientFactory);
const traceServerUrlPromise = provider.getTraceServerUrlPromise();
return lazyTspClientFactory(traceServerUrlPromise);
return lazyTspClientFactory(() => provider.getTraceServerUrlPromise());
})
)
.inSingletonScope();
Expand Down

0 comments on commit b5b68e1

Please sign in to comment.