Skip to content

Commit 579e714

Browse files
committed
Poll for plugin files
1 parent c9cce4f commit 579e714

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
@@ -25,6 +25,7 @@ import { FolderContext } from "../FolderContext";
2525
import { getPlatformConfig, resolveTaskCwd } from "../utilities/tasks";
2626
import { SwiftTask, TaskPlatformSpecificConfig } from "../tasks/SwiftTaskProvider";
2727
import { Version } from "../utilities/version";
28+
import { existsSync } from "fs";
2829

2930
const LOADING_ICON = "loading~spin";
3031
/**
@@ -469,6 +470,7 @@ export class ProjectPanelProvider implements vscode.TreeDataProvider<TreeNode> {
469470
private activeTasks: Set<string> = new Set();
470471
private lastComputedNodes: TreeNode[] = [];
471472
private buildPluginOutputWatcher?: vscode.FileSystemWatcher;
473+
private buildPluginFolderWatcher?: vscode.Disposable;
472474

473475
onDidChangeTreeData = this.didChangeTreeDataEmitter.event;
474476

@@ -565,13 +567,27 @@ export class ProjectPanelProvider implements vscode.TreeDataProvider<TreeNode> {
565567
if (this.buildPluginOutputWatcher) {
566568
this.buildPluginOutputWatcher.dispose();
567569
}
568-
this.buildPluginOutputWatcher = vscode.workspace.createFileSystemWatcher(
569-
new vscode.RelativePattern(folderContext.folder, ".build/plugins/outputs/{*,*/*}")
570-
);
570+
if (this.buildPluginFolderWatcher) {
571+
this.buildPluginFolderWatcher.dispose();
572+
}
573+
571574
const fire = () => this.didChangeTreeDataEmitter.fire();
572-
this.buildPluginOutputWatcher.onDidCreate(fire);
573-
this.buildPluginOutputWatcher.onDidDelete(fire);
574-
this.buildPluginOutputWatcher.onDidChange(fire);
575+
const buildPath = path.join(folderContext.folder.fsPath, ".build/plugins/outputs");
576+
this.buildPluginFolderWatcher = watchForFolder(
577+
buildPath,
578+
() => {
579+
this.buildPluginOutputWatcher = vscode.workspace.createFileSystemWatcher(
580+
new vscode.RelativePattern(buildPath, "{*,*/*}")
581+
);
582+
this.buildPluginOutputWatcher.onDidCreate(fire);
583+
this.buildPluginOutputWatcher.onDidDelete(fire);
584+
this.buildPluginOutputWatcher.onDidChange(fire);
585+
},
586+
() => {
587+
this.buildPluginOutputWatcher?.dispose();
588+
fire();
589+
}
590+
);
575591
}
576592

577593
getTreeItem(element: TreeNode): vscode.TreeItem {
@@ -590,7 +606,6 @@ export class ProjectPanelProvider implements vscode.TreeDataProvider<TreeNode> {
590606
...this.lastComputedNodes,
591607
];
592608
}
593-
594609
const nodes = await this.computeChildren(folderContext, element);
595610

596611
// If we're fetching the root nodes then save them in case we have an error later,
@@ -794,3 +809,35 @@ class TaskPoller implements vscode.Disposable {
794809
}
795810
}
796811
}
812+
813+
/**
814+
* Polls for the existence of a folder at the given path every 2.5 seconds.
815+
* Notifies via the provided callbacks when the folder becomes available or is deleted.
816+
*/
817+
function watchForFolder(
818+
folderPath: string,
819+
onAvailable: () => void,
820+
onDeleted: () => void
821+
): vscode.Disposable {
822+
const POLL_INTERVAL = 2500;
823+
let folderExists = existsSync(folderPath);
824+
825+
if (folderExists) {
826+
onAvailable();
827+
}
828+
829+
const interval = setInterval(() => {
830+
const nowExists = existsSync(folderPath);
831+
if (nowExists && !folderExists) {
832+
folderExists = true;
833+
onAvailable();
834+
} else if (!nowExists && folderExists) {
835+
folderExists = false;
836+
onDeleted();
837+
}
838+
}, POLL_INTERVAL);
839+
840+
return {
841+
dispose: () => clearInterval(interval),
842+
};
843+
}

0 commit comments

Comments
 (0)