Observation<O extends Observable<T>, T> class abstract

Observation is the observe action that an observer observes an observable.

Observation ~ ObservAtion ~ Observe Action is often happened when there is an intention to connect Observable and Observer.

Example:

Part 1/2:

void main() {
  final observable = ObservableFromIterable<int>([1, 2, 3]);
  final observer = Observer<int>((data) {
    print('onData: $data');
  });
  final observation = observable.observe(observer.onData);
  observation.dispose();
}

Prints:

onData: 1
onData: 2
onData: 3
  • Observable is the logical configuration describe what items to be produced.
  • Observation apply Observable's logical configuration and produces those items then delivery to Observer.
  • Observer handles those items.

Part 2/2:

Implementation of ObservableFromIterable with its Observation.

class ObservableFromIterable<T> implements Observable<T> {

  ObservableFromIterable(this.iterable);

  final Iterable<T> iterable;

  @override
  Disposable observe(OnData<T> onData) {
    return _Observation(    // Make the connection between
      configuration: this,  // the observable (configuration)
      emit: onData,         // and observer   (emit)
    );
  }
}

class _Observation<T> extends Observation<ObservableFromIterable<T>, T> {
  
  _Observation({
    required super.configuration, 
    required super.emit
  });

  Iterator<T>? _iterator; 

  @override
  void init() {
    _iterator = configuration.iterable.iterator;
    _iterate(_iterator!);
  }

  void _iterate(Iterator<T> iterator) {
    while (iterator.moveNext()) {
      emit(iterator.current);
    }
  }

  @override
  void dispose() {
    _iterator = null;
  }
}
Implemented types
Available Extensions

Constructors

Observation({required O configuration, required OnData<T> emit})
Create Observation with configuration and emit.

Properties

configuration → O
Observable is the configuration.
final
emit OnData<T>
Observer is emit.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

dispose() → void
inherited
init() → void
Initialize the observation.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited