Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reactive] Fluent conversion via compose() & to() #1592

Merged
merged 1 commit into from
Mar 31, 2020

Conversation

akarnokd
Copy link
Collaborator

Since Java has no extension methods, it is generally inconvenient and ugly to apply custom operators to chains.

The closest thing is to add conversion methods that take a function and return the object to continue chaining onto.

compose

Take the current sequence and apply a function to it to produce the new sequence:

Function<Multi<T>, Multi<T>> addLogging = upstream -> 
     upstream
     .peek(Logger:log)
     .onError(Logger::log);

Function<Multi<T>, Multi<T>> addAsynchrony = upstream ->
      Global.isDebugging() 
          ? upstream.observeOn(Runnable::run) 
          : upstream.observeOn(backgroundExecutor);

source.map(mapping)
         .compose(addLogging)
         .compose(addAsynchrony);

to

Often though, the transformation should result in a type other than the base type. For this, use the to() method which can return any type:

Multi.range(1, 5)
    .map(Object::toString)
    .to(upstream -> {
        TestSubscriber<String> ts = new TestSubscriber<>();
        upstream.subscribe(ts);
        return ts;
    })
    .assertResult("1", "2", "3", "4", "5");

@danielkec danielkec self-assigned this Mar 27, 2020
@danielkec danielkec added the reactive Reactive streams and related components label Mar 27, 2020
@danielkec danielkec added this to the 2.0.0 milestone Mar 27, 2020
@danielkec danielkec merged commit 49dd04d into helidon-io:master Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reactive Reactive streams and related components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants