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 akka.stream.scaladsl.Sink
import akka.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 akka.stream.javadsl.Keep;
import akka.stream.javadsl.Source;
import akka.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

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.