Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Ergonomics: Pointer to associated FileSystemFileHandle from FileSystemSyncAccessHandle? #99

Open
tomayac opened this issue Mar 2, 2023 · 0 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest

Comments

@tomayac
Copy link
Contributor

tomayac commented Mar 2, 2023

For getting a FileSystemSyncAccessHandle, you need a FileSystemFileHandle first, which, in a Worker, where you want to work with the all-sync file methods, you generally don't have much use for, so you could "skip" it as below:

// In a `Worker`.
const root = await navigator.storage.getDirectory();
const accessHandle = await (await root.getFileHandle('fast', {create: true})).createSyncAccessHandle();

If, however, you decide at some point wanting to transfer the file from the OPFS over to the user-visible file system, you now need to postMessage() a FileSystemFileHandle in order to let the user run showSaveFilePicker() over on the main thread, where you can then write() the File you obtain via getFile().

Would it make sense to add a FileSystemSyncAccessHandle.fileHandle property, that would point at the associated FileSystemFileHandle? This would enable the following flow:

// In the `Worker`.
const root = await navigator.storage.getDirectory();
const accessHandle = await (await root.getFileHandle('fast', {create: true})).createSyncAccessHandle();
/* … */
self.postMessage('save', [accessHandle.fileHandle]);
// On the main thread after receiving the message with the `FileSystemFileHandle`.
/* … */
const saveHandle = await showSaveFilePicker();
const writable = await saveHandle.createWritable();
await writable.write(await fileHandle.getFile());
await writable.close();

(The alternative would be to just keep a reference around to the FileSystemFileHandle in the Worker until it's needed. It's really mostly a potential ergonomics improvement. Thoughts?)

@annevk annevk added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest
Development

No branches or pull requests

2 participants