interleave

Emits a specifiable number of elements from the original source, then from the provided source and repeats.

Fan-in operators

Signature

Source.interleaveSource.interleave Flow.interleaveFlow.interleave

Description

Emits a specifiable number of elements from the original source, then from the provided source and repeats. If one source completes the rest of the other stream will be emitted.

Example

Scala
sourceimport org.apache.pekko
import pekko.stream.scaladsl.Sink
import pekko.stream.scaladsl.Source

val sourceA = Source(List(1, 2, 3, 4))
val sourceB = Source(List(10, 20, 30, 40))

sourceA.interleave(sourceB, segmentSize = 2).runWith(Sink.foreach(println))
// prints 1, 2, 10, 20, 3, 4, 30, 40
Java
sourceimport org.apache.pekko.stream.javadsl.Keep;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.stream.javadsl.Sink;

import java.util.*;

Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.interleave(sourceB, 2).runForeach(System.out::println, system);
// prints 1, 2, 10, 20, 3, 4, 30, 40

Reactive Streams semantics

emits when element is available from the currently consumed upstream

backpressures when upstream backpressures

completes when both upstreams have completed