Skip to content

Commit 79eb46f

Browse files
committed
prepare 0.0.2
1 parent 9992dff commit 79eb46f

File tree

6 files changed

+65
-27
lines changed

6 files changed

+65
-27
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.2 - Jan 12, 2021
2+
3+
- Internal refactor
4+
15
## 0.0.1 - Dec 15, 2020
26

37
- Initial version.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
[![codecov](https://codecov.io/gh/Flutter-Dart-Open-Source/listenable_stream/branch/master/graph/badge.svg?token=6eORcR6Web)](https://codecov.io/gh/Flutter-Dart-Open-Source/listenable_stream)
88
[![Flutter Tests](https://github.com/Flutter-Dart-Open-Source/listenable_stream/workflows/Flutter%20Tests/badge.svg)](https://github.com/Flutter-Dart-Open-Source/listenable_stream.git)
99

10+
- [x] `Listenable``Stream<Listenable>`
11+
- [x] `ValueListenable<T>``ValueStream<T>`
12+
1013
## Listenable.toStream()
1114
```dart
1215
final ChangeNotifier changeNotifier = ChangeNotifier();

lib/src/common.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/foundation.dart' show Listenable, VoidCallback;
4+
5+
/// @private
6+
/// Convert this [Listenable] to a [Stream].
7+
Stream<R> toStreamWithTransform<T extends Listenable, R>(
8+
T listenable,
9+
R Function(T) transform,
10+
) {
11+
StreamController<R> controller;
12+
VoidCallback listener;
13+
14+
final onListenOrOnResume = () {
15+
try {
16+
listenable
17+
.addListener(listener = () => controller.add(transform(listenable)));
18+
} catch (_ /*Ignore*/) {
19+
controller.close();
20+
}
21+
};
22+
23+
final createOnPauseOrOnCancel = ([bool closeOnError = false]) {
24+
return () {
25+
try {
26+
listenable.removeListener(listener);
27+
listener = null;
28+
} catch (_ /*Ignore*/) {
29+
if (identical(closeOnError, true)) {
30+
controller.close();
31+
}
32+
}
33+
};
34+
};
35+
36+
controller = StreamController<R>(
37+
onListen: onListenOrOnResume,
38+
onPause: createOnPauseOrOnCancel(true),
39+
onResume: onListenOrOnResume,
40+
onCancel: createOnPauseOrOnCancel(),
41+
);
42+
43+
return controller.stream;
44+
}

lib/src/listenable_to_stream.dart

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
11
import 'dart:async';
22

3-
import 'package:flutter/foundation.dart' show Listenable, VoidCallback;
3+
import 'package:flutter/foundation.dart' show Listenable;
4+
5+
import 'common.dart';
46

57
/// Convert this [Listenable] to a [Stream].
68
extension ListenableToStream<T extends Listenable> on T {
79
/// Convert this [Listenable] to a [Stream].
8-
Stream<T> toStream() {
9-
StreamController<T> controller;
10-
VoidCallback listener;
11-
12-
controller = StreamController<T>(
13-
sync: true,
14-
onListen: () {
15-
listener = () => controller.add(this);
16-
addListener(listener);
17-
},
18-
onCancel: () {
19-
try {
20-
removeListener(listener);
21-
listener = null;
22-
} catch (_ /*Ignore*/) {}
23-
},
24-
);
25-
26-
return controller.stream;
27-
}
10+
Stream<T> toStream() => toStreamWithTransform<T, T>(this, (t) => t);
2811
}

lib/src/value_listenable_to_value_stream.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import 'dart:async' show Stream, StreamSubscription;
33
import 'package:flutter/foundation.dart' show ValueListenable;
44
import 'package:rxdart/rxdart.dart' show ErrorAndStackTrace, ValueStream;
55

6-
import 'listenable_to_stream.dart';
6+
import 'common.dart';
77
import 'streamx.dart';
88

9+
extension _ValueListenableToStreamExtension<T> on ValueListenable<T> {
10+
Stream<T> toStream() =>
11+
toStreamWithTransform<ValueListenable<T>, T>(this, (l) => l.value);
12+
}
13+
914
/// Convert this [ValueListenable] to a [ValueStream].
1015
/// The returned [ValueStream] is a Single-Subscription [Stream].
1116
///
@@ -52,12 +57,11 @@ class ValueListenableStream<T> extends Stream<T> implements ValueStream<T> {
5257
void Function() onDone,
5358
bool cancelOnError,
5459
}) {
55-
final getValue = ([void _]) => _valueListenable.value;
56-
5760
if (_replayValue) {
58-
_stream ??= _valueListenable.toStream().map(getValue).startWith(getValue);
61+
_stream ??=
62+
_valueListenable.toStream().startWith(() => _valueListenable.value);
5963
} else {
60-
_stream ??= _valueListenable.toStream().map(getValue);
64+
_stream ??= _valueListenable.toStream();
6165
}
6266

6367
return _stream.listen(

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: listenable_stream
22
description: Convert Listenable (eg. ChangeNotifier), ValueListenable(eg. ValueNotifier) to Stream / ValueStream.
3-
version: 0.0.1
3+
version: 0.0.2
44
author: Petrus Nguyễn Thái Học <hoc081098@gmail.com>
55
homepage: https://github.com/Flutter-Dart-Open-Source/listenable_stream.git
66
repository: https://github.com/Flutter-Dart-Open-Source/listenable_stream.git

0 commit comments

Comments
 (0)