Skip to content

Implement iterable on types where it's defined #21

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

Open
kevmoo opened this issue Feb 20, 2023 · 3 comments
Open

Implement iterable on types where it's defined #21

kevmoo opened this issue Feb 20, 2023 · 3 comments
Assignees

Comments

@kevmoo
Copy link
Member

kevmoo commented Feb 20, 2023

Not sure if this in on your radar or not, @joshualitt

@srujzs
Copy link
Contributor

srujzs commented Oct 31, 2023

To make this more concrete, there are iterable types in the IDL e.g. NodeList

https://github.com/w3c/webref/blob/75015f219dc2fede4af8c953d93b1517c1691204/ed/idl/dom.idl#L158

that should expose iterable methods like forEach. https://webidl.spec.whatwg.org/#idl-iterable

@ditman
Copy link
Member

ditman commented Feb 29, 2024

I checked and the FileList type seems to not be marked iterable, but in the MDN example, they use it with for ... of:

for (const file of fileInput.files) {
  output.innerText += `\n${file.name}`;
}

It'd be nice if there was some way of wrapping those "things that have length and item(i)" in something that lets me use them as lists in Dart!


PS, I added this:

/// Adds a `toList` method to [web.FileList] objects.
extension WebFileListToDartList on web.FileList {
  /// Converts a [web.FileList] into a [List] of [web.File].
  ///
  /// This method makes a copy.
  List<web.File> get toList =>
      <web.File>[for (int i = 0; i < length; i++) item(i)!];
}

@srujzs
Copy link
Contributor

srujzs commented Mar 1, 2024

There's a similar issue here: #91 (see point b).

I think going the route of adding a toDartIterable in the SDK makes sense for types like this which behave very similar to lists but are not Arrays e.g.

extension on JSObject {
  /// Wraps this object with an [Iterable] implementation whose
  /// methods forward to the object.
  external Iterable<T> toDartIterable<T extends JSAny?>();
}

These types don't really have a supertype in JS that groups them in some kind of "list-like" category, so there isn't a concrete separation from JSObject. We could make it return a List<T> as well, I'm just not sure if types like FileList support all the methods we need for that interface.

I don't want to expose APIs that copy over the values into a Dart list as it's more expensive and users likely iterate over the list only once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

3 participants