Skip to content

Commit 380a24f

Browse files
Merge pull request #648 from gjsjohnmurray/fix-608
fix #608 update connections when settings are changed
2 parents c9d4f38 + 928dbfc commit 380a24f

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

src/api/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
panel,
1313
checkConnection,
1414
schemas,
15+
checkingConnection,
1516
} from "../extension";
1617
import { currentWorkspaceFolder, outputConsole } from "../utils";
1718

@@ -143,8 +144,8 @@ export class AtelierAPI {
143144
return cookies;
144145
}
145146

146-
public clearCookies(): void {
147-
this.cache.set("cookies", []);
147+
public async clearCookies(): Promise<void> {
148+
await this.cache.put("cookies", []);
148149
}
149150

150151
public xdebugUrl(): string {
@@ -153,7 +154,7 @@ export class AtelierAPI {
153154
return `${proto}://${host}:${port}${pathPrefix}/api/atelier/v${apiVersion}/%25SYS/debug`;
154155
}
155156

156-
public updateCookies(newCookies: string[]): Promise<any> {
157+
public async updateCookies(newCookies: string[]): Promise<void> {
157158
const cookies = this.cache.get("cookies", []);
158159
newCookies.forEach((cookie) => {
159160
const [cookieName] = cookie.split("=");
@@ -164,7 +165,7 @@ export class AtelierAPI {
164165
cookies.push(cookie);
165166
}
166167
});
167-
return this.cache.put("cookies", cookies);
168+
await this.cache.put("cookies", cookies);
168169
}
169170

170171
private setConnection(workspaceFolderName: string, namespace?: string): void {
@@ -322,7 +323,7 @@ export class AtelierAPI {
322323
});
323324
if (response.status === 401) {
324325
authRequestMap.delete(target);
325-
if (this.wsOrFile) {
326+
if (this.wsOrFile && !checkingConnection) {
326327
setTimeout(() => {
327328
checkConnection(true, typeof this.wsOrFile === "object" ? this.wsOrFile : undefined);
328329
}, 1000);
@@ -380,7 +381,12 @@ export class AtelierAPI {
380381
panel.tooltip = "Disconnected";
381382
workspaceState.update(this.configName + ":host", undefined);
382383
workspaceState.update(this.configName + ":port", undefined);
383-
setTimeout(checkConnection, 30000);
384+
if (!checkingConnection) {
385+
setTimeout(checkConnection, 30000);
386+
}
387+
} else if (error.code === "EPROTO") {
388+
// This can happen if https was configured but didn't work
389+
authRequestMap.delete(target);
384390
}
385391
throw error;
386392
}

src/extension.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ export function getXmlUri(uri: vscode.Uri): vscode.Uri {
163163
}
164164
let reporter: TelemetryReporter = null;
165165

166+
export let checkingConnection = false;
167+
166168
let serverManagerApi: any;
167169

168170
// Map of the intersystems.server connection specs we have resolved via the API to that extension
@@ -191,6 +193,11 @@ export function getResolvedConnectionSpec(key: string, dflt: any): any {
191193
}
192194

193195
export async function checkConnection(clearCookies = false, uri?: vscode.Uri): Promise<void> {
196+
// Do nothing if already checking the connection
197+
if (checkingConnection) {
198+
return;
199+
}
200+
194201
const { apiTarget, configName } = connectionTarget(uri);
195202
if (clearCookies) {
196203
/// clean-up cached values
@@ -268,7 +275,8 @@ export async function checkConnection(clearCookies = false, uri?: vscode.Uri): P
268275
disableConnection(configName);
269276
return;
270277
}
271-
api
278+
checkingConnection = true;
279+
return api
272280
.serverInfo()
273281
.then((info) => {
274282
panel.text = api.connInfo;
@@ -279,6 +287,7 @@ export async function checkConnection(clearCookies = false, uri?: vscode.Uri): P
279287
serverVersion: info.result.content.version,
280288
healthshare: hasHS ? "yes" : "no",
281289
});
290+
return;
282291
})
283292
.catch((error) => {
284293
let message = error.message;
@@ -324,10 +333,13 @@ export async function checkConnection(clearCookies = false, uri?: vscode.Uri): P
324333
throw error;
325334
})
326335
.finally(() => {
327-
explorerProvider.refresh();
328-
if (uri && schemas.includes(uri.scheme)) {
329-
vscode.commands.executeCommand("workbench.files.action.refreshFilesExplorer");
330-
}
336+
checkingConnection = false;
337+
setTimeout(() => {
338+
explorerProvider.refresh();
339+
if (uri && schemas.includes(uri.scheme)) {
340+
vscode.commands.executeCommand("workbench.files.action.refreshFilesExplorer");
341+
}
342+
}, 20);
331343
});
332344
}
333345

@@ -487,7 +499,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
487499
const uri = oneToCheck[1];
488500
const serverName = uri.scheme === "file" ? config("conn", configName).server : configName;
489501
await resolveConnectionSpec(serverName);
490-
await checkConnection(true, uri);
502+
// Ignore any failure
503+
checkConnection(true, uri).finally();
491504
}
492505

493506
vscode.workspace.onDidChangeWorkspaceFolders(async ({ added, removed }) => {
@@ -522,8 +535,34 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
522535

523536
vscode.workspace.onDidChangeConfiguration(async ({ affectsConfiguration }) => {
524537
if (affectsConfiguration("objectscript.conn") || affectsConfiguration("intersystems.servers")) {
525-
await checkConnection(true);
538+
if (affectsConfiguration("intersystems.servers")) {
539+
// Gather the server names previously resolved
540+
const resolvedServers: string[] = [];
541+
resolvedConnSpecs.forEach((v, k) => resolvedServers.push(k));
542+
// Clear the cache
543+
resolvedConnSpecs.clear();
544+
// Resolve them again, sequentially in case user needs to be prompted for credentials
545+
for await (const serverName of resolvedServers) {
546+
await resolveConnectionSpec(serverName);
547+
}
548+
}
549+
// Check connections sequentially for each workspace folder
550+
let refreshFilesExplorer = false;
551+
for await (const folder of vscode.workspace.workspaceFolders) {
552+
if (schemas.includes(folder.uri.scheme)) {
553+
refreshFilesExplorer = true;
554+
}
555+
try {
556+
await checkConnection(true, folder.uri);
557+
} catch (_) {
558+
continue;
559+
}
560+
}
526561
explorerProvider.refresh();
562+
if (refreshFilesExplorer) {
563+
// This unavoidably switches to the File Explorer view, so only do it if isfs folders were found
564+
vscode.commands.executeCommand("workbench.files.action.refreshFilesExplorer");
565+
}
527566
}
528567
});
529568
vscode.window.onDidCloseTerminal((t) => {
@@ -548,7 +587,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
548587
});
549588

550589
vscode.window.onDidChangeActiveTextEditor(async (textEditor: vscode.TextEditor) => {
551-
await checkConnection();
590+
await checkConnection(false, textEditor?.document.uri);
552591
posPanel.text = "";
553592
if (textEditor?.document.fileName.endsWith(".xml") && config("autoPreviewXML")) {
554593
return xml2doc(context, textEditor);
@@ -712,7 +751,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
712751
const workspaceFolder = currentWorkspaceFolder();
713752
if (workspaceFolder && workspaceFolder !== workspaceState.get<string>("workspaceFolder")) {
714753
workspaceState.update("workspaceFolder", workspaceFolder);
715-
await checkConnection();
754+
await checkConnection(false, editor?.document.uri);
716755
}
717756
}
718757
}),

0 commit comments

Comments
 (0)