Reactive<D> class

A special kind of StreamController that holds its latest stream data, and sends that as the first data to any new listener.

Reactive stream is a multi-subscription stream, added at Dart version 2.9.0. This allows multiple subscription, and each listener to be treated as an individual stream.

Reactive's data can get and set data synchronously. When a new data is set, it will be sent to all listeners of Reactive's stream immediately.

Reactive only supports asynchronous stream for now. Synchronous stream option could be added when some use case is reported.

Example

final aReactiveInt = Reactive<int>(0, disposer: null);

aReactiveInt.data = 1;
aReactiveInt.stream.listen(print); // prints 1 2 3.
aReactiveInt.data = 2;
aReactiveInt.stream.listen(print); // prints 2 3.
aReactiveInt.data = 3;
aReactiveInt.stream.listen(print); // prints 3.
print(aReactiveInt.data); // prints 3.

Disposing its resource, or delegating to other ReactiveResource.

Reactive is a kind of ReactiveResource, which provides resource disposing action by dispose sink. dispose action can be delegated to other ReactiveResource, typically a ReactiveComponent. See ReactiveComponent for more explanation about delegating dispose.

Reactive data should be encapsulated in a ReactiveComponent.

Reactive is strongly encouraged to be a private member of ReactiveComponent to hide its data mutating and other behaviors. Only a stream of a Reactive data should be publicized. Static analysis support is planned to prevent Reactive from being publicized.

class C with ReactiveComponent {
  Reactive<int> __aReactiveInt;
  Reactive<int> get _aReactiveInt =
      Reactive<int>(0, disposer: disposer);

  Stream<int> get anIntStream => _aReactiveInt.stream;
}

In current Dart spec, a lazy initialization technique with "??=" let them access the other instance members at its callback functions.

When "null-safety" is available in a future Dart, thankfully the notation will be conciser as below,

class C with ReactiveComponent {
  late final Reactive<int> _aReactiveInt =
      Reactive<int>(0, disposer: disposer);

  Stream<int> get anIntStream => _aReactiveInt.stream;
}
Implemented types
Mixed in types

Constructors

Reactive(D _data, {@required ResourceDisposer disposer, VoidCallback onDispose, VoidCallback onListen, VoidCallback onPause, VoidCallback onResume, FutureOrVoidCallback onCancel})
Creates a Reactive data with its initial data, and optional parameters of disposer and callbacks. [...]

Properties

data ↔ D
The latest data of this stream controller.
read / write
dispose → VoidSink
A VoidSink to dispose of the resources.
read-only, inherited
disposed Stream<void>
A stream to notify the resource has been disposed of.
read-only, inherited
disposer → ResourceDisposer
A subject of disposing the resources. [...]
@protected, read-only, inherited
done Future<void>
A future which is completed when the stream controller is done sending events. [...]
read-only, override
hashCode int
The hash code for this object. [...]
read-only, inherited
hasListener bool
Whether there is a subscriber on the Stream.
read-only, override
isClosed bool
Whether the stream controller is closed for adding more events. [...]
read-only, override
isDisposeEventSent bool
Check whether an event data has been added to dispose sink once. [...]
@protected, read-only, inherited
isPaused bool
Whether the subscription would need to buffer events. [...]
read-only, override
onCancel FutureOrVoidCallback
The callback which is called when the stream is canceled. [...]
read / write, override
onListen ↔ VoidCallback
The callback which is called when the stream is listened to. [...]
read / write, override
onPause ↔ VoidCallback
The callback which is called when the stream is paused. [...]
read / write, override
onResume ↔ VoidCallback
The callback which is called when the stream is resumed. [...]
read / write, override
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
sink StreamSink<D>
Returns a view of this object that only publicizes the StreamSink interface.
read-only, override
stream Stream<D>
The stream that this controller is controlling. [...]
read-only, override

Methods

add(D newData) → void
Alias method of data setter.
override
addError(Object error, [StackTrace stackTrace]) → void
Sends or enqueues an error event. [...]
override
addStream(Stream<D> source, {bool cancelOnError}) Future<void>
Receives events from source and puts them into this controller's stream. [...]
override
close() Future<void>
Closes the stream. [...]
override
delegateDisposingTo(ResourceDisposer disposerDelegate) → void
Delegates its dispose call to disposerDelegate. [...]
@protected, inherited
doDispose() Future<void>
Dispose its own resources. [...]
@protected, override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
onDispose() → void
A synchronous callback on adding event data to dispose. [...]
@protected, override
toString() String
A string representation of this object. [...]
inherited

Operators

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