Skip to content

Commit 509913f

Browse files
committed
Poll for plugin files
1 parent 5bf44e1 commit 509913f

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

src/ui/ProjectPanelProvider.ts

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import contextKeys from "../contextKeys";
2323
import { Dependency, ResolvedDependency, Target } from "../SwiftPackage";
2424
import { FolderContext } from "../FolderContext";
2525
import { Version } from "../utilities/version";
26+
import { existsSync } from "fs";
2627

2728
const LOADING_ICON = "loading~spin";
2829
/**
@@ -449,6 +450,7 @@ export class ProjectPanelProvider implements vscode.TreeDataProvider<TreeNode> {
449450
private activeTasks: Set<string> = new Set();
450451
private lastComputedNodes: TreeNode[] = [];
451452
private buildPluginOutputWatcher?: vscode.FileSystemWatcher;
453+
private buildPluginFolderWatcher?: vscode.Disposable;
452454

453455
onDidChangeTreeData = this.didChangeTreeDataEmitter.event;
454456

@@ -545,13 +547,27 @@ export class ProjectPanelProvider implements vscode.TreeDataProvider<TreeNode> {
545547
if (this.buildPluginOutputWatcher) {
546548
this.buildPluginOutputWatcher.dispose();
547549
}
548-
this.buildPluginOutputWatcher = vscode.workspace.createFileSystemWatcher(
549-
new vscode.RelativePattern(folderContext.folder, ".build/plugins/outputs/{*,*/*}")
550-
);
550+
if (this.buildPluginFolderWatcher) {
551+
this.buildPluginFolderWatcher.dispose();
552+
}
553+
551554
const fire = () => this.didChangeTreeDataEmitter.fire();
552-
this.buildPluginOutputWatcher.onDidCreate(fire);
553-
this.buildPluginOutputWatcher.onDidDelete(fire);
554-
this.buildPluginOutputWatcher.onDidChange(fire);
555+
const buildPath = path.join(folderContext.folder.fsPath, ".build/plugins/outputs");
556+
this.buildPluginFolderWatcher = watchForFolder(
557+
buildPath,
558+
() => {
559+
this.buildPluginOutputWatcher = vscode.workspace.createFileSystemWatcher(
560+
new vscode.RelativePattern(buildPath, "{*,*/*}")
561+
);
562+
this.buildPluginOutputWatcher.onDidCreate(fire);
563+
this.buildPluginOutputWatcher.onDidDelete(fire);
564+
this.buildPluginOutputWatcher.onDidChange(fire);
565+
},
566+
() => {
567+
this.buildPluginOutputWatcher?.dispose();
568+
fire();
569+
}
570+
);
555571
}
556572

557573
getTreeItem(element: TreeNode): vscode.TreeItem {
@@ -570,7 +586,6 @@ export class ProjectPanelProvider implements vscode.TreeDataProvider<TreeNode> {
570586
...this.lastComputedNodes,
571587
];
572588
}
573-
574589
const nodes = await this.computeChildren(folderContext, element);
575590

576591
// If we're fetching the root nodes then save them in case we have an error later,
@@ -773,3 +788,35 @@ class TaskPoller implements vscode.Disposable {
773788
}
774789
}
775790
}
791+
792+
/**
793+
* Polls for the existence of a folder at the given path every 2.5 seconds.
794+
* Notifies via the provided callbacks when the folder becomes available or is deleted.
795+
*/
796+
function watchForFolder(
797+
folderPath: string,
798+
onAvailable: () => void,
799+
onDeleted: () => void
800+
): vscode.Disposable {
801+
const POLL_INTERVAL = 2500;
802+
let folderExists = existsSync(folderPath);
803+
804+
if (folderExists) {
805+
onAvailable();
806+
}
807+
808+
const interval = setInterval(() => {
809+
const nowExists = existsSync(folderPath);
810+
if (nowExists && !folderExists) {
811+
folderExists = true;
812+
onAvailable();
813+
} else if (!nowExists && folderExists) {
814+
folderExists = false;
815+
onDeleted();
816+
}
817+
}, POLL_INTERVAL);
818+
819+
return {
820+
dispose: () => clearInterval(interval),
821+
};
822+
}

0 commit comments

Comments
 (0)