Skip to content

Commit 3fa7dcb

Browse files
authored
Allow passing in a promise that resolves with the connection token (microsoft#158586)
1 parent 1e6ac12 commit 3fa7dcb

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

src/vs/platform/remote/browser/remoteAuthorityResolverService.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export class RemoteAuthorityResolverService extends Disposable implements IRemot
1818
private readonly _onDidChangeConnectionData = this._register(new Emitter<void>());
1919
public readonly onDidChangeConnectionData = this._onDidChangeConnectionData.event;
2020

21-
private readonly _cache: Map<string, ResolverResult>;
22-
private readonly _connectionToken: string | undefined;
21+
private readonly _promiseCache = new Map<string, Promise<ResolverResult>>();
22+
private readonly _cache = new Map<string, ResolverResult>();
23+
private readonly _connectionToken: Promise<string> | string | undefined;
2324
private readonly _connectionTokens: Map<string, string>;
2425

25-
constructor(@IProductService productService: IProductService, connectionToken: string | undefined, resourceUriProvider: ((uri: URI) => URI) | undefined) {
26+
constructor(@IProductService productService: IProductService, connectionToken: Promise<string> | string | undefined, resourceUriProvider: ((uri: URI) => URI) | undefined) {
2627
super();
27-
this._cache = new Map<string, ResolverResult>();
2828
this._connectionToken = connectionToken;
2929
this._connectionTokens = new Map<string, string>();
3030
if (resourceUriProvider) {
@@ -34,13 +34,12 @@ export class RemoteAuthorityResolverService extends Disposable implements IRemot
3434
}
3535

3636
async resolveAuthority(authority: string): Promise<ResolverResult> {
37-
if (!this._cache.has(authority)) {
38-
const result = this._doResolveAuthority(authority);
39-
RemoteAuthorities.set(authority, result.authority.host, result.authority.port);
40-
this._cache.set(authority, result);
41-
this._onDidChangeConnectionData.fire();
37+
let result = this._promiseCache.get(authority);
38+
if (!result) {
39+
result = this._doResolveAuthority(authority);
40+
this._promiseCache.set(authority, result);
4241
}
43-
return this._cache.get(authority)!;
42+
return result;
4443
}
4544

4645
async getCanonicalURI(uri: URI): Promise<URI> {
@@ -52,19 +51,23 @@ export class RemoteAuthorityResolverService extends Disposable implements IRemot
5251
return null;
5352
}
5453
const resolverResult = this._cache.get(authority)!;
55-
const connectionToken = this._connectionTokens.get(authority) || this._connectionToken;
54+
const connectionToken = this._connectionTokens.get(authority) || resolverResult.authority.connectionToken;
5655
return {
5756
host: resolverResult.authority.host,
5857
port: resolverResult.authority.port,
5958
connectionToken: connectionToken
6059
};
6160
}
6261

63-
private _doResolveAuthority(authority: string): ResolverResult {
64-
const connectionToken = this._connectionTokens.get(authority) || this._connectionToken;
62+
private async _doResolveAuthority(authority: string): Promise<ResolverResult> {
63+
const connectionToken = await Promise.resolve(this._connectionTokens.get(authority) || this._connectionToken);
6564
const defaultPort = (/^https:/.test(window.location.href) ? 443 : 80);
6665
const { host, port } = parseAuthorityWithOptionalPort(authority, defaultPort);
67-
return { authority: { authority, host: host, port: port, connectionToken } };
66+
const result: ResolverResult = { authority: { authority, host: host, port: port, connectionToken } };
67+
RemoteAuthorities.set(authority, result.authority.host, result.authority.port);
68+
this._cache.set(authority, result);
69+
this._onDidChangeConnectionData.fire();
70+
return result;
6871
}
6972

7073
_clearResolvedAuthority(authority: string): void {

src/vs/platform/sign/browser/signService.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ export class SignService implements ISignService {
99

1010
declare readonly _serviceBrand: undefined;
1111

12-
private readonly _tkn: string | null;
13-
14-
constructor(token: string | undefined) {
15-
this._tkn = token || null;
16-
}
12+
constructor(
13+
private readonly _token: Promise<string> | string | undefined
14+
) { }
1715

1816
async createNewMessage(value: string): Promise<IMessage> {
1917
return { id: '', data: value };
@@ -22,6 +20,7 @@ export class SignService implements ISignService {
2220
return true;
2321
}
2422
async sign(value: string): Promise<string> {
25-
return this._tkn || '';
23+
const token = await Promise.resolve(this._token);
24+
return token || '';
2625
}
2726
}

src/vs/workbench/browser/web.api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export interface IWorkbenchConstructionOptions {
137137
/**
138138
* The connection token to send to the server.
139139
*/
140-
readonly connectionToken?: string;
140+
readonly connectionToken?: string | Promise<string>;
141141

142142
/**
143143
* An endpoint to serve iframe content ("webview") from. This is required

0 commit comments

Comments
 (0)