coarse check / fine check

This commit is contained in:
Seth Trowbridge 2024-10-11 16:11:42 -04:00
parent e892449c63
commit 904f7944b2
2 changed files with 59 additions and 40 deletions

3
transpiler/_test.html Normal file
View File

@ -0,0 +1,3 @@

View File

@ -3,58 +3,74 @@
<body> <body>
<button id="open">open</button> <button id="open">open</button>
<script type="module"> <script type="module">
// Function to walk through the directory and check files // Function to walk through the directory and update file handles
async function checkFiles(dirHandle, extension, date, path="", /** @type {Map<string, File>}*/map, /** @type {Map<string, File>}*/deleteList, /** @type {Array<{fullPath:string, file:File>}*/focusList) { async function updateFileHandles(dirHandle, extension, path = "", map) {
for await (const entry of dirHandle.values()) for await (const entry of dirHandle.values()) {
{ if (entry.kind === 'file' && entry.name.endsWith(extension)) {
if (entry.kind === 'file' && entry.name.endsWith(extension))
{
const file = await entry.getFile(); const file = await entry.getFile();
const fullPath = path + entry.name; const fullPath = path + entry.name;
map.set(fullPath, file); if (!map.has(dirHandle)) {
deleteList.delete(fullPath); map.set(dirHandle, new Set());
if (file.lastModified > date)
{
focusList.push({file, fullPath});
} }
} else if (entry.kind === 'directory') map.get(dirHandle).add(fullPath);
{ } else if (entry.kind === 'directory') {
await checkFiles(entry, extension, date, path+entry.name, map, deleteList, focusList); // Recursively check subdirectories 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() { async function main() {
const dirHandle = await window.showDirectoryPicker(); const dirHandle = await window.showDirectoryPicker();
const extension = '.html'; // Change this to the desired file extension const extension = '.html'; // Change this to the desired file extension
let oldFiles = new Map(); let fileHandlesMap = new Map();
let focusFiles = []; let lastCheckTime = new Date().getTime();
let time = new Date().getTime();
const Loop =async()=>
{
console.log("checking");
focusFiles = []; const CoarseCheck = async () => {
console.log("===========coarse check==================");
fileHandlesMap = new Map();
await updateFileHandles(dirHandle, extension, "", fileHandlesMap);
setTimeout(CoarseCheck, 10000);
};
/** @type {Map<string, File>} */ const FineCheck =async()=>{
const newFiles = new Map(); const focusFiles = await checkModifiedDates(fileHandlesMap, new Date(lastCheckTime));
await checkFiles(dirHandle, extension, time, undefined, newFiles, oldFiles, focusFiles); if (focusFiles.length) {
console.log("Updated files:", focusFiles);
// oldFiles now represent delete list // Open the updated files or handle them as needed
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);
} }
Loop(); lastCheckTime = new Date().getTime();
setTimeout(FineCheck, 1000);
}
CoarseCheck();
FineCheck();
} }
document.getElementById("open").addEventListener("click", main); document.getElementById("open").addEventListener("click", main);
</script> </script>
</body> </body>