Skip to content

Commit b3ba6c4

Browse files
committed
refacor and update docs
1 parent 931e0a1 commit b3ba6c4

File tree

6 files changed

+37
-40
lines changed

6 files changed

+37
-40
lines changed

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ class MyBloc implements BaseBloc {
4747

4848
## Example: A port of the standard "Counter Button" example from Flutter
4949

50-
### 1. File `counter_bloc.dart`:
50+
### 1. File `counter_bloc.dart`
51+
5152
```dart
5253
import 'dart:async';
5354
55+
import 'package:disposebag/disposebag.dart';
5456
import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';
5557
import 'package:rxdart_ext/rxdart_ext.dart';
5658
@@ -62,41 +64,36 @@ class CounterBloc extends DisposeCallbackBaseBloc {
6264
final StateStream<int> state;
6365
6466
CounterBloc._({
65-
required void Function() dispose,
67+
required VoidAction dispose,
6668
required this.increment,
6769
required this.state,
6870
}) : super(dispose);
6971
7072
factory CounterBloc() {
71-
// ignore: close_sinks
7273
final incrementController = StreamController<void>();
7374
74-
final state = incrementController.stream
75+
final state$ = incrementController.stream
7576
.scan<int>((acc, _, __) => acc + 1, 0)
7677
.publishState(0);
77-
final connection = state.connect();
7878
7979
return CounterBloc._(
80-
dispose: () async {
81-
await connection.cancel();
82-
await incrementController.close();
83-
print('CounterBloc::disposed');
84-
},
85-
increment: () => incrementController.add(null),
86-
state: state,
80+
dispose: DisposeBag([incrementController, state$.connect()]).dispose,
81+
increment: incrementController.addNull,
82+
state: state$,
8783
);
8884
}
8985
}
9086
```
9187

92-
### 2. File `main.dart`:
88+
### 2. File `main.dart`
89+
9390
```dart
9491
import 'package:example/bloc.dart';
9592
import 'package:flutter/material.dart';
9693
import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';
9794
9895
class TextCounter1 extends StatelessWidget {
99-
const TextCounter1({Key? key}) : super(key: key);
96+
const TextCounter1({super.key});
10097
10198
@override
10299
Widget build(BuildContext context) {
@@ -107,15 +104,15 @@ class TextCounter1 extends StatelessWidget {
107104
builder: (context, state) {
108105
return Text(
109106
'COUNTER 1: $state',
110-
style: Theme.of(context).textTheme.headline6,
107+
style: Theme.of(context).textTheme.titleLarge,
111108
);
112109
},
113110
);
114111
}
115112
}
116113
117114
class IncrementButton extends StatelessWidget {
118-
const IncrementButton({Key? key}) : super(key: key);
115+
const IncrementButton({super.key});
119116
120117
@override
121118
Widget build(BuildContext context) {
@@ -124,9 +121,8 @@ class IncrementButton extends StatelessWidget {
124121
return FloatingActionButton(
125122
onPressed: bloc.increment,
126123
tooltip: 'Increment',
127-
child: Icon(Icons.add),
124+
child: const Icon(Icons.add),
128125
);
129126
}
130127
}
131-
132128
```

example/README.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,37 @@
44
```dart
55
import 'dart:async';
66
7-
import 'package:distinct_value_connectable_stream/distinct_value_connectable_stream.dart';
7+
import 'package:disposebag/disposebag.dart';
88
import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';
99
import 'package:rxdart_ext/rxdart_ext.dart';
1010
1111
class CounterBloc extends DisposeCallbackBaseBloc {
1212
/// Inputs
13-
final void Function() increment;
13+
final VoidAction increment;
1414
1515
/// Outputs
16-
final DistinctValueStream<int> state;
16+
final StateStream<int> state;
1717
1818
CounterBloc._({
19-
required void Function() dispose,
19+
required VoidAction dispose,
2020
required this.increment,
2121
required this.state,
2222
}) : super(dispose);
2323
2424
factory CounterBloc() {
25-
// ignore: close_sinks
2625
final incrementController = StreamController<void>();
2726
28-
final state = incrementController.stream
29-
.scan<int>((acc, _, __) => acc! + 1, 0)
30-
.publishValueDistinct(0);
31-
final connection = state.connect();
27+
final state$ = incrementController.stream
28+
.scan<int>((acc, _, __) => acc + 1, 0)
29+
.publishState(0);
3230
3331
return CounterBloc._(
34-
dispose: () async {
35-
await connection.cancel();
36-
await incrementController.close();
37-
print('>>> disposed');
38-
},
39-
increment: () => incrementController.add(null),
40-
state: state,
32+
dispose: DisposeBag([incrementController, state$.connect()]).dispose,
33+
increment: incrementController.addNull,
34+
state: state$,
4135
);
4236
}
4337
}
44-
4538
```
4639

4740
### 2. File `main.dart`:

example/lib/bloc_with_deps.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Bloc1 extends DisposeCallbackBaseBloc {
2828
final StateStream<String?> string$;
2929

3030
Bloc1._({
31-
required void Function() dispose,
31+
required VoidAction dispose,
3232
required this.load,
3333
required this.string$,
3434
}) : super(dispose);

example/lib/counter_bloc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CounterBloc extends DisposeCallbackBaseBloc {
1212
final StateStream<int> state;
1313

1414
CounterBloc._({
15-
required void Function() dispose,
15+
required VoidAction dispose,
1616
required this.increment,
1717
required this.state,
1818
}) : super(dispose);

lib/src/base.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ abstract class BaseBloc {
77
/// Base bloc that implements [BaseBloc.dispose] by passing callback to constructor,
88
/// and call it when [BaseBloc.dispose] called.
99
class DisposeCallbackBaseBloc implements BaseBloc {
10-
final void Function() _dispose;
10+
final VoidAction _dispose;
1111

1212
/// Create a [DisposeCallbackBaseBloc] by a dispose callback.
1313
DisposeCallbackBaseBloc(this._dispose);
@@ -19,8 +19,13 @@ class DisposeCallbackBaseBloc implements BaseBloc {
1919
// Function types
2020

2121
/// Represents a function that have no arguments and return no data.
22+
/// See also [VoidFunc0].
2223
typedef VoidAction = void Function();
2324

25+
/// Represents a function that have no arguments and return no data.
26+
/// This is an alias of [VoidAction].
27+
typedef VoidFunc0 = VoidAction;
28+
2429
/// Represents a function with zero arguments: `() -> R`.
2530
typedef Func0<R> = R Function();
2631

test/flutter_bloc_pattern_test.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ void main() {
5151
group('Base and Error', () {
5252
test('Function types', () {
5353
// ignore: omit_local_variable_types
54-
final VoidAction a = () {};
54+
final VoidAction a1 = () {};
55+
56+
// ignore: omit_local_variable_types
57+
final VoidFunc0 a2 = () {};
5558

5659
// ignore: omit_local_variable_types
5760
final Func0<void> f0 = () {};
@@ -88,7 +91,7 @@ void main() {
8891
final Func9<int, String, double, int, List<int>, Map<int, int>, void,
8992
bool, bool, bool> f9 = (i, s, d, i2, li, map, _, b, b2) => b && b2;
9093

91-
[a, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9].forEach(print);
94+
[a1, a2, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9].forEach(print);
9295
});
9396

9497
test('DisposeCallbackBaseBloc', () {

0 commit comments

Comments
 (0)