You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My first test of this api, I hit wanting to get deep files immediately. Wanting to do something as simple as
/*I've removed await/.then() for simplicity sake[ "./possibly/deep/directory/of/unknown/depth.json", "./possibly/deep/directory/of/unknown/depth/even/deeper.json", "./some/other/folder/somewhere.json"]*/constfilesToProcess=JSON.parse(folderHandler.getFileHandler("processMe.json").getFile().text());for(constfilePathoffilesToProcess){// "./possibly/deep/directory/of/unknown/depth.json"constfileContent=JSON.parse(folderHandler.getFileHandler(filePath).getFile().text());doSomethingWith(fileContent);}
But I ended up having to use the code below,
This is with some caching map, because I didn't want to have to build up the promises of each of the ./possibly/deep/directory/of/unknown/ folder each and every time.
constcache: Map<string,FileSystemDirectoryHandle>=newMap();constfilesToProcess=JSON.parse(folderHandler.getFileHandler("processMe.json").getFile().text());for(constfilePathoffilesToProcess){// "./possibly/deep/directory/of/unknown/depth.json"constfileContent=awaitreadFile(files,rootFolderHandler,cache);doSomethingWith(fileContent);}asyncfunctionreadFile(path: string,// "./possibly/deep/directory/of/unknown/depth.json"rootFolderHandler: FileSystemDirectoryHandle,cache: Map<string,FileSystemDirectoryHandle>,): Promise<any>{const{ folderParts, filename }=getPathParts(path);constfolder=awaitgetFolder(rootFolderHandler,folderParts,cache);returnloadJson(folder.getFileHandle(filename));}functiongetPathParts(path: string): PathParts{constfolderParts=path.split(/\/|\\/g).filter((p)=>p!=='.');constfilename=folderParts.pop()!;// assuming the file name is the last part.return{
folderParts,
filename,};}asyncfunctiongetFolder(rootFolder: FileSystemDirectoryHandle,parts: string[],cache: Map<string,FileSystemDirectoryHandle>,): Promise<FileSystemDirectoryHandle>{letcurrentFolder=rootFolder;for(leti=0;i<parts.length;i++){constfolderName=parts[i];constpath=parts.slice(0,i+1).join('/');letpathFolder=cache.get(path);if(!pathFolder){pathFolder=awaitcurrentFolder.getDirectoryHandle(folderName);// this is the part that builds up over and overcache.set(path,pathFolder);// cache so we don't have to do the await over and over again for same folders.}currentFolder=pathFolder;}returncurrentFolder;}functionloadJson(file: Promise<FileSystemFileHandle>): Promise<any>{returnfile.then((fh)=>fh.getFile()).then((f)=>f.text()).then(JSON.parse);}
This type of stuff would be great if it was just internal.
I am 100% confident javascript engine devs could do this stuff 1000% better than my late night botching attempt and I am sure I will not be the only person that would want to do this, especially if the first ever thing I wanted to do, needed it.
The text was updated successfully, but these errors were encountered:
My first test of this api, I hit wanting to get deep files immediately. Wanting to do something as simple as
But I ended up having to use the code below,
This is with some caching map, because I didn't want to have to build up the promises of each of the
./possibly/deep/directory/of/unknown/
folder each and every time.This type of stuff would be great if it was just internal.
I am 100% confident javascript engine devs could do this stuff 1000% better than my late night botching attempt and I am sure I will not be the only person that would want to do this, especially if the first ever thing I wanted to do, needed it.
The text was updated successfully, but these errors were encountered: