diff --git a/lib/get-readers.js b/lib/get-readers.js index 2ebe41b..b2b2fd9 100644 --- a/lib/get-readers.js +++ b/lib/get-readers.js @@ -1,5 +1,7 @@ 'use strict'; +const watchFiles = []; + const getExt = value => { const parts = value.split('.'); return parts[parts.length - 1]; @@ -114,7 +116,18 @@ const getReaders = async (handler, vcdPath) => { }); dropZoneEl.addEventListener('click', () => { - inputEl.click(); + if (window.showOpenFilePicker){ + showOpenFilePicker().then(async fileHandle => { + const files = await Promise.all(fileHandle.map(async file => { + return await file.getFile(); + })) + watchFiles.push({fileHandle, files}); + await handleFiles({files}, handler)(); + }); + return; + } else { + inputEl.click(); + } }, false); @@ -144,5 +157,7 @@ const getReaders = async (handler, vcdPath) => { }; module.exports = getReaders; +module.exports.watchFiles = watchFiles; +module.exports.handleFiles = handleFiles; /* eslint-env browser */ diff --git a/lib/vcdrom.js b/lib/vcdrom.js index 610480f..3941063 100644 --- a/lib/vcdrom.js +++ b/lib/vcdrom.js @@ -123,10 +123,33 @@ global.VCDrom = async (divName, vcdPath) => { content.innerHTML = stringify(dropZone({width: 2048, height: 2048})); const mod = await createVCD(); - const inst = await webVcdParser(mod); // VCD parser instance + let inst = await webVcdParser(mod); // VCD parser instance const handler = getHandler(content, inst); await getReaders(handler, vcdPath); // console.log(content); + + if (window.showOpenFilePicker == undefined) { + return; + } + setInterval(async () => { + getReaders.watchFiles.map( + async ({fileHandle, files}) => { + const newFiles = await Promise.all(fileHandle.map(async file => { + return await file.getFile(); + })); + const allEq = newFiles.every((newFile, i) => { + const file = files[i]; + const eq = newFile.lastModified === file.lastModified; + getReaders.watchFiles[i].files = newFiles; + return eq; + }); + if (!allEq) { + inst = await webVcdParser(mod); + const handler = getHandler(content, inst); + await getReaders.handleFiles({files: newFiles}, handler)(); + } + }); + }, 1000); }; /* eslint-env browser */