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

Added function PNFuture.await() #338

Merged
merged 5 commits into from
Mar 7, 2025
Merged

Added function PNFuture.await() #338

merged 5 commits into from
Mar 7, 2025

Conversation

marcin-cebo
Copy link
Contributor

@marcin-cebo marcin-cebo commented Mar 6, 2025

fix: Internal fixes.

as a convenient function for those users that use coroutines.

New module pubnub-kotlin-coroutines has been created.
import kotlin.coroutines.resumeWithException

suspend fun <T> PNFuture<T>.await(): T {
val t = suspendCancellableCoroutine { cont ->
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: t isn't needed, I'd just do suspend fun <T> PNFuture<T>.await(): T = suspendCancellableCoroutine { ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I will change it.


class UtilKtTest {
@Test
fun `await returns result on success`() = runBlocking {
Copy link
Contributor

Choose a reason for hiding this comment

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

use runTest from kotlinx-coroutines-test instead of runBlocking

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

import org.junit.jupiter.api.Test
import kotlin.test.assertFailsWith

class FakePNFuture<T>(private val block: (Consumer<com.pubnub.api.v2.callbacks.Result<T>>) -> Unit) : PNFuture<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't really need it, I think you should be able to just do :
Unit.asFuture().then { error("exception!") }
or
expected.asFuture()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -29,3 +29,4 @@ include("examples:kotlin-app")
include("examples:java-app")
includeBuild("build-logic/ktlint-custom-rules")
includeBuild("migration_utils")
include("pubnub-kotlin-coroutines")
Copy link
Contributor

Choose a reason for hiding this comment

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

I would maybe put it in the pubnub-kotlin directory, but it's not critical, I think when you release it to maven the end result is the same

Copy link
Contributor Author

@marcin-cebo marcin-cebo Mar 6, 2025

Choose a reason for hiding this comment

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

ok

@marcin-cebo
Copy link
Contributor Author

@pubnub-release-bot release kotlin as v10.4.5

@marcin-cebo marcin-cebo merged commit 46494e3 into master Mar 7, 2025
6 checks passed
@marcin-cebo marcin-cebo deleted the mc/PNFuture_await branch March 7, 2025 15:09
@pubnub-release-bot
Copy link
Contributor

🚀 Release successfully completed 🚀

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.

3 participants