web
web copied to clipboard
Make `NodeList` `Iterable<Node>`
Any chance of making NodeList
mix-in Iterable<Node>
?
Something like:
@JS()
@staticInterop
class IterableNodeList extends html.NodeList with Iterable<html.Node> {
@override
Iterator<html.Node> get iterator => _NodeListIterator(this);
}
class _NodeListIterator implements Iterator<html.Node> {
_NodeListIterator(html.NodeList data) : _data = data;
final html.NodeList _data;
int _currentIndex = 0;
@override
html.Node get current => _data.item(_currentIndex)!;
@override
bool moveNext() {
_currentIndex++;
return _currentIndex < _data.length;
}
}
(Or similar!)
Maybe it should also mixin List, similar to dart:html
https://api.dart.dev/stable/2.19.5/dart-html/NodeList-class.html
FYI - @rutvik110 added a nifty wrapper to handle such immutable lists easier with JSImmutableListWrapper
here: https://github.com/dart-lang/web/issues/281.
That being said, it's a wrapper and not like a dart:html
list type where it's both the interop type and a List
. Doing both is not really feasible since it'll require modifying the type system quite heavily in dart2wasm (and we're trying to get away from this in the JS compilers). If this is insufficient, feel free to reopen, but for now marking as completed.