How to use alsoTo

Hi,

I would expect that the completion stage “b” completes in the example below. But it does not and “a” does. How do i get “b” to complete?

        ActorSystem system = ActorSystem.create();

        Sink<String, CompletionStage<List<String>>> seq = Flow.<String>create().log("second").toMat(Sink.seq(), Keep.right());
        CompletionStage<List<String>> b = seq.preMaterialize(system).first();
        CompletionStage<List<String>> a = Source.from(Arrays.asList("a", "b", "c"))
                .alsoTo(seq)
                .log("first")
                .toMat(Sink.seq(), Keep.right())
                .run(system);

        a.thenAccept(System.out::println);
        b.thenAccept(System.out::println);

Hi @flo,

I think the problem is that in alsoTo you use seq instead of using the Sink provided by preMaterialize. From the preMaterialize docs:

   * Materializes this Sink, immediately returning (1) its materialized value, and (2) a new Sink
   * that can be consume elements 'into' the pre-materialized one.
   *
   * Useful for when you need a materialized value of a Sink when handing it out to someone to materialize it for you.
   *
   * Note that the `ActorSystem` can be used as the `systemProvider` parameter.

So, while b is the first element of the resulting pair after invoking preMaterialize you are ignoring the second element of the pair which is the Sink. Try the following instead:

    Sink<String, CompletionStage<List<String>>> seq = Flow.<String>create().log("second").toMat(Sink.seq(), Keep.right());
    Pair<CompletionStage<List<String>>, Sink<String, NotUsed>> pair = seq.preMaterialize(system);
    CompletionStage<List<String>> b = pair.first();
    CompletionStage<List<String>> a = Source.from(Arrays.asList("a", "b", "c"))
            .alsoTo(pair.second())
            .log("first")
            .toMat(Sink.seq(), Keep.right())
            .run(system);

    a.thenAccept(System.out::println);
    b.thenAccept(System.out::println).thenAccept( x -> System.out.println("B is done."));

/* outputs:
[a, b, c]
[a, b, c]
B is done.
*/

Cheers,

I see, thank you!