coarse check / fine check
This commit is contained in:
		
							parent
							
								
									e892449c63
								
							
						
					
					
						commit
						904f7944b2
					
				
							
								
								
									
										3
									
								
								transpiler/_test.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								transpiler/_test.html
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| @ -3,59 +3,75 @@ | ||||
| <body> | ||||
| <button id="open">open</button> | ||||
| <script type="module"> | ||||
| // Function to walk through the directory and check files | ||||
| async function checkFiles(dirHandle, extension, date, path="", /** @type {Map<string, File>}*/map, /** @type {Map<string, File>}*/deleteList, /** @type {Array<{fullPath:string, file:File>}*/focusList) { | ||||
|     for await (const entry of dirHandle.values()) | ||||
|     { | ||||
|         if (entry.kind === 'file' && entry.name.endsWith(extension)) | ||||
|         { | ||||
| // Function to walk through the directory and update file handles | ||||
| async function updateFileHandles(dirHandle, extension, path = "", map) { | ||||
|     for await (const entry of dirHandle.values()) { | ||||
|         if (entry.kind === 'file' && entry.name.endsWith(extension)) { | ||||
|             const file = await entry.getFile(); | ||||
|             const fullPath = path + entry.name; | ||||
|             map.set(fullPath, file); | ||||
|             deleteList.delete(fullPath); | ||||
|             if (file.lastModified > date) | ||||
|             { | ||||
|                 focusList.push({file, fullPath}); | ||||
|             if (!map.has(dirHandle)) { | ||||
|                 map.set(dirHandle, new Set()); | ||||
|             } | ||||
|         } else if (entry.kind === 'directory') | ||||
|         { | ||||
|             await checkFiles(entry, extension, date, path+entry.name, map, deleteList, focusList); // Recursively check subdirectories | ||||
|             map.get(dirHandle).add(fullPath); | ||||
|         } else if (entry.kind === 'directory') { | ||||
|             await updateFileHandles(entry, extension, path + entry.name + "/", map); // Recursively check subdirectories | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // Main function to set up the interval polling task | ||||
| // Function to check modified dates of known file handles | ||||
| async function checkModifiedDates(map, date) { | ||||
|     const focusFiles = []; | ||||
|     for (const [dirHandle, filenames] of map.entries()) { | ||||
|         for (const filename of filenames) { | ||||
|             try | ||||
|             { | ||||
|                 const fileHandle = await dirHandle.getFileHandle(filename.split('/').pop()); | ||||
|                 const file = await fileHandle.getFile(); | ||||
|                 if (file.lastModified > date.getTime()) { | ||||
|                     focusFiles.push({ file, fullPath: filename }); | ||||
|                 } | ||||
|             } | ||||
|             catch(e) | ||||
|             { | ||||
|                 console.log("file deleted?", filename); | ||||
|                 filenames.delete(filename); | ||||
|             } | ||||
| 
 | ||||
|         } | ||||
|     } | ||||
|     return focusFiles; | ||||
| } | ||||
| 
 | ||||
| // Main function to set up the intervals | ||||
| async function main() { | ||||
|     const dirHandle = await window.showDirectoryPicker(); | ||||
|     const extension = '.html'; // Change this to the desired file extension | ||||
|     let oldFiles = new Map(); | ||||
|     let focusFiles = []; | ||||
|     let time = new Date().getTime(); | ||||
|     const Loop =async()=> | ||||
|     { | ||||
|         console.log("checking"); | ||||
|     let fileHandlesMap = new Map(); | ||||
|     let lastCheckTime = new Date().getTime(); | ||||
| 
 | ||||
|         focusFiles = []; | ||||
|          | ||||
|         /** @type {Map<string, File>} */ | ||||
|         const newFiles = new Map(); | ||||
|         await checkFiles(dirHandle, extension, time, undefined, newFiles, oldFiles, focusFiles); | ||||
| 
 | ||||
|         // oldFiles now represent delete list | ||||
|         oldFiles.size && console.log("delete:", oldFiles); | ||||
| 
 | ||||
|         // process updated files | ||||
|         focusFiles.length && console.log("update:", focusFiles); | ||||
| 
 | ||||
|         // reset resources | ||||
|         oldFiles = newFiles; | ||||
|         time = new Date().getTime(); | ||||
| 
 | ||||
|         setTimeout(Loop, 3000); | ||||
|     const CoarseCheck = async () => { | ||||
|         console.log("===========coarse check=================="); | ||||
|         fileHandlesMap = new Map(); | ||||
|         await updateFileHandles(dirHandle, extension, "", fileHandlesMap); | ||||
|         setTimeout(CoarseCheck, 10000); | ||||
|     }; | ||||
|      | ||||
|     const FineCheck =async()=>{ | ||||
|         const focusFiles = await checkModifiedDates(fileHandlesMap, new Date(lastCheckTime)); | ||||
|         if (focusFiles.length) { | ||||
|             console.log("Updated files:", focusFiles); | ||||
|             // Open the updated files or handle them as needed | ||||
|         } | ||||
|         lastCheckTime = new Date().getTime(); | ||||
|         setTimeout(FineCheck, 1000); | ||||
|     } | ||||
|     Loop(); | ||||
| 
 | ||||
|     CoarseCheck(); | ||||
|     FineCheck(); | ||||
| } | ||||
| 
 | ||||
| document.getElementById("open").addEventListener("click", main); | ||||
| </script> | ||||
| </body> | ||||
| </html> | ||||
| </html> | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user