...
 
Commits (2)
## 2.0.0
- Move output stream to `.stream` to reduce API surface.
## 1.1.0
- Mark some methods as `@protected`.
......
......@@ -16,40 +16,21 @@ import 'package:bloc_stream/bloc_stream.dart';
import 'package:equatable/equatable.dart';
import 'package:example/event_repository.dart';
// State
// We use the Equatable package so we can easily implement the == operator for
// our state.
abstract class EventsState extends Equatable {
@override
List<Object> get props => [];
}
class NoEvents extends EventsState {}
class EventsLoaded extends EventsState {
EventsLoaded(events);
final List<Event> events;
@override
List<Object> get props => events;
}
// Bloc
class EventsBloc extends BlocStream<EventsState> {
class EventsBloc extends BlocStream<List<Event>> {
EventBloc(this.repository);
final EventRepository repository;
@override
int get initialValue => NoEvents();
int get initialValue => null;
Future<void> fetch() async {
try {
final events = await repository.list();
add(EventsLoaded(events));
subject.add(events);
} catch (err) {
addError(err);
subject.addError(err);
}
}
}
......@@ -57,7 +38,9 @@ class EventsBloc extends BlocStream<EventsState> {
## Usage with Flutter
Use this package in combination with
See [flutter_bloc_stream](https://pub.dev/packages/flutter_bloc_stream).
You can also use this package in combination with
[Provider](https://pub.dev/packages/provider) and
[StreamBuilder](https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html).
......
......@@ -5,11 +5,11 @@ class CounterBloc extends BlocStream<int> {
int get initialValue => 0;
void increment() {
add(value + 1);
subject.add(value + 1);
}
void decrement() {
add(value - 1);
subject.add(value - 1);
}
}
......@@ -17,8 +17,8 @@ class MultiplicationBloc extends BlocStream<int> {
MultiplicationBloc(CounterBloc counter) : _counter = counter {
// Bloc has a helper method for cleaning up subscriptions without having to
// write a boilerplate close() override.
cancelOnClose(counter.distinct().listen((count) {
add(_multiply(count));
cancelOnClose(counter.stream.distinct().listen((count) {
subject.add(_multiply(count));
}));
}
......@@ -49,8 +49,8 @@ void main() {
// 1
// MULTIPLY 6
// MULTIPLY 3
counter.distinct().listen(print);
multiplier.distinct().listen((i) => print('MULTIPLY $i'));
counter.stream.distinct().listen(print);
multiplier.stream.distinct().listen((i) => print('MULTIPLY $i'));
counter.increment();
counter.increment();
......
......@@ -6,43 +6,22 @@ import 'package:meta/meta.dart';
import 'package:rxdart/rxdart.dart';
import 'package:rxdart/subjects.dart';
abstract class BlocStream<State> extends Stream<State> {
abstract class BlocStream<State> {
BlocStream() {
_subject = BehaviorSubject.seeded(initialValue);
subject = BehaviorSubject.seeded(initialValue);
}
State get initialValue => null;
BehaviorSubject<State> _subject;
State get value => _subject.value;
@override
bool get isBroadcast => _subject.isBroadcast;
/// Protected BehaviorSubject used for implementation.
@protected
BehaviorSubject<State> subject;
@override
StreamSubscription<State> listen(
void Function(State) onData, {
Function onError,
void Function() onDone,
bool cancelOnError,
}) {
return _subject.listen(
onData,
onError: onError,
onDone: onDone,
cancelOnError: cancelOnError,
);
}
/// Output stream to be consumed by application.
Stream<State> get stream => subject.stream;
// Methods for adding data to the _subject
@protected
void add(State value) => _subject.add(value);
@protected
void addError(Object error, [StackTrace stackTrace]) =>
_subject.addError(error, stackTrace);
State get initialValue => null;
State get value => subject.value;
// Helper method to reduce boilerplate when composing BLoC's together.
final _closers = <void Function()>[];
/// Helper method to reduce boilerplate when composing BLoC's together.
@protected
void cancelOnClose(StreamSubscription subscription,
[void Function() onClose]) {
......@@ -52,13 +31,15 @@ abstract class BlocStream<State> extends Stream<State> {
});
}
final _closers = <void Function()>[];
Observable<State> asObservable() {
return Observable(this);
return Observable(subject);
}
@mustCallSuper
Future<void> close() {
_closers.forEach((fn) => fn());
return _subject.close();
return subject.close();
}
}
name: bloc_stream
description: A simple package that helps you to implement the BLoC pattern in your applications.
version: 1.1.0
version: 2.0.0
homepage: https://gitlab.com/arisechurch/bloc_stream
environment:
......
......@@ -6,11 +6,11 @@ class CounterBloc extends BlocStream<int> {
int get initialValue => 0;
void increment() {
add(value + 1);
subject.add(value + 1);
}
void decrement() {
add(value - 1);
subject.add(value - 1);
}
}
......
## 2.0.0
- Update for `bloc_stream` 2.x
## 1.0.0
- Initial version
......@@ -3,7 +3,33 @@
This package provides two helpers to help reduce boilerplate when working with
bloc_stream:
- `BlocStreamProvider`
- `BlocStreamBuilder`
## `BlocStreamProvider`
See example for usage.
This is a simple wrapper around the `Provider` package that automatically
disposes `BlocStream`s for you.
```dart
// Create a provider
BlocStreamProvider(
create: (context) => CounterBloc(),
child: myApp,
);
// Consume in a child context.
final counter = BlocStreamProvider.of<CounterBloc>(context);
```
## `BlocStreamBuilder`
A simple wrapper around `StreamBuilder` for building a widget tree when the data
changes. If the `bloc` parameter then it will automatically look for the BLoC
using `BlocStreamProvider.of`:
```dart
BlocStreamBuilder<CounterBloc, int>(builder: (context, snapshot) {
if (snapshot.hasError) return Text("Fail.");
else if (!snapshot.hasData) return Container();
return Text("${snapshot.data}");
});
```
......@@ -11,11 +11,11 @@ class CounterBloc extends BlocStream<int> {
int get initialValue => 0;
void increment() {
add(value + 1);
subject.add(value + 1);
}
void decrement() {
add(value - 1);
subject.add(value - 1);
}
}
......
......@@ -18,7 +18,7 @@ class BlocStreamBuilder<B extends BlocStream<S>, S> extends StatelessWidget {
final actualBloc = bloc ?? BlocStreamProvider.of<B>(context);
return StreamBuilder<S>(
stream: actualBloc.distinct(),
stream: actualBloc.stream.distinct(),
initialData: actualBloc.value,
builder: builder,
);
......
name: flutter_bloc_stream
description: Helper package for using bloc_stream with Flutter. Includes BlocStreamProvider and BlocStreamBuilder.
version: 1.0.0
version: 2.0.0
homepage: https://gitlab.com/arisechurch/bloc_stream
environment:
......@@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
bloc_stream: ">= 1.0.0 < 2.0.0"
bloc_stream: ">= 2.0.0 < 3.0.0"
provider: ">= 3.0.0 < 4.0.0"
dev_dependencies:
......