-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publisher scanWith enhancements (#2640)
Motivation: Publisher scanWith operators only allow a limited set of mapped terminal transformations. Use cases like mapping state and preserving the onError Throwable aren't possible. Modifications: - Introduce ScanMapper and ScanLifetimeMapper which support MappedTerminal that allows all the terminal combinations to be mapped. - Deprecate ScanWithMapper and ScanWithLifetimeMapper in favor of these new APIs. - Deprecate Publisher.scanWith and Publisher.scanWithLiftime in favor of the new extension operators.
- Loading branch information
1 parent
7f0b795
commit d078d0d
Showing
15 changed files
with
603 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...cetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/ScanLifetimeMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright © 2023 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.concurrent.api; | ||
|
||
import io.servicetalk.concurrent.PublisherSource.Subscriber; | ||
import io.servicetalk.concurrent.PublisherSource.Subscription; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Provides the ability to transform (aka map) signals emitted via | ||
* the {@link Publisher#scanWithLifetimeMapper(Supplier)} operator, as well as the ability to cleanup state | ||
* via {@link #afterFinally}. | ||
* @param <T> Type of items emitted by the {@link Publisher} this operator is applied. | ||
* @param <R> Type of items emitted by this operator. | ||
*/ | ||
public interface ScanLifetimeMapper<T, R> extends ScanMapper<T, R> { | ||
/** | ||
* Invoked after a terminal signal {@link Subscriber#onError(Throwable)} or | ||
* {@link Subscriber#onComplete()} or {@link Subscription#cancel()}. | ||
* No further interaction will occur with the {@link ScanLifetimeMapper} to prevent use-after-free | ||
* on internal state. | ||
*/ | ||
void afterFinally(); | ||
} |
106 changes: 106 additions & 0 deletions
106
servicetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/ScanMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright © 2023 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.concurrent.api; | ||
|
||
import io.servicetalk.concurrent.PublisherSource.Subscriber; | ||
|
||
import java.util.function.Supplier; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Provides the ability to transform (aka map) signals emitted via the {@link Publisher#scanWithMapper(Supplier)} | ||
* operator. | ||
* @param <T> Type of items emitted by the {@link Publisher} this operator is applied. | ||
* @param <R> Type of items emitted by this operator. | ||
*/ | ||
public interface ScanMapper<T, R> { | ||
/** | ||
* Invoked on each {@link Subscriber#onNext(Object)} signal and maps from type {@link T} to type {@link R}. | ||
* @param next The next element emitted from {@link Subscriber#onNext(Object)}. | ||
* @return The result of mapping {@code next}. | ||
*/ | ||
@Nullable | ||
R mapOnNext(@Nullable T next); | ||
|
||
/** | ||
* Invoked when a {@link Subscriber#onError(Throwable)} signal is received and can map the current state into an | ||
* object of type {@link R} which will be emitted downstream as {@link Subscriber#onNext(Object)}, followed by | ||
* a terminal signal. | ||
* <p> | ||
* If this method throws the exception will be propagated downstream via {@link Subscriber#onError(Throwable)}. | ||
* @param cause The cause from upstream {@link Subscriber#onError(Throwable)}. | ||
* @return | ||
* <ul> | ||
* <li>{@code null} if no mapping is required and {@code cause} is propagated to | ||
* {@link Subscriber#onError(Throwable)}</li> | ||
* <li>non-{@code null} will propagate {@link MappedTerminal#onNext()} to {@link Subscriber#onNext(Object)} | ||
* then will terminate with {@link MappedTerminal#terminal()}</li> | ||
* </ul> | ||
* @throws Throwable If an exception occurs, which will be propagated downstream via | ||
* {@link Subscriber#onError(Throwable)}. | ||
*/ | ||
@Nullable | ||
MappedTerminal<R> mapOnError(Throwable cause) throws Throwable; | ||
|
||
/** | ||
* Invoked when a {@link Subscriber#onComplete()} signal is received and can map the current state into an | ||
* object of type {@link R} which will be emitted downstream as {@link Subscriber#onNext(Object)}, followed by | ||
* a terminal signal. | ||
* <p> | ||
* If this method throws the exception will be propagated downstream via {@link Subscriber#onError(Throwable)}. | ||
* @return | ||
* <ul> | ||
* <li>{@code null} if no mapping is required and {@code cause} is propagated to | ||
* {@link Subscriber#onError(Throwable)}</li> | ||
* <li>non-{@code null} will propagate {@link MappedTerminal#onNext()} to {@link Subscriber#onNext(Object)} | ||
* then will terminate with {@link MappedTerminal#terminal()}</li> | ||
* </ul> | ||
* @throws Throwable If an exception occurs, which will be propagated downstream via | ||
* {@link Subscriber#onError(Throwable)}. | ||
*/ | ||
@Nullable | ||
MappedTerminal<R> mapOnComplete() throws Throwable; | ||
|
||
/** | ||
* Result of a mapping operation of a terminal signal. | ||
* @param <R> The mapped result type. | ||
*/ | ||
interface MappedTerminal<R> { | ||
/** | ||
* Get the signal to be delivered to {@link Subscriber#onNext(Object)} if {@link #onNextValid()}. | ||
* @return the signal to be delivered to {@link Subscriber#onNext(Object)} if {@link #onNextValid()}. | ||
*/ | ||
@Nullable | ||
R onNext(); | ||
|
||
/** | ||
* Determine if {@link #onNext()} is valid and should be propagated downstream. | ||
* @return {@code true} to propagate {@link #onNext()}, {@code false} will only propagate {@link #terminal()}. | ||
*/ | ||
boolean onNextValid(); | ||
|
||
/** | ||
* The terminal event to propagate. | ||
* @return | ||
* <ul> | ||
* <li>{@code null} means {@link Subscriber#onComplete()}</li> | ||
* <li>non-{@code null} will propagate as {@link Subscriber#onError(Throwable)}</li> | ||
* </ul> | ||
*/ | ||
@Nullable | ||
Throwable terminal(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.