SourceCompanionOps

ox.channels.SourceCompanionOps

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
object Source.type

Members list

Value members

Concrete methods

def concat[T](sources: Seq[() => Source[T]])(using Ox, StageCapacity): Source[T]
def empty[T]: Source[T]
def failed[T](t: Throwable): Source[T]

Creates a source that fails immediately with the given java.lang.Throwable

Creates a source that fails immediately with the given java.lang.Throwable

Value parameters

t

The java.lang.Throwable to fail with

Attributes

Returns

A source that would fail immediately with the given java.lang.Throwable

def fromFork[T](f: Fork[T])(using Ox, StageCapacity): Source[T]
def fromIterable[T](it: Iterable[T])(using Ox, StageCapacity): Source[T]
def fromIterator[T](it: => Iterator[T])(using Ox, StageCapacity): Source[T]
def fromValues[T](ts: T*)(using Ox, StageCapacity): Source[T]
def future[T](from: Future[T])(using StageCapacity, ExecutionContext): Source[T]

Creates a source that emits a single value when from completes or fails otherwise. The from completion is performed on the provided scala.concurrent.ExecutionContext. Note that when from fails with scala.concurrent.ExecutionException then its cause is returned as source failure.

Creates a source that emits a single value when from completes or fails otherwise. The from completion is performed on the provided scala.concurrent.ExecutionContext. Note that when from fails with scala.concurrent.ExecutionException then its cause is returned as source failure.

Value parameters

from

A scala.concurrent.Future that returns value upon completion.

Attributes

Returns

A source that will emit value upon a from scala.concurrent.Future completion.

Example
 import ox.*
 import ox.channels.Source
 import scala.concurrent.ExecutionContext.Implicits.global
 import scala.concurrent.Future
 supervised {
   Source
     .future(Future.failed(new RuntimeException("future failed")))
     .receive()                               // ChannelClosed.Error(java.lang.RuntimeException: future failed)
   Source.future(Future.successful(1)).toList // List(1)
 }
def futureSource[T](from: Future[Source[T]])(using Ox, StageCapacity, ExecutionContext): Source[T]

Creates a source that emits elements from future source when from completes or fails otherwise. The from completion is performed on the provided scala.concurrent.ExecutionContext whereas elements are emitted through Ox. Note that when from fails with scala.concurrent.ExecutionException then its cause is returned as source failure.

Creates a source that emits elements from future source when from completes or fails otherwise. The from completion is performed on the provided scala.concurrent.ExecutionContext whereas elements are emitted through Ox. Note that when from fails with scala.concurrent.ExecutionException then its cause is returned as source failure.

Value parameters

from

A scala.concurrent.Future that returns source upon completion.

Attributes

Returns

A source that will emit values upon a from scala.concurrent.Future completion.

Example
 import ox.*
 import ox.channels.Source
 import scala.concurrent.ExecutionContext.Implicits.global
 import scala.concurrent.Future
 supervised {
   Source
     .futureSource(Future.failed(new RuntimeException("future failed")))
     .receive()                                                           // ChannelClosed.Error(java.lang.RuntimeException: future failed)
   Source.futureSource(Future.successful(Source.fromValues(1, 2))).toList // List(1, 2)
 }
def interleaveAll[T](sources: Seq[Source[T]], segmentSize: Int, eagerComplete: Boolean)(using Ox, StageCapacity): Source[T]

Sends a given number of elements (determined byc segmentSize) from each source in sources to the returned channel and repeats. The order of elements in all sources is preserved.

Sends a given number of elements (determined byc segmentSize) from each source in sources to the returned channel and repeats. The order of elements in all sources is preserved.

If any of the sources is done before the others, the behavior depends on the eagerCancel flag. When set to true, the returned channel is completed immediately, otherwise the interleaving continues with the remaining non-completed sources. Once all but one sources are complete, the elements of the remaining non-complete source are sent to the returned channel.

Must be run within a scope, since a child fork is created which receives from the subsequent sources and sends to the resulting channel.

Value parameters

eagerComplete

If true, the returned channel is completed as soon as any of the sources completes. If 'false`, the interleaving continues with the remaining non-completed sources.

segmentSize

The number of elements sent from each source before switching to the next one. Default is 1.

sources

The sources whose elements will be interleaved.

Attributes

Returns

A source to which the interleaved elements from both sources would be sent.

Example
 scala>
 import ox.*
 import ox.channels.Source
 supervised {
   val s1 = Source.fromValues(1, 2, 3, 4, 5, 6, 7, 8)
   val s2 = Source.fromValues(10, 20, 30)
   val s3 = Source.fromValues(100, 200, 300, 400, 500)
   Source.interleaveAll(List(s1, s2, s3), segmentSize = 2, eagerComplete = true).toList
 }
 scala> val res0: List[Int] = List(1, 2, 10, 20, 100, 200, 3, 4, 30)
def iterate[T](zero: T)(f: T => T)(using Ox, StageCapacity): Source[T]
def range(from: Int, to: Int, step: Int)(using Ox, StageCapacity): Source[Int]

A range of number, from from, to to (inclusive), stepped by step.

A range of number, from from, to to (inclusive), stepped by step.

Attributes

def repeat[T](element: T)(using Ox, StageCapacity): Source[T]
def tick[T](interval: FiniteDuration, element: T)(using Ox, StageCapacity): Source[T]
def timeout[T](interval: FiniteDuration, element: T)(using Ox, StageCapacity): Source[T]
def unfold[S, T](initial: S)(f: S => Option[(T, S)])(using Ox, StageCapacity): Source[T]