distinct_value_connectable_observable 2.0.2

distinct_value_connectable_observable #

Distinct & Connectable & ValueObservable RxDart

Build Status Pub

Dart package: https://pub.dartlang.org/packages/distinct_value_connectable_observable.

Useful for flutter BLoC pattern, expose broadcast state stream to UI, can synchronous access to the last emitted item, and distinct until changed

A library for Dart developers. Created from templates made available by Stagehand under a BSD-style license.

Implement BLoC #

Without using packageUsing package

Usage #

A simple usage example:

Import distinct_value_connectable_observable:

import 'package:distinct_value_connectable_observable/distinct_value_connectable_observable.dart';

Wrap your Stream or Observable in a DistinctValueConnectableObservable using constructor:

final Stream<State> state$;
final distinctValueConnectable$ = DistinctValueConnectableObservable(state$);

or

final Stream<State> state$;
final distinctValueConnectable$ = DistinctValueConnectableObservable.seeded(
  state$,
  seedValue: State.initial(),
);

You can pass equals parameter to constructor, used to determined equality:

final Stream<State> state$;
final bool Function(State, State) isEquals;

final distinctValueConnectable$ = DistinctValueConnectableObservable.seeded(
  state$,
  seedValue: State.initial(),
  equals: isEquals,
);

Another approach, using helper functions: publishValueDistinct, publishValueSeededDistinct, shareValueDistinct, shareValueSeededDistinct.

final source = Observable.fromIterable([1, 2, 2, 3, 3, 3]);
final connectable = publishValueDistinct(source);

// Does not print anything at first
connectable.listen(print);

// Start listening to the source Observable. Will cause the previous
// line to start printing 1, 2, 3
final subscription = connectable.connect();

// Late subscribers will receive the last emitted value
connectable.listen(print); // Prints 3

// Can access the latest emitted value synchronously. Prints 3
print(connectable.value);

// Stop emitting items from the source stream and close the underlying
// BehaviorSubject
await subscription.cancel();

Features and bugs #

Please file feature requests and bugs at the issue tracker.

License #

MIT License

Copyright (c) 2019 Petrus Nguyễn Thái Học

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2.0.2 #

  • Update rxdart dependency to version 0.22.1

2.0.1 #

  • Update rxdart dependency to version 0.22.0

2.0.0+2 #

  • Update README.md

2.0.0+1 #

  • Update README.md

2.0.0 #

  • Add new functions 💘💘💘 : publishValueDistinct, publishValueSeededDistinct, shareValueDistinct, shareValueSeededDistinct 🔥🔥🔥🔥🔥🔥.

1.0.0+3 #

  • Change meta version

1.0.0+2 #

  • Update RxDart package to ^0.21.0, DistinctValueConnectableObservable now has a separate factory constructor seeded(). This allows you to seed this stream with a null value.

1.0.0+1 #

  • Some refactors, update readme.md

1.0.0 #

  • Initial version, created by Stagehand

example/distinct_value_connectable_observable_example.dart

import 'package:distinct_value_connectable_observable/distinct_value_connectable_observable.dart';
import 'package:meta/meta.dart';
import 'package:rxdart/rxdart.dart';

class CounterBloc {
  ///
  /// Inputs
  ///
  final void Function(int) increment;
  final void Function(int) decrement;

  ///
  /// Outputs
  ///
  final ValueObservable<int> state;

  ///
  /// Clean up
  ///
  final void Function() dispose;

  CounterBloc._({
    @required this.increment,
    @required this.decrement,
    @required this.state,
    @required this.dispose,
  });

  factory CounterBloc() {
    final incrementController = PublishSubject<int>();
    final decrementController = PublishSubject<int>();

    final state$ = DistinctValueConnectableObservable.seeded(
      Observable.merge(<Stream<int>>[
        incrementController,
        decrementController.map((i) => -i),
      ]).scan<int>((acc, e, _) => acc + e, 0),
      seedValue: 0,
    );

    /// or:
    ///```
    /// final state$ = publishValueSeededDistinct(
    ///   Observable.merge(<Stream<int>>[
    ///     incrementController,
    ///     decrementController.map((i) => -i),
    ///   ]).scan<int>((acc, e, _) => acc + e, 0),
    ///   seedValue: 0,
    /// );
    /// ```
    final subscription = state$.connect();

    return CounterBloc._(
      increment: incrementController.add,
      decrement: decrementController.add,
      state: state$,
      dispose: () async {
        await subscription.cancel();
        await Future.wait(
            [incrementController, decrementController].map((c) => c.close()));
      },
    );
  }
}

main() async {
  var counterBloc = CounterBloc();
  print(counterBloc.state.value);

  var listen = counterBloc.state.listen((i) => print('[LOGGER] state=$i'));
  counterBloc.increment(0);
  counterBloc.increment(2);
  counterBloc.decrement(2);
  counterBloc.decrement(2);
  counterBloc.decrement(2);
  counterBloc.increment(2);
  counterBloc.increment(2);
  counterBloc.increment(0);
  counterBloc.increment(0);
  counterBloc.increment(0);
  counterBloc.increment(0);
  counterBloc.increment(0);

  await Future.delayed(Duration(seconds: 5));

  await listen.cancel();
  await counterBloc.dispose();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  distinct_value_connectable_observable: ^2.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:distinct_value_connectable_observable/distinct_value_connectable_observable.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
80
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

We analyzed this package on Aug 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:distinct_value_connectable_observable/distinct_value_connectable_observable.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
meta >=1.1.1 <2.0.0 1.1.7
rxdart ^0.22.1 0.22.1+1
Dev dependencies
mockito ^3.0.0
pedantic ^1.0.0
test ^1.0.0