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

FileSystemDirectoryHandle is missing the methods to enumerate its content #318

Open
gdurandrexel opened this issue Oct 29, 2024 · 2 comments

Comments

@gdurandrexel
Copy link

In version 1.1.0, there is no values(), keys() or entries() defined in FileSystemDirectoryHandle.

I understand only standard API is emitted but in this case, in MDN, only Android Webview is not supported, which is exactly the same as removeEntry() which is emitted.

Did I miss something?

@srujzs
Copy link
Contributor

srujzs commented Oct 29, 2024

You're correct - they are missing and need to be added. As always, the workaround is to use external extension methods until this is added. I believe this is a duplicate of #21, except here it's async: https://github.com/w3c/webref/blob/23c3d948f58f16e420d388269d29312f04cc419f/ed/idl/fs.idl#L45.

The Web IDL definition is here: https://webidl.spec.whatwg.org/#idl-iterable. There's an async section after that as well: https://webidl.spec.whatwg.org/#idl-async-iterable. This would add entries, keys, values, and forEach (if not async). We may want to add an Iterator type to dart:js_interop or package:web to support this.

I thought we somewhat handled this with handling getter/setter and adding a List wrapper type, but we did not.

@gdurandrexel
Copy link
Author

For those wanting an implementation right now, here is some code that works (with no exception handling):

extension FileSystemDirectoryHandleEx on FileSystemDirectoryHandle {
  Stream<FileSystemHandle> values() {
    final iterator = callMethod<JSObject>('values'.toJS);

    return _asyncIterator<FileSystemHandle>(iterator);
  }
}

Stream<T> _asyncIterator<T extends JSObject>(JSObject iterator) async* {
  while (true) {
    final next = await iterator.callMethod<JSPromise<T>>('next'.toJS).toDart;

    if (next.getProperty<JSBoolean>('done'.toJS).toDart) {
      break;
    }

    yield next.getProperty<T>('value'.toJS);
  }
}

Use like this:

await for (FileSystemHandle handle in directoryHandle.values()) {
  ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

2 participants