Skip to content

Commit 59d6e3e

Browse files
committed
fix: js.Iterator with advanced types
1 parent 94b3b5c commit 59d6e3e

5 files changed

Lines changed: 29 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
## 1.0.0-dev.4
2+
3+
- Fixes for `js.Iterator` with Arrays. (Resolves issue with `Headers`).
4+
15
## 1.0.0-dev.3
6+
27
- **BREAKING**: Renamed extensions from `<Class>Extension` to `<Class>InstanceMembers`.
38
- Added `fetch_api.compatibility_layer` library to support Dart 2.19.
49
- Added `createHeadersFromMap`

lib/src/headers/headers.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ extension HeadersInstanceMembers on Headers {
9797

9898
/// Returns an [IteratorWrapper] allowing to go through all key/value pairs
9999
/// contained in this object.
100-
IteratorWrapper<List<String>> entries() => IteratorWrapper(_entries());
100+
IteratorWrapper<List<String>> entries() => IteratorWrapper(
101+
_entries(),
102+
(value) => (value as List?)?.cast(),
103+
);
101104

102105
/// Returns an [IteratorWrapper] allowing you to go through all keys of the
103106
/// key/value pairs contained in this object.

lib/src/iterator_result.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ class IteratorResult<T> {
3434

3535
/// Any JavaScript value returned by the iterator.
3636
/// Can be omitted when [done] is `true`.
37-
external T? value;
37+
external dynamic value;
3838
}

lib/src/iterator_wrapper.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,28 @@ import 'dart:core' as core;
44
import 'iterator.dart' as js;
55

66

7+
/// Function that processes JS dynamic value into target class [T].
8+
/// It is needed when JS value isn't exact type of [T], e.g. JS lists are
9+
/// always dynamic.
10+
typedef IteratorValueCaster<T> = T? Function(dynamic value);
11+
712
/// Wrapper on top of [js.Iterator], implementing [Iterable] and [Iterator] APIs.
813
class IteratorWrapper<T> extends IterableBase<T> implements core.Iterator<T> {
9-
IteratorWrapper(this._iterator);
14+
IteratorWrapper(this._iterator, [IteratorValueCaster<T>? valueCaster])
15+
: valueCaster = valueCaster ?? _valueCasterAs;
16+
17+
/// Default value caster via `as`.
18+
static T? _valueCasterAs<T>(dynamic value) => value as T?;
1019

20+
/// Target [js.Iterator].
1121
final js.Iterator<T> _iterator;
1222

23+
/// Function that processes JS dynamic value into target class [T].
24+
/// It is needed when JS value isn't exact type of [T], e.g. JS lists are
25+
/// always dynamic.
26+
final IteratorValueCaster<T> valueCaster;
27+
28+
/// Current value buffer.
1329
T? _current;
1430

1531
@override
@@ -18,7 +34,7 @@ class IteratorWrapper<T> extends IterableBase<T> implements core.Iterator<T> {
1834
@override
1935
bool moveNext() {
2036
final next = _iterator.next();
21-
_current = next.value;
37+
_current = valueCaster(next.value);
2238
return !(next.done ?? false);
2339
}
2440

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: fetch_api
22
description: JavaScript bindings for Fetch API, allowing a flexible HTTP requests.
3-
version: 1.0.0-dev.3
3+
version: 1.0.0-dev.4
44
homepage: https://github.com/Zekfad/fetch_api
55
repository: https://github.com/Zekfad/fetch_api
66
issue_tracker: https://github.com/Zekfad/fetch_api/issues

0 commit comments

Comments
 (0)