ReactiveComponent mixin

A mixin that represents ReactiveComponent.

ReactiveComponent is a unit for stream-based reactive programming, that encapsulates its members and publicizes only Sink and Stream interfaces.

It can be a delegate to its instance members of ReactiveResource via disposer, for disposing of their resources together.

Deliberately, it is designed as mixin, so that a subclass can freely inherit from other class as necessary.

Simple example

class Counter with ReactiveComponent {
  Counter(this._initialCount);
  final int _initialCount;

  VoidReactiveSink _increment;
  VoidReactiveSink get increment => _increment ??= VoidReactiveSink((_) {
        _count.data++;
      }, disposer: disposer);

  Reactive<int> __count;
  Reactive<int> get _count => __count ??= Reactive<int>(_initialCount,
      disposer: disposer);
  Stream<int> get count => _count.stream;
}

final counter = Counter(0);

// Initial data is delivered immediately.
print(counter.count.first.then(print)); // prints 0.

// Add null data to the `increment` Sink.
// This is shorthand notation of `counter.increment.add(null);`.
counter.increment();

// Multiple listeners are allowed. The latest data is immediately delivered
// on each listen.
print(await counter.count.listen(print)); // prints 1.
print(await counter.count.listen(print)); // prints 1.

// Add null data to the `dispose` Sink.
// This is shorthand notation of `counter.dispose.add(null);`.
// The all resources of the component (`_increment` and `__count`)
// will be disposed of together.
counter.dispose();

It utilizes lazy initialization technique ( "??=" ) so that the two instance members can access the other members on their callback functions.

With late notation in null-safety feature, Counter will be conciser as below,

class Counter with ReactiveComponent {
  Counter(this._initialCount);
  final int _initialCount;

  late final VoidReactiveSink increment = VoidReactiveSink((_) {
        _count.data++;
      }, disposer: disposer);

  late final _count = Reactive<int>(_initialCount, disposer: disposer);
  Stream<int> get count => _count.stream;
}

More examples are listed on this package's example/ directory, including complex composed components, and composing Firebase.

https://github.com/polyflection/reactive_component/tree/master/example

Superclass Constraints
Implements
Annotations

Constructors

ReactiveComponent()

Properties

dispose → VoidSink
A VoidSink to dispose of the resources.
read-only, override
disposed Stream<void>
A stream to notify the resource has been disposed of.
read-only, override
disposer → ResourceDisposer
A subject of disposing the resources. [...]
@protected, read-only, override
isDisposeEventSent bool
Check whether an event data has been added to dispose sink once. [...]
@protected, read-only, override
hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

doDispose() Future<void>
Dispose its own resources. [...]
@protected, override
onDispose() → void
A synchronous callback on adding event data to dispose. [...]
@protected, override
delegateDisposingTo(ResourceDisposer disposerDelegate) → void
Delegates its dispose call to disposerDelegate. [...]
@protected, override
toString() String
A string representation of this object. [...]
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited

Operators

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