@setoelkahfi / svara / commits / a191287

fix: loading bar doesnt pop up on every file change

mellbacon committed Aug 23, 2023 at 15:04 UTC a19128794b3a8c2a63275083456058f4cdb82ab5
1 file changed +35 -25
src/lib/File.ts
+35 -25
@@ -32,8 +32,10 @@ export async function openFolder() {
32 // load file watcher
33 await watchImmediate(
34 directory,
35 - () => {
36 - updateTree(directory);
35 + (e) => {
36 + const {type} = e;
37 + const updateType = Object.entries(type)[0][0];
38 + updateTree(directory, updateType);
39 },
40 { recursive: true }
41 )
@@ -44,13 +46,18 @@ export async function openFolder() {
46 export const treeLoading = writable(false);
47 let progressTimeout = null;
48 let loadInterval = null;
47 -async function updateTree(directory) {
49 +async function updateTree(directory, updateType = "") {
50 let loadTime = 0;
51
52 clearTimeout(progressTimeout);
53 clearInterval(loadInterval);
52 - treeLoading.set(true);
53 - loadInterval = setInterval(() => {loadTime++}, 1000)
54 +
55 + // dont show directory loading bar for simple file changes
56 + if (updateType !== "modify") {
57 + treeLoading.set(true);
58 + loadInterval = setInterval(() => {loadTime++}, 1000)
59 + }
60 +
61 let tree;
62 try {
63 tree = await fs.readDir(directory, {recursive: true});
@@ -60,38 +67,31 @@ async function updateTree(directory) {
67 if (!tree || tree === undefined) {
68 console.error("Cannot load directory");
69
63 - clearInterval(loadInterval);
64 -
65 - dirLoadFail.set(true);
66 - dirToLoad.set("Cannot load directory");
67 -
68 - progressTimeout = setTimeout(() => {
69 - treeLoading.set(false);
70 - }, 5000)
70 + cancelDirectoryLoad("Cannot load directory");
71 return null;
72 }
73 +
74 if (loadTime > 100) {
75 console.error(`Directory load time was too long. Aborting...`);
76 console.warn(`Cancelled load after ${loadTime} seconds`);
76 - clearInterval(loadInterval);
77 -
78 - dirLoadFail.set(true);
79 - dirToLoad.set("Error: Directory load timeout.");
77
81 - progressTimeout = setTimeout(() => {
82 - treeLoading.set(false);
83 - }, 5000)
78 + cancelDirectoryLoad("Error: Directory load timeout.");
79 return null;
80 }
81 +
82 let directoryName = get(dirToLoad);
83 tree = [{id: -1, name: directoryName, path: directory, children: buildTree(sortTree(tree))}];
84 filetree.set(tree);
85 clearInterval(loadInterval);
90 - if (loadTime < 45) {
91 - console.log(`Directory load time: ${loadTime < 1 ? "less than 1" : loadTime}s`);
92 - }
93 - else {
94 - console.warn(`Directory load time: ${loadTime < 1 ? "less than 1" : loadTime}s`);
86 +
87 + //TODO: move this to a log file
88 + if (updateType !== "modify") {
89 + if (loadTime < 45) {
90 + console.log(`Directory load time: ${loadTime < 1 ? "less than 1" : loadTime}s`);
91 + }
92 + else {
93 + console.warn(`Directory load time: ${loadTime < 1 ? "less than 1" : loadTime}s`);
94 + }
95 }
96 id = 0;
97 treeLoading.set(false);
@@ -130,6 +130,16 @@ function sortTree(tree: fs.FileEntry[]) {
130 return sortedTree;
131 }
132
133 +export function cancelDirectoryLoad(msg: string) {
134 + clearInterval(loadInterval);
135 +
136 + dirLoadFail.set(true);
137 + dirToLoad.set(msg);
138 + progressTimeout = setTimeout(() => {
139 + treeLoading.set(false);
140 + }, 5000)
141 +}
142 +
143 export async function moveFile(source: string, dest: string, file: string) {
144 const filename = file.split(path.sep).pop();
145 if (!await dialog.confirm(`Are you sure you want to move "${filename}" from "./${source.split(path.sep).pop()}" into "./${dest.split(path.sep).pop()}?"`, {title: "Nucleus: Move File"})) {