Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
2.0.0 (#15)
Browse files Browse the repository at this point in the history
* 2.0.0

* 2.0.0

* fix CI
  • Loading branch information
hoc081098 committed Sep 13, 2021
1 parent 8465fc6 commit 6dfbe2d
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 339 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/dart.yml
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: google/dart:2.12
image: google/dart:latest

steps:
- uses: actions/checkout@v2
Expand All @@ -24,7 +24,7 @@ jobs:
run: dart analyze --fatal-infos --fatal-warnings

- name: Format code
run: dartfmt -n ./lib ./test --set-exit-if-changed
run: dart format . --set-exit-if-changed

- name: Active coverage
run: pub global activate coverage
Expand All @@ -41,4 +41,4 @@ jobs:
- name: Format coverage
run: pub global run coverage:format_coverage --lcov --in=coverage.json --out=lcov.info --packages=.packages --report-on=lib

- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v2.1.0
1 change: 1 addition & 0 deletions AUTHORS
@@ -0,0 +1 @@
Petrus Nguy峄卬 Th谩i H峄峜 <hoc081098@gmail.com>
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,12 @@
## 2.0.0 - Sep 13, 2021

- **DEPRECATED**. This package is now merged into [rxdart_ext](https://pub.dev/packages/rxdart_ext) package. Please use [rxdart_ext](https://pub.dev/packages/rxdart_ext) package for the same purpose, thanks.
- `DistinctValueSubject` -> `StateSubject`.
- `DistinctValueConnectableStream` -> `StateConnectableStream`.
- `publishValueDistinct` -> `publishState`.
- `shareValueDistinct` -> `shareState`.
- `distinctValue` -> `toStateStream`.

## 1.3.0 - May 9, 2021

- Update `rxdart` to `0.27.0`.
Expand Down
10 changes: 6 additions & 4 deletions README.md
@@ -1,5 +1,7 @@
# distinct_value_connectable_stream <img src="https://avatars3.githubusercontent.com/u/6407041?s=200&v=4" width="32">

# **DEPRECATED**. This package is now merged into [rxdart_ext](https://pub.dev/packages/rxdart_ext) package. Please use [rxdart_ext](https://pub.dev/packages/rxdart_ext) package for the same purpose, thanks.

- `Distinct` & `Connectable` & `ValueStream` RxDart Stream.
- Useful for flutter `BLoC pattern` - `StreamBuilder`.

Expand Down Expand Up @@ -59,15 +61,15 @@
[comment]: <> (## Implement BLoC)

[comment]: <> ( ### Without using package)

[comment]: <> ( <p align="center">)

[comment]: <> ( <img src="https://github.com/hoc081098/distinct_value_connectable_stream/raw/master/bloc1.png" width="480"/>)

[comment]: <> ( </p>)

[comment]: <> ( ### Using package)

[comment]: <> ( <p align="center">)

[comment]: <> ( <img src="https://github.com/hoc081098/distinct_value_connectable_stream/raw/master/bloc2.png" width="480"/>)
Expand All @@ -89,7 +91,7 @@ final distinctState$ = state$.publishValueDistinct(UiState.initial());
distinctState$.connect();
StreamBuilder<UiState>(
initialData: distinctState$.requireValue,
initialData: distinctState$.value,
stream: distinctState$,
builder: (context, snapshot) {
final UiState state = snapshot.requireData;
Expand Down
2 changes: 2 additions & 0 deletions example/distinct_value_connectable_stream_example.dart
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'dart:async';

import 'package:distinct_value_connectable_stream/distinct_value_connectable_stream.dart';
Expand Down
24 changes: 22 additions & 2 deletions lib/src/as_broadcast.dart
@@ -1,8 +1,11 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'dart:async';

import 'package:rxdart_ext/rxdart_ext.dart';

import 'distinct_value_connectable_stream.dart';
import 'distinct_value_stream.dart';
import 'distinct_value_stream_mixin.dart';

/// Convert a [DistinctValueStream] to a broadcast [DistinctValueStream].
extension BroadcastDistinctValueStreamExtensions<T> on DistinctValueStream<T> {
Expand Down Expand Up @@ -35,7 +38,6 @@ extension BroadcastDistinctValueStreamExtensions<T> on DistinctValueStream<T> {
}

class _AsBroadcastStream<T> extends StreamView<T>
with DistinctValueStreamMixin<T>
implements DistinctValueStream<T> {
final DistinctValueStream<T> source;

Expand All @@ -47,4 +49,22 @@ class _AsBroadcastStream<T> extends StreamView<T>

@override
T get value => source.value;

@override
Never get error => throw ValueStreamError.hasNoError();

@override
Null get errorOrNull => null;

@override
bool get hasError => false;

@override
Null get stackTrace => null;

@override
bool get hasValue => true;

@override
T get valueOrNull => value;
}
129 changes: 12 additions & 117 deletions lib/src/distinct_value_connectable_stream.dart
@@ -1,126 +1,18 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'dart:async';

import 'package:meta/meta.dart';
import 'package:rxdart_ext/rxdart_ext.dart'
show
ConnectableStream,
ConnectableStreamSubscription,
ValueStream,
ValueSubject;
import 'package:rxdart_ext/rxdart_ext.dart';

import 'distinct_value_stream.dart';
import 'distinct_value_stream_mixin.dart';
import 'distinct_value_subject.dart';

/// A [ConnectableStream] that converts a single-subscription Stream into
/// a broadcast [Stream], and provides synchronous access to the latest emitted value.
///
/// This is a combine of [ConnectableStream], [ValueStream], [ValueSubject] and [Stream.distinct].
@sealed
abstract class DistinctValueConnectableStream<T> extends ConnectableStream<T>
implements DistinctValueStream<T> {
DistinctValueConnectableStream._(Stream<T> stream) : super(stream);

/// Constructs a [Stream] which only begins emitting events when
/// the [connect] method is called, this [Stream] acts like a
/// [ValueSubject] and distinct until changed.
///
/// Data events are skipped if they are equal to the previous data event.
/// Equality is determined by the provided [equals] method. If that is omitted,
/// the '==' operator on the last provided data element is used.
factory DistinctValueConnectableStream(
Stream<T> source,
T seedValue, {
bool Function(T previous, T next)? equals,
bool sync = true,
}) =>
_DistinctValueConnectableStream<T>._(
source,
DistinctValueSubject(seedValue, sync: sync, equals: equals),
equals,
);

@override
DistinctValueStream<T> autoConnect(
{void Function(StreamSubscription<T> subscription)? connection});

@override
StreamSubscription<T> connect();

@override
DistinctValueStream<T> refCount();
}

class _DistinctValueConnectableStream<T>
extends DistinctValueConnectableStream<T> with DistinctValueStreamMixin<T> {
final Stream<T> _source;
final DistinctValueSubject<T> _subject;
var _used = false;

@override
final bool Function(T, T) equals;

_DistinctValueConnectableStream._(
this._source,
this._subject,
bool Function(T, T)? equals,
) : equals = equals ?? DistinctValueStream.defaultEquals,
super._(_subject);

late final _connection = ConnectableStreamSubscription<T>(
_source.listen(
_subject.add,
onError: null,
onDone: _subject.close,
),
_subject,
);

void _checkUsed() {
if (_used) {
throw StateError('Cannot reuse this stream. This causes many problems.');
}
_used = true;
}

@override
DistinctValueStream<T> autoConnect({
void Function(StreamSubscription<T> subscription)? connection,
}) {
_checkUsed();

_subject.onListen = () {
final subscription = _connection;
connection?.call(subscription);
};
_subject.onCancel = null;

return this;
}

@override
StreamSubscription<T> connect() {
_checkUsed();

_subject.onListen = _subject.onCancel = null;
return _connection;
}

@override
DistinctValueStream<T> refCount() {
_checkUsed();

ConnectableStreamSubscription<T>? subscription;

_subject.onListen = () => subscription = _connection;
_subject.onCancel = () => subscription?.cancel();

return this;
}

@override
T get value => _subject.value;
}
@Deprecated(
"Use StateConnectableStream from 'rxdart_ext' package instead. This package is deprecated!")
typedef DistinctValueConnectableStream<T> = StateConnectableStream<T>;

/// Provide two extension methods for [Stream]:
/// - [publishValueDistinct]
Expand Down Expand Up @@ -157,13 +49,14 @@ extension DistinctValueConnectableExtensions<T> on Stream<T> {
/// // ValueSubject
/// subscription.cancel();
/// ```
@Deprecated(
"Use StateConnectableExtensions.publishState from 'rxdart_ext' package instead. This package is deprecated!")
DistinctValueConnectableStream<T> publishValueDistinct(
T seedValue, {
bool Function(T previous, T next)? equals,
bool sync = true,
}) =>
DistinctValueConnectableStream<T>(this, seedValue,
equals: equals, sync: sync);
publishState(seedValue, equals: equals, sync: sync);

/// Convert the this Stream into a new [DistinctValueStream] that can
/// be listened to multiple times, providing an initial value.
Expand Down Expand Up @@ -197,10 +90,12 @@ extension DistinctValueConnectableExtensions<T> on Stream<T> {
/// subscription.cancel();
/// subscription2.cancel();
/// ```
@Deprecated(
"Use StateConnectableExtensions.shareState from 'rxdart_ext' package instead. This package is deprecated!")
DistinctValueStream<T> shareValueDistinct(
T seedValue, {
bool Function(T previous, T next)? equals,
bool sync = true,
}) =>
publishValueDistinct(seedValue, equals: equals, sync: sync).refCount();
shareState(seedValue, equals: equals, sync: sync);
}

0 comments on commit 6dfbe2d

Please sign in to comment.