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

Deprecate awaitSingleOr*, specialize some await* functions for Mono and Maybe #2628

Merged
merged 9 commits into from
Apr 21, 2021

Conversation

dkhalanskyjb
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb commented Apr 5, 2021

This PR

  • Deprecates awaitSingleOr* on arbitrary Publishers
  • Adds specialized awaitSingle and awaitSingleOrNull methods on Maybe<T> and Mono<T>
  • Deprecates Maybe<T>.await() in favor of Maybe<T>.awaitSingleOrNull()
  • Adds specializations of most of the await* methods for Mono<T> and deprecates them, as the only useful methods on Mono<T> are awaitSingle and awaitSingleOrNull
  • Rewords some documentation for await* methods

Fixes #2591
Fixes #1587

@qwwdfsad qwwdfsad self-requested a review April 6, 2021 13:06
@dkhalanskyjb dkhalanskyjb changed the title Make awaitSingleOrNull() consistent with singleOrNull() Deprecate awaitSingleOr*, specialize some await* functions for Mono and Maybe Apr 16, 2021
@dkhalanskyjb dkhalanskyjb mentioned this pull request Apr 16, 2021
reactive/kotlinx-coroutines-reactive/src/Await.kt Outdated Show resolved Hide resolved
message = "Deprecated without a replacement due to its name incorrectly conveying the behavior. " +
"Please consider using awaitFirstOrDefault().",
level = DeprecationLevel.WARNING
)
public suspend fun <T> Publisher<T>.awaitSingleOrDefault(default: T): T = awaitOne(Mode.SINGLE_OR_DEFAULT, default)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things regarding deprecation:

  1. It's worth adding ### Deprecation section to KDoc with an explanation of why this function is deprecated and, if necessary, why there is no built-in replacement. The rest of KDoc can be removed, but not hard opinion here.
  2. To simplify maintenance and user's view of deprecation timeline, we add a short note about it:
@Deprecated(
    message = "Deprecated without a replacement due to its name incorrectly conveying the behavior. " +
        "Please consider using awaitFirstOrDefault().",
    level = DeprecationLevel.WARNING
) // Warning since 1.5, error in 1.6, hidden in 1.7


override fun onComplete() {
if (!seenValue)
cont.resume(null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] I'd suggest sticking to either one-liners or using explicit scoping with {}

Copy link
Member

@qwwdfsad qwwdfsad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Please address minor doc issues and it's good to go

*/
@Deprecated(
message = "Mono produces at most one value, so the semantics of dropping the remaining elements are not useful. " +
"Please use awaitSingle() instead.",
level = DeprecationLevel.WARNING,
replaceWith = ReplaceWith("this.awaitSingle()")
)
) // Warning since 1.5, error in 1.6, hidden in 1.7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't be hidden because this is added as a lint hint, otherwise, it wouldn't be added in the first place

* function immediately cancels its [Subscription] and resumes with [CancellationException].
*
* @throws NoSuchElementException if the publisher does not emit any value
* This function is deprecated in favor of [Mono.awaitSingle].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This phrasing implicitly implies that this function was non-deprecated prior to this version.
I suggest rephrasing it more explicitly, e.g.: "This is a deprecated lint function to improve user experience and guard against ambiguous Mono usages." and adding example to the end of KDoc:

Example of ambiguous usage:
\```
// The signature of the call suggests that one or zero values can be returned from 'findById', but 'awaitFirstOrNull' instead of 'awaitSingleOrNull' may trick the reader into believing that more than one value can be returned
myDbClient.findById(uniqueId).awaitFirstOrNull()
\```

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@dkhalanskyjb dkhalanskyjb merged commit 71df60e into develop Apr 21, 2021
@dkhalanskyjb dkhalanskyjb deleted the fix-2591 branch April 21, 2021 07:30
pablobaxter pushed a commit to pablobaxter/kotlinx.coroutines that referenced this pull request Sep 14, 2022
…nd Maybe (Kotlin#2628)

* Deprecated `awaitSingleOr*` on arbitrary Publishers
* Added specialized `awaitSingle` and `awaitSingleOrNull` methods on
  `Maybe<T>` and `Mono<T>`
* Deprecated `Maybe<T>.await()` in favor of `Maybe<T>.awaitSingleOrNull()`
* Added specializations of most of the `await*` methods for `Mono<T>` and
  deprecated them, as the only useful methods on `Mono<T>` are
  `awaitSingle` and `awaitSingleOrNull`
* Reworded some documentation for `await*` methods

Fixes Kotlin#2591
Fixes Kotlin#1587
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants