Skip to content

Commit 691d324

Browse files
Merge pull request #6 from alvarobcprado/feature/add-auto-initial-state-stream-data
Feature/add auto initial state stream data
2 parents f3a690c + c14bec5 commit 691d324

File tree

9 files changed

+23
-15
lines changed

9 files changed

+23
-15
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.6
2+
* **break**: remove `initialData` and `emptyWidget` from `StateStream` collector
3+
* add automatically emit of initial value to `StateStream`
4+
15
## 0.0.5
26
* remove stream from ActionNotifier in favor of extension type `ActionStream`
37
* remove stream from StateNotifier in favor of extension type `StateStream`

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class _MainAppState extends State<MainApp> {
6363
// Collect the data from the stream
6464
// and build a widget based on its data
6565
counter.countStream.collectAsWidget(
66-
initialData: 0,
6766
(value) {
6867
return Text('Count: $value');
6968
},

example/lib/calculator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:view_model_macro/view_model_macro.dart';
44
class Calculator {
55
Calculator() {
66
countStream.collect((countValue) {
7-
if (countValue % 3 == 0) {
7+
if (countValue % 3 == 0 && countValue > 0) {
88
_dispatchShowSnackBar();
99
}
1010
});

example/lib/main.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class _MainAppState extends State<MainApp> {
4343
mainAxisSize: MainAxisSize.min,
4444
children: [
4545
counter.countStream.collectAsWidget(
46-
initialData: 0,
4746
(value) {
4847
return Text('Count: $value');
4948
},

example/pubspec.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ packages:
144144
url: "https://pub.dev"
145145
source: hosted
146146
version: "1.9.0"
147+
rxdart:
148+
dependency: transitive
149+
description:
150+
name: rxdart
151+
sha256: "5c3004a4a8dbb94bd4bf5412a4def4acdaa12e12f269737a5751369e12d1a962"
152+
url: "https://pub.dev"
153+
source: hosted
154+
version: "0.28.0"
147155
sky_engine:
148156
dependency: transitive
149157
description: flutter
@@ -211,7 +219,7 @@ packages:
211219
path: ".."
212220
relative: true
213221
source: path
214-
version: "0.0.4"
222+
version: "0.0.5"
215223
vm_service:
216224
dependency: transitive
217225
description:

lib/src/core/collector.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@ import 'package:view_model_macro/src/widgets/stream_listener.dart';
77
/// Extension methods for collecting data from [StateStream]s.
88
extension StateStreamCollectorX<T> on StateStream<T> {
99
/// Collects the data from the stream to build a widget based on its data.
10-
Widget collectAsWidget(
11-
Widget Function(T) builder, {
12-
Widget? emptyData,
13-
T? initialData,
14-
}) {
10+
Widget collectAsWidget(Widget Function(T) builder) {
1511
return StreamBuilder(
1612
stream: stream,
17-
initialData: initialData,
1813
builder: (context, snapshot) {
1914
if (snapshot.hasData) {
2015
return builder(snapshot.data as T);
2116
} else {
22-
return emptyData ?? const SizedBox.shrink();
17+
return const SizedBox.shrink();
2318
}
2419
},
2520
);

lib/src/notifiers/notifier.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import 'package:meta/meta.dart';
1616
/// {@endtemplate}
1717
abstract class Notifier<T> {
1818
/// {@macro Notifier}
19-
Notifier();
19+
Notifier([StreamController<T>? controller]):
20+
_controller = controller ?? StreamController<T>.broadcast();
2021

21-
final StreamController<T> _controller = StreamController<T>.broadcast();
22+
final StreamController<T> _controller;
2223
final List<StreamSubscription<T>> _subscriptions = [];
2324

2425
/// Notifies all listeners with the new value.

lib/src/notifiers/state_notifier.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:async';
22

3+
import 'package:rxdart/rxdart.dart';
34
import 'package:view_model_macro/src/notifiers/notifier.dart';
45

56
/// The type wrapper for [Stream]s emitted by a [StateNotifier].
@@ -19,7 +20,7 @@ extension type StateStream<T>(Stream<T> stream) {}
1920
/// {@endtemplate}
2021
class StateNotifier<T> extends Notifier<T> {
2122
/// {@macro StateNotifier}
22-
StateNotifier(this._state);
23+
StateNotifier(this._state) : super(BehaviorSubject<T>.seeded(_state));
2324

2425
T _state;
2526

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: view_model_macro
22
description: "Experimental support for ViewModel classes in Dart using pckg:macros."
3-
version: 0.0.5
3+
version: 0.0.6
44
repository: https://github.com/alvarobcprado/view_model_macro
55
topics: [macros, view-model]
66

@@ -13,6 +13,7 @@ dependencies:
1313
sdk: flutter
1414
macros: ^0.1.0-main.5
1515
meta: ^1.15.0
16+
rxdart: ^0.28.0
1617

1718
dev_dependencies:
1819
flutter_test:

0 commit comments

Comments
 (0)