Commit 4d0ec640 authored by Tim Smart's avatar Tim Smart

Switch back to the Stream based API

parent f84e20ac
## 3.0.0
- Switch back to original API
## 2.1.0
- Remove `asObservable`
......
......@@ -5,22 +5,49 @@ import 'dart:async';
import 'package:meta/meta.dart';
import 'package:rxdart/rxdart.dart';
abstract class BlocStream<State> {
abstract class BlocStream<State> extends Stream<State> {
BlocStream() {
subject = BehaviorSubject.seeded(initialValue);
_subject = BehaviorSubject.seeded(initialValue);
}
/// Protected BehaviorSubject used for implementation.
@protected
BehaviorSubject<State> subject;
State get initialValue => null;
/// Output stream to be consumed by application.
Stream<State> get stream => subject.stream;
BehaviorSubject<State> _subject;
State get value => _subject.value;
State get initialValue => null;
State get value => subject.value;
@override
bool get isBroadcast => _subject.isBroadcast;
@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,
);
}
// Methods for adding data to the _subject
@protected
void add(State value) {
if (_subject.isClosed) return;
_subject.add(value);
}
/// Helper method to reduce boilerplate when composing BLoC's together.
@protected
void addError(Object error, [StackTrace stackTrace]) {
if (_subject.isClosed) return;
_subject.addError(error, stackTrace);
}
// Helper method to reduce boilerplate when composing BLoC's together.
final _closers = <void Function()>[];
@protected
void cancelOnClose(StreamSubscription subscription,
[void Function() onClose]) {
......@@ -30,24 +57,9 @@ abstract class BlocStream<State> {
});
}
final _closers = <void Function()>[];
@mustCallSuper
Future<void> close() {
_closers.forEach((fn) => fn());
return subject.close();
return _subject.close();
}
StreamSubscription<State> listen(
void Function(State) onData, {
Function onError,
void Function() onDone,
bool cancelOnError,
}) =>
subject.listen(
onData,
onError: onError,
onDone: onDone,
cancelOnError: cancelOnError,
);
}
name: bloc_stream
description: A simple package that helps you to implement the BLoC pattern in your applications.
version: 2.1.0
version: 3.0.0
homepage: https://gitlab.com/arisechurch/bloc_stream
environment:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment