Dataflow mediator for ReactiveStreams
Library ID dataflow-mediator-reactivestreams
Latest version 1.0.0

Publisher to Attribute conversion

First of all, we'll explain how to convert a Publisher to an Attribute. Notice how we are not talking about Field here: the reason is that the Publisher can enter a state of error, that can only be handled by an Attribute.

If you wish, you can convert the returned Attribute to a simple Field using one of the many functions provided, depending on your needs.

The mediator provides an easy to use extension function to the Publisher, called toAttribute():

import com.femastudios.dataflow.async.* import com.femastudios.dataflow.async.util.* import com.femastudios.dataflow.listen.* import com.femastudios.dataflow.mediator.reactivestreams.* import io.reactivex.Flowable import org.reactivestreams.Publisher fun main() { val publisher : Publisher<Int> = Flowable.just(1, 2, 3) //Create a Publisher val attribute : Attribute<Int> = publisher.toAttribute() //Converts the Publisher to an Attribute println(attribute.value) lifecycle { listen(attribute) { println(it) } } println(attribute.value) }
Publisher<Integer> publisher = Flowable.just(1, 2, 3); //Create a Publisher Attribute<Integer> attribute = DataflowReactorMediation.toAttribute(publisher); //Converts the Publisher to an Attribute System.out.println(attribute.getValue()); LifecycleOwner.lifecycle(lc -> { lc.listen(attribute, value -> { System.out.println(value); }); }); System.out.println(attribute.getValue());

When the Attribute becomes active (has at least one listener), it will subscribe to the Publisher and update its value with the latest value emitted by the Publisher. When the Attribute becomes inactive (has not any listener), it will unsubscribe and keep the last seen value.

Until the attribute receives the first value, its status will be LOADING.

If the Publisher terminates in an error, the attribute state will be set to ERROR. More specifically, it will contain an instance of PublisherError: this class extends the standard attribute Error class and additionally contains the Throwable instance that triggered the Publisher error. Notice that since the Publisher cannot recover from an error state, neither can our attribute.