Consumer

abstract class Consumer[-In, +R] extends Observable[In] => Task[R] with Serializable

The Consumer is a specification of how to consume an observable.

The Consumer is a specification of how to consume an observable.

It is a factory of subscribers with a completion callback attached, being effectively a way to transform observables into tasks for less error prone consuming of streams.

Companion
object
trait Serializable
trait Observable[In] => Task[R]
class Object
trait Matchable
class Any
trait Sync[In, R]

Value members

Abstract methods

def createSubscriber(cb: Callback[Throwable, R], s: Scheduler): (Subscriber[In], AssignableCancelable)

Builds a new Subscriber that can be subscribed to an Observable for consuming a stream, with a callback that should eventually get called with a materialized result.

Builds a new Subscriber that can be subscribed to an Observable for consuming a stream, with a callback that should eventually get called with a materialized result.

Notes:

  • calling the callback must obey the contract for the Callback type
  • the given callback should always get called, unless the upstream gets canceled
  • the given callback can be called when the subscriber is finished processing, but not necessarily
  • if the given callback isn't called after the subscriber is done processing, then the Task returned by apply loses the ability to cancel the stream, as that Task will complete before the stream is finished
Value Params
cb

is the Callback that will get called once the created subscriber is finished.

s

is the Scheduler that will get used for subscribing to the source observable and to process the events.

Returns

a new subscriber that can be used to consume observables.

Concrete methods

final def apply(source: Observable[In]): Task[R]

Given a source Observable, convert it into a Task by piggybacking on createSubscriber.

Given a source Observable, convert it into a Task by piggybacking on createSubscriber.

final def contramap[In2](f: In2 => In): Consumer[In2, R]

Given a contravariant mapping function, transform the source consumer by transforming the input.

Given a contravariant mapping function, transform the source consumer by transforming the input.

final def map[R2](f: R => R2): Consumer[In, R2]

Given a mapping function, when consuming a stream, applies the mapping function to the final result, thus modifying the output of the source consumer.

Given a mapping function, when consuming a stream, applies the mapping function to the final result, thus modifying the output of the source consumer.

Note that for applying the mapping function an asynchronous boundary is forced, otherwise it could trigger a stack overflow exception. For more efficient mapping of the result, it's probably better to map the resulting Task on Observable.consumeWith.

See also

mapTask for a variant that can map the output to a Task that can be processed asynchronously.

final def mapEval[F[_], R2](f: R => F[R2])(F: TaskLike[F]): Consumer[In, R2]

Given a mapping function, when consuming a stream, applies the mapping function to the final result, thus modifying the output of the source consumer.

Given a mapping function, when consuming a stream, applies the mapping function to the final result, thus modifying the output of the source consumer.

The mapping function returns results using a generic F[_] data type that must implement the cats.effect.Effect type class. Examples of such classes are cats.effect.IO and monix.eval.Task, thus being able to do asynchronous processing.

See mapTask for the version that's specialized on Task.

final def mapTask[R2](f: R => Task[R2]): Consumer[In, R2]

Given a mapping function, when consuming a stream, applies the mapping function to the final result, thus modifying the output of the source consumer.

Given a mapping function, when consuming a stream, applies the mapping function to the final result, thus modifying the output of the source consumer.

The mapping function returns a Task that can be used to process results asynchronously.

Note that for applying the mapping function an asynchronous boundary is forced, otherwise it could trigger a stack overflow exception. For more efficient mapping of the result, it's probably better to map the resulting Task on Observable.consumeWith.

See mapEval for the version that can work with any data type that implements cats.effect.Effect.

final def transformInput[In2](f: Observable[In2] => Observable[In]): Consumer[In2, R]

Given a function that transforms the input stream, uses it to transform the source consumer into one that accepts events of the type specified by the transformation function.

Given a function that transforms the input stream, uses it to transform the source consumer into one that accepts events of the type specified by the transformation function.

Inherited methods

@unspecialized
def andThen[A](g: Task[R] => A): Observable[In] => A
Inherited from
Function1
@unspecialized
def compose[A](g: A => Observable[In]): A => Task[R]
Inherited from
Function1
override def toString(): String
Definition Classes
Function1 -> Any
Inherited from
Function1