simple_observable 1.1.0+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 85

pub.dev

simple_observable #

Observe value changes using a Future, Stream, and/or a callback.

Source #

class Observable<T> {
  Observable({T initialValue, this.onChanged, this.checkEquality = true})
      : _value = initialValue;

  /// If true, setting the [value] will only notifiy if the new value is different
  /// than the current value.
  final bool checkEquality;
  final void Function(T value) onChanged;

  var _completer = Completer<T>();

  bool _canceled = false;
  bool get canceled => _canceled;

  T _value;

  /// The current value of this observable.
  T get value => _value;
  set value(T val) {
    if (!canceled && (!checkEquality || _value != val)) {
      _value = val;
      // Delaying notify() allows the Future and Stream to update correctly.
      Future.delayed(Duration(microseconds: 1), () => notify(val));
    }
  }

  /// Alias for [value] setter. Good for passing to a Future or Stream.
  void setValue(T val) => value = val;

  @protected
  @mustCallSuper
  void notify(T val) {
    if (onChanged != null) onChanged(val);
    // Completing with a microtask allows a new completer to be constructed
    // before listeners of [nextValue] are called, allowing them to listen to
    // nextValue again if desired.
    _completer.complete(Future.microtask(() => val));
    _completer = Completer<T>();
  }

  Future<T> get nextValue => _completer.future;
  Stream<T> get values async* {
    while (!canceled) {
      yield await nextValue;
    }
  }

  /// Permanently disables this observable. Further changes to [value] will be
  /// ignored, the outputs [onChanged], [nextValue], and [values] will not be
  /// called again.
  @mustCallSuper
  void cancel() => _canceled = true;
}

[1.1.0] - August 4, 2019

  • Rename SimpleObservable to just Observable.

[1.0.0] - August 4, 2019

  • Remove the Debouncer and Throttle classes.
  • Remove deprecations. This package will be a dependency of the debounce_throttle package instead. It has value as a stand alone package.

[0.3.1] - July 19, 2019

  • Fix Throttle.

[0.3.0+2] - July 19, 2019

  • Deprecate library in favor of new name, debounce_throttle.

[0.3.0] - July 19, 2019

  • Change argument list to allow passing an initial value.
  • Updated example.

[0.2.1] - July 19, 2019

  • Add [Throttle] class.

[0.2.0] - November 5, 2018

  • breaking: once canceled the observable/debouncer is no longer usable.
  • Moved cancel() to base class.
  • Return type of callback function is now void instead of dynamic.
  • Add more to ./example/main.dart.

[0.1.1] - October 22, 2018

  • Add setValue().
  • Put source code in readme.

[0.1.0] - October 22, 2018

  • First release.

example/main.dart

import 'package:simple_observable/simple_observable.dart';

// See also https://pub.dev/packages/debounce_throttle

void main() {
  final obs = Observable(initialValue: 0, onChanged: printCallback);
  printFuture(obs);
  obs.values.listen(printStream);

  obs.value = 1;
  obs.value = 2;
  obs.value = 3;

  while (obs.value < 10) obs.value += 1;
}

void printCallback(int value) => print('Callback: $value');
void printFuture(Observable obs) => obs.nextValue.then((value) {
      print('Future: $value');
      printFuture(obs);
    });
void printStream(int value) => print('Stream: $value');

// Output:
//
// Callback: 1
// Future: 1
// Stream: 1
// Callback: 2
// Future: 2
// Stream: 2
// Callback: 3
// Future: 3
// Stream: 3
// Callback: 4
// Future: 4
// Stream: 4
// Callback: 5
// Future: 5
// Stream: 5
// Callback: 6
// Future: 6
// Stream: 6
// Callback: 7
// Future: 7
// Stream: 7
// Callback: 8
// Future: 8
// Stream: 8
// Callback: 9
// Future: 9
// Stream: 9
// Callback: 10
// Future: 10
// Stream: 10

Use this package as a library

1. Depend on it

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


dependencies:
  simple_observable: ^1.1.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:simple_observable/simple_observable.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
70
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]
85
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.0.0 1.1.7
Dev dependencies
test ^1.0.0