From 69843fd621612984b47882e8c0b2c108249a3362 Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Wed, 16 Feb 2022 15:02:25 -0800 Subject: [PATCH 01/12] create intermediate supportedTargetMain and supportedTargetTest sourcesets goes between common and js/apple --- kmp-nativecoroutines-core/build.gradle.kts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/kmp-nativecoroutines-core/build.gradle.kts b/kmp-nativecoroutines-core/build.gradle.kts index 7b498bef..9f418f2b 100644 --- a/kmp-nativecoroutines-core/build.gradle.kts +++ b/kmp-nativecoroutines-core/build.gradle.kts @@ -38,12 +38,18 @@ kotlin { implementation(Dependencies.Kotlinx.atomicfu) } } - val appleMain by creating { + val supportedTargetMain by creating { dependsOn(commonMain) } - val appleTest by creating { + val supportedTargetTest by creating { dependsOn(commonTest) } + val appleMain by creating { + dependsOn(supportedTargetMain) + } + val appleTest by creating { + dependsOn(supportedTargetTest) + } listOf( macosX64, macosArm64, iosArm64, iosX64, iosSimulatorArm64, @@ -57,5 +63,11 @@ kotlin { dependsOn(appleTest) } } + val jsMain by getting { + dependsOn(supportedTargetMain) + } + val jsTest by getting { + dependsOn(supportedTargetTest) + } } } From b640f2e7ce1640f3eeb9c1c5555cae1604bdbdcf Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Wed, 16 Feb 2022 15:03:55 -0800 Subject: [PATCH 02/12] create cross-platform freeze function --- .../com/rickclephas/kmp/nativecoroutines/FreezingApple.kt | 5 +++++ .../com/rickclephas/kmp/nativecoroutines/FreezingJs.kt | 6 ++++++ .../kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt | 3 +++ 3 files changed, 14 insertions(+) create mode 100644 kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingApple.kt create mode 100644 kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingJs.kt create mode 100644 kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingApple.kt b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingApple.kt new file mode 100644 index 00000000..a14b4d5f --- /dev/null +++ b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingApple.kt @@ -0,0 +1,5 @@ +package com.rickclephas.kmp.nativecoroutines + +import kotlin.native.concurrent.freeze + +actual fun T.freeze(): T = this.freeze() diff --git a/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingJs.kt b/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingJs.kt new file mode 100644 index 00000000..7e3457c4 --- /dev/null +++ b/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingJs.kt @@ -0,0 +1,6 @@ +package com.rickclephas.kmp.nativecoroutines + +/** + * Freezing is a no-op on JS + */ +internal actual fun T.freeze() = this \ No newline at end of file diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt new file mode 100644 index 00000000..34768f34 --- /dev/null +++ b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt @@ -0,0 +1,3 @@ +package com.rickclephas.kmp.nativecoroutines + +internal expect fun T.freeze(): T From 244840047fde1b0abedfe774ea1c63fddf811e2e Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Wed, 16 Feb 2022 15:04:30 -0800 Subject: [PATCH 03/12] create cross-platform PlatformError abstraction --- .../com/rickclephas/kmp/nativecoroutines/NSError.kt | 7 +++++++ .../kmp/nativecoroutines/PlatformErrorJs.kt | 6 ++++++ .../kmp/nativecoroutines/PlatformError.kt | 13 +++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformErrorJs.kt create mode 100644 kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformError.kt diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt index 2b91a447..89d26484 100644 --- a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt +++ b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt @@ -6,6 +6,13 @@ import platform.Foundation.NSError import platform.Foundation.NSLocalizedDescriptionKey import kotlin.native.concurrent.freeze +actual typealias PlatformError = NSError + +internal actual fun Throwable.asPlatformError(): PlatformError = this.asNSError() + +actual val PlatformError.kotlinCause + get() = this.userInfo["KotlinException"] as? Throwable + /** * Converts a [Throwable] to a [NSError]. * diff --git a/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformErrorJs.kt b/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformErrorJs.kt new file mode 100644 index 00000000..5e503688 --- /dev/null +++ b/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformErrorJs.kt @@ -0,0 +1,6 @@ +package com.rickclephas.kmp.nativecoroutines + +actual typealias PlatformError = Throwable +actual fun Throwable.asPlatformError() = this +actual val PlatformError.kotlinCause: Throwable? + get() = this \ No newline at end of file diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformError.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformError.kt new file mode 100644 index 00000000..c6a8faa6 --- /dev/null +++ b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformError.kt @@ -0,0 +1,13 @@ +package com.rickclephas.kmp.nativecoroutines + +/** + * Represents an error in a way that the specific platform is able to handle + */ +expect class PlatformError + +/** + * Converts a [Throwable] to a [PlatformError]. + */ +internal expect fun Throwable.asPlatformError(): PlatformError + +internal expect val PlatformError.kotlinCause: Throwable? \ No newline at end of file From 8a8008e91059837ca7db20b2ba3834fc17c02e29 Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Wed, 16 Feb 2022 15:05:48 -0800 Subject: [PATCH 04/12] move core classes from appleMain to supportedTargetMain --- .../rickclephas/kmp/nativecoroutines/CoroutineScope.kt | 0 .../rickclephas/kmp/nativecoroutines/NativeCallback.kt | 2 -- .../kmp/nativecoroutines/NativeCancellable.kt | 1 - .../com/rickclephas/kmp/nativecoroutines/NativeFlow.kt | 10 ++++------ .../rickclephas/kmp/nativecoroutines/NativeSuspend.kt | 10 ++++------ 5 files changed, 8 insertions(+), 15 deletions(-) rename kmp-nativecoroutines-core/src/{appleMain => supportedTargetMain}/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt (100%) rename kmp-nativecoroutines-core/src/{appleMain => supportedTargetMain}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt (92%) rename kmp-nativecoroutines-core/src/{appleMain => supportedTargetMain}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt (91%) rename kmp-nativecoroutines-core/src/{appleMain => supportedTargetMain}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt (87%) rename kmp-nativecoroutines-core/src/{appleMain => supportedTargetMain}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt (87%) diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt similarity index 100% rename from kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt rename to kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt similarity index 92% rename from kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt rename to kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt index 18390787..1aef03d0 100644 --- a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt @@ -1,7 +1,5 @@ package com.rickclephas.kmp.nativecoroutines -import kotlin.native.concurrent.freeze - /** * A callback with a single argument. * diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt similarity index 91% rename from kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt rename to kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt index c0786d83..d3262256 100644 --- a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt @@ -1,7 +1,6 @@ package com.rickclephas.kmp.nativecoroutines import kotlinx.coroutines.Job -import kotlin.native.concurrent.freeze /** * A function that cancels the coroutines [Job]. diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt similarity index 87% rename from kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt rename to kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt index f1dce7e6..a682a210 100644 --- a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt @@ -5,8 +5,6 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch -import platform.Foundation.NSError -import kotlin.native.concurrent.freeze /** * A function that collects a [Flow] via callbacks. @@ -14,7 +12,7 @@ import kotlin.native.concurrent.freeze * The function takes an `onItem` and `onComplete` callback * and returns a cancellable that can be used to cancel the collection. */ -typealias NativeFlow = (onItem: NativeCallback, onComplete: NativeCallback) -> NativeCancellable +typealias NativeFlow = (onItem: NativeCallback, onComplete: NativeCallback) -> NativeCancellable /** * Creates a [NativeFlow] for this [Flow]. @@ -25,7 +23,7 @@ typealias NativeFlow = (onItem: NativeCallback, onComplete: NativeCallback */ fun Flow.asNativeFlow(scope: CoroutineScope? = null): NativeFlow { val coroutineScope = scope ?: defaultCoroutineScope - return (collect@{ onItem: NativeCallback, onComplete: NativeCallback -> + return (collect@{ onItem: NativeCallback, onComplete: NativeCallback -> val job = coroutineScope.launch { try { collect { onItem(it) } @@ -35,13 +33,13 @@ fun Flow.asNativeFlow(scope: CoroutineScope? = null): NativeFlow { // this is required since the job could be cancelled before it is started throw e } catch (e: Throwable) { - onComplete(e.asNSError()) + onComplete(e.asPlatformError()) } } job.invokeOnCompletion { cause -> // Only handle CancellationExceptions, all other exceptions should be handled inside the job if (cause !is CancellationException) return@invokeOnCompletion - onComplete(cause.asNSError()) + onComplete(cause.asPlatformError()) } return@collect job.asNativeCancellable() }).freeze() diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt similarity index 87% rename from kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt rename to kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt index 676b38ca..9da1315a 100644 --- a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt @@ -3,8 +3,6 @@ package com.rickclephas.kmp.nativecoroutines import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch -import platform.Foundation.NSError -import kotlin.native.concurrent.freeze /** * A function that awaits a suspend function via callbacks. @@ -12,7 +10,7 @@ import kotlin.native.concurrent.freeze * The function takes an `onResult` and `onError` callback * and returns a cancellable that can be used to cancel the suspend function. */ -typealias NativeSuspend = (onResult: NativeCallback, onError: NativeCallback) -> NativeCancellable +typealias NativeSuspend = (onResult: NativeCallback, onError: NativeCallback) -> NativeCancellable /** * Creates a [NativeSuspend] for the provided suspend [block]. @@ -22,7 +20,7 @@ typealias NativeSuspend = (onResult: NativeCallback, onError: NativeCallba */ fun nativeSuspend(scope: CoroutineScope? = null, block: suspend () -> T): NativeSuspend { val coroutineScope = scope ?: defaultCoroutineScope - return (collect@{ onResult: NativeCallback, onError: NativeCallback -> + return (collect@{ onResult: NativeCallback, onError: NativeCallback -> val job = coroutineScope.launch { try { onResult(block()) @@ -31,13 +29,13 @@ fun nativeSuspend(scope: CoroutineScope? = null, block: suspend () -> T): Na // this is required since the job could be cancelled before it is started throw e } catch (e: Throwable) { - onError(e.asNSError()) + onError(e.asPlatformError()) } } job.invokeOnCompletion { cause -> // Only handle CancellationExceptions, all other exceptions should be handled inside the job if (cause !is CancellationException) return@invokeOnCompletion - onError(cause.asNSError()) + onError(cause.asPlatformError()) } return@collect job.asNativeCancellable() }).freeze() From 020d9ab0e32d02199fbdbfb0568b4734bf9af561 Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Thu, 17 Feb 2022 11:55:27 -0800 Subject: [PATCH 05/12] remove JS platform (will be added back later) --- kmp-nativecoroutines-core/build.gradle.kts | 6 ------ .../com/rickclephas/kmp/nativecoroutines/FreezingJs.kt | 6 ------ .../com/rickclephas/kmp/nativecoroutines/PlatformErrorJs.kt | 6 ------ 3 files changed, 18 deletions(-) delete mode 100644 kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingJs.kt delete mode 100644 kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformErrorJs.kt diff --git a/kmp-nativecoroutines-core/build.gradle.kts b/kmp-nativecoroutines-core/build.gradle.kts index 9f418f2b..66a1f16f 100644 --- a/kmp-nativecoroutines-core/build.gradle.kts +++ b/kmp-nativecoroutines-core/build.gradle.kts @@ -63,11 +63,5 @@ kotlin { dependsOn(appleTest) } } - val jsMain by getting { - dependsOn(supportedTargetMain) - } - val jsTest by getting { - dependsOn(supportedTargetTest) - } } } diff --git a/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingJs.kt b/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingJs.kt deleted file mode 100644 index 7e3457c4..00000000 --- a/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/FreezingJs.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.rickclephas.kmp.nativecoroutines - -/** - * Freezing is a no-op on JS - */ -internal actual fun T.freeze() = this \ No newline at end of file diff --git a/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformErrorJs.kt b/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformErrorJs.kt deleted file mode 100644 index 5e503688..00000000 --- a/kmp-nativecoroutines-core/src/jsMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformErrorJs.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.rickclephas.kmp.nativecoroutines - -actual typealias PlatformError = Throwable -actual fun Throwable.asPlatformError() = this -actual val PlatformError.kotlinCause: Throwable? - get() = this \ No newline at end of file From 8ca657c6ddf5aee5dbc77b43b30250da2618bfbc Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Thu, 17 Feb 2022 11:59:44 -0800 Subject: [PATCH 06/12] move tests from appleTest to supportedTargetTest --- .../nativecoroutines/NativeCallbackTests.kt | 0 .../nativecoroutines/NativeCancellableTests.kt | 0 .../kmp/nativecoroutines/NativeFlowTests.kt | 18 +++++++++++------- .../kmp/nativecoroutines/NativeSuspendTests.kt | 7 +++---- 4 files changed, 14 insertions(+), 11 deletions(-) rename kmp-nativecoroutines-core/src/{appleTest => supportedTargetTest}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt (100%) rename kmp-nativecoroutines-core/src/{appleTest => supportedTargetTest}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt (100%) rename kmp-nativecoroutines-core/src/{appleTest => supportedTargetTest}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt (85%) rename kmp-nativecoroutines-core/src/{appleTest => supportedTargetTest}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt (93%) diff --git a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt similarity index 100% rename from kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt rename to kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt diff --git a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt similarity index 100% rename from kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt rename to kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt diff --git a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt similarity index 85% rename from kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt rename to kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt index 302ab64a..d4704cd9 100644 --- a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt @@ -11,7 +11,7 @@ class NativeFlowTests { @Test fun `ensure frozen`() { - val flow = flow { } + val flow = flow { } assertFalse(flow.isFrozen, "Flow shouldn't be frozen yet") val nativeFlow = flow.asNativeFlow() assertTrue(nativeFlow.isFrozen, "NativeFlow should be frozen") @@ -19,8 +19,8 @@ class NativeFlowTests { } @Test - fun `ensure completion callback is invoked`() = runBlocking { - val flow = flow { } + fun `ensure completion callback is invoked`() = kotlinx.coroutines.runBlocking { + val flow = flow { } val job = Job() val nativeFlow = flow.asNativeFlow(CoroutineScope(job)) val completionCount = atomic(0) @@ -33,7 +33,7 @@ class NativeFlowTests { } @Test - fun `ensure exceptions are received as errors`() = runBlocking { + fun `ensure exceptions are received as errors`() = kotlinx.coroutines.runBlocking { val exception = RandomException() val flow = flow { throw exception } val job = Job() @@ -50,7 +50,7 @@ class NativeFlowTests { } @Test - fun `ensure values are received`() = runBlocking { + fun `ensure values are received`() = kotlinx.coroutines.runBlocking { val values = listOf(RandomValue(), RandomValue(), RandomValue(), RandomValue()) val flow = flow { values.forEach { emit(it) } } val job = Job() @@ -61,11 +61,15 @@ class NativeFlowTests { receivedValueCount.incrementAndGet() }, { _, _ -> }) job.children.forEach { it.join() } // Waits for the collection to complete - assertEquals(values.size, receivedValueCount.value, "Item callback should be called for every value") + assertEquals( + values.size, + receivedValueCount.value, + "Item callback should be called for every value" + ) } @Test - fun `ensure collection is cancelled`() = runBlocking { + fun `ensure collection is cancelled`() = kotlinx.coroutines.runBlocking { val flow = MutableSharedFlow() val job = Job() val nativeFlow = flow.asNativeFlow(CoroutineScope(job)) diff --git a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt similarity index 93% rename from kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt rename to kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt index 84e45d36..174c0e7f 100644 --- a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt @@ -4,7 +4,6 @@ import kotlinx.atomicfu.atomic import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.delay -import kotlinx.coroutines.runBlocking import kotlin.coroutines.cancellation.CancellationException import kotlin.native.concurrent.isFrozen import kotlin.test.* @@ -31,7 +30,7 @@ class NativeSuspendTests { } @Test - fun `ensure correct result is received`() = runBlocking { + fun `ensure correct result is received`() = kotlinx.coroutines.runBlocking { val value = RandomValue() val job = Job() val nativeSuspend = nativeSuspend(CoroutineScope(job)) { delayAndReturn(100, value) } @@ -49,7 +48,7 @@ class NativeSuspendTests { } @Test - fun `ensure exceptions are received as errors`() = runBlocking { + fun `ensure exceptions are received as errors`() = kotlinx.coroutines.runBlocking { val exception = RandomException() val job = Job() val nativeSuspend = nativeSuspend(CoroutineScope(job)) { delayAndThrow(100, exception) } @@ -69,7 +68,7 @@ class NativeSuspendTests { } @Test - fun `ensure function is cancelled`() = runBlocking { + fun `ensure function is cancelled`() = kotlinx.coroutines.runBlocking { val job = Job() val nativeSuspend = nativeSuspend(CoroutineScope(job)) { delayAndReturn(5_000, RandomValue()) } val receivedResultCount = atomic(0) From 7a1e21efd9a4f140930fed0de8b0c5c40bd22be9 Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Thu, 17 Feb 2022 13:42:23 -0800 Subject: [PATCH 07/12] rename PlatformError to NativeError --- .../com/rickclephas/kmp/nativecoroutines/NSError.kt | 6 +++--- .../rickclephas/kmp/nativecoroutines/NativeError.kt | 13 +++++++++++++ .../rickclephas/kmp/nativecoroutines/NativeFlow.kt | 8 ++++---- .../kmp/nativecoroutines/NativeSuspend.kt | 8 ++++---- .../kmp/nativecoroutines/PlatformError.kt | 13 ------------- 5 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt delete mode 100644 kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformError.kt diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt index 89d26484..a7356cfc 100644 --- a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt +++ b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt @@ -6,11 +6,11 @@ import platform.Foundation.NSError import platform.Foundation.NSLocalizedDescriptionKey import kotlin.native.concurrent.freeze -actual typealias PlatformError = NSError +actual typealias NativeError = NSError -internal actual fun Throwable.asPlatformError(): PlatformError = this.asNSError() +internal actual fun Throwable.asNativeError(): NativeError = this.asNSError() -actual val PlatformError.kotlinCause +actual val NativeError.kotlinCause get() = this.userInfo["KotlinException"] as? Throwable /** diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt new file mode 100644 index 00000000..393f986c --- /dev/null +++ b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt @@ -0,0 +1,13 @@ +package com.rickclephas.kmp.nativecoroutines + +/** + * Represents an error in a way that the specific platform is able to handle + */ +expect class NativeError + +/** + * Converts a [Throwable] to a [NativeError]. + */ +internal expect fun Throwable.asNativeError(): NativeError + +internal expect val NativeError.kotlinCause: Throwable? \ No newline at end of file diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt index a682a210..02734a32 100644 --- a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt @@ -12,7 +12,7 @@ import kotlinx.coroutines.launch * The function takes an `onItem` and `onComplete` callback * and returns a cancellable that can be used to cancel the collection. */ -typealias NativeFlow = (onItem: NativeCallback, onComplete: NativeCallback) -> NativeCancellable +typealias NativeFlow = (onItem: NativeCallback, onComplete: NativeCallback) -> NativeCancellable /** * Creates a [NativeFlow] for this [Flow]. @@ -23,7 +23,7 @@ typealias NativeFlow = (onItem: NativeCallback, onComplete: NativeCallback */ fun Flow.asNativeFlow(scope: CoroutineScope? = null): NativeFlow { val coroutineScope = scope ?: defaultCoroutineScope - return (collect@{ onItem: NativeCallback, onComplete: NativeCallback -> + return (collect@{ onItem: NativeCallback, onComplete: NativeCallback -> val job = coroutineScope.launch { try { collect { onItem(it) } @@ -33,13 +33,13 @@ fun Flow.asNativeFlow(scope: CoroutineScope? = null): NativeFlow { // this is required since the job could be cancelled before it is started throw e } catch (e: Throwable) { - onComplete(e.asPlatformError()) + onComplete(e.asNativeError()) } } job.invokeOnCompletion { cause -> // Only handle CancellationExceptions, all other exceptions should be handled inside the job if (cause !is CancellationException) return@invokeOnCompletion - onComplete(cause.asPlatformError()) + onComplete(cause.asNativeError()) } return@collect job.asNativeCancellable() }).freeze() diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt index 9da1315a..f7253963 100644 --- a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt @@ -10,7 +10,7 @@ import kotlinx.coroutines.launch * The function takes an `onResult` and `onError` callback * and returns a cancellable that can be used to cancel the suspend function. */ -typealias NativeSuspend = (onResult: NativeCallback, onError: NativeCallback) -> NativeCancellable +typealias NativeSuspend = (onResult: NativeCallback, onError: NativeCallback) -> NativeCancellable /** * Creates a [NativeSuspend] for the provided suspend [block]. @@ -20,7 +20,7 @@ typealias NativeSuspend = (onResult: NativeCallback, onError: NativeCallba */ fun nativeSuspend(scope: CoroutineScope? = null, block: suspend () -> T): NativeSuspend { val coroutineScope = scope ?: defaultCoroutineScope - return (collect@{ onResult: NativeCallback, onError: NativeCallback -> + return (collect@{ onResult: NativeCallback, onError: NativeCallback -> val job = coroutineScope.launch { try { onResult(block()) @@ -29,13 +29,13 @@ fun nativeSuspend(scope: CoroutineScope? = null, block: suspend () -> T): Na // this is required since the job could be cancelled before it is started throw e } catch (e: Throwable) { - onError(e.asPlatformError()) + onError(e.asNativeError()) } } job.invokeOnCompletion { cause -> // Only handle CancellationExceptions, all other exceptions should be handled inside the job if (cause !is CancellationException) return@invokeOnCompletion - onError(cause.asPlatformError()) + onError(cause.asNativeError()) } return@collect job.asNativeCancellable() }).freeze() diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformError.kt b/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformError.kt deleted file mode 100644 index c6a8faa6..00000000 --- a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/PlatformError.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.rickclephas.kmp.nativecoroutines - -/** - * Represents an error in a way that the specific platform is able to handle - */ -expect class PlatformError - -/** - * Converts a [Throwable] to a [PlatformError]. - */ -internal expect fun Throwable.asPlatformError(): PlatformError - -internal expect val PlatformError.kotlinCause: Throwable? \ No newline at end of file From 89bd0d8c6bbdd8f196ed5c66977c7ca144740437 Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Thu, 17 Feb 2022 13:42:47 -0800 Subject: [PATCH 08/12] make kotlinCause internal in appleMain --- .../kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt index a7356cfc..864da0f7 100644 --- a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt +++ b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt @@ -10,7 +10,7 @@ actual typealias NativeError = NSError internal actual fun Throwable.asNativeError(): NativeError = this.asNSError() -actual val NativeError.kotlinCause +internal actual val NativeError.kotlinCause get() = this.userInfo["KotlinException"] as? Throwable /** From 8b408dc896cc56b7584677d18f798305a636c40c Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Thu, 17 Feb 2022 13:45:25 -0800 Subject: [PATCH 09/12] inline asNSError into asNativeError --- .../com/rickclephas/kmp/nativecoroutines/NSError.kt | 12 +++++------- .../rickclephas/kmp/nativecoroutines/NSErrorTests.kt | 8 ++++---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt index 864da0f7..df2fbc2e 100644 --- a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt +++ b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt @@ -8,11 +8,6 @@ import kotlin.native.concurrent.freeze actual typealias NativeError = NSError -internal actual fun Throwable.asNativeError(): NativeError = this.asNSError() - -internal actual val NativeError.kotlinCause - get() = this.userInfo["KotlinException"] as? Throwable - /** * Converts a [Throwable] to a [NSError]. * @@ -22,7 +17,7 @@ internal actual val NativeError.kotlinCause * The Kotlin throwable can be retrieved from the [NSError.userInfo] with the key `KotlinException`. */ @OptIn(UnsafeNumber::class) -internal fun Throwable.asNSError(): NSError { +internal actual fun Throwable.asNativeError(): NativeError { val userInfo = mutableMapOf() userInfo["KotlinException"] = this.freeze() val message = message @@ -30,4 +25,7 @@ internal fun Throwable.asNSError(): NSError { userInfo[NSLocalizedDescriptionKey] = message } return NSError.errorWithDomain("KotlinException", 0.convert(), userInfo) -} \ No newline at end of file +} + +internal actual val NativeError.kotlinCause + get() = this.userInfo["KotlinException"] as? Throwable diff --git a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt b/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt index 50de7ef6..eda09300 100644 --- a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt +++ b/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt @@ -11,7 +11,7 @@ class NSErrorTests { fun `ensure frozen`() { val exception = RandomException() assertFalse(exception.isFrozen, "Exception shouldn't be frozen yet") - val nsError = exception.asNSError() + val nsError = exception.asNativeError() assertTrue(nsError.isFrozen, "NSError should be frozen") assertTrue(exception.isFrozen, "Exception should be frozen") } @@ -20,7 +20,7 @@ class NSErrorTests { @OptIn(UnsafeNumber::class) fun `ensure NSError domain and code are correct`() { val exception = RandomException() - val nsError = exception.asNSError() + val nsError = exception.asNativeError() assertEquals("KotlinException", nsError.domain, "Incorrect NSError domain") assertEquals(0.convert(), nsError.code, "Incorrect NSError code") } @@ -28,7 +28,7 @@ class NSErrorTests { @Test fun `ensure localizedDescription is set to message`() { val exception = RandomException() - val nsError = exception.asNSError() + val nsError = exception.asNativeError() assertEquals(exception.message, nsError.localizedDescription, "Localized description isn't set to message") } @@ -36,7 +36,7 @@ class NSErrorTests { @Test fun `ensure exception is part of user info`() { val exception = RandomException() - val nsError = exception.asNSError() + val nsError = exception.asNativeError() assertSame(exception, nsError.userInfo["KotlinException"], "Exception isn't part of the user info") } } \ No newline at end of file From 12a8509bea6191df3d757a319b76d244ff97212f Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Thu, 17 Feb 2022 13:46:48 -0800 Subject: [PATCH 10/12] use kotlinCause instead of looking at userInfo --- .../com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt | 2 +- .../com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt | 4 ++-- .../rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt b/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt index eda09300..96916dd3 100644 --- a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt +++ b/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt @@ -37,6 +37,6 @@ class NSErrorTests { fun `ensure exception is part of user info`() { val exception = RandomException() val nsError = exception.asNativeError() - assertSame(exception, nsError.userInfo["KotlinException"], "Exception isn't part of the user info") + assertSame(exception, nsError.kotlinCause, "Exception isn't part of the user info") } } \ No newline at end of file diff --git a/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt index d4704cd9..eb92bfa3 100644 --- a/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt @@ -41,7 +41,7 @@ class NativeFlowTests { val completionCount = atomic(0) nativeFlow({ _, _ -> }, { error, _ -> assertNotNull(error, "Flow should complete with an error") - val kotlinException = error.userInfo["KotlinException"] + val kotlinException = error.kotlinCause assertSame(exception, kotlinException, "Kotlin exception should be the same exception") completionCount.incrementAndGet() }) @@ -76,7 +76,7 @@ class NativeFlowTests { val completionCount = atomic(0) val cancel = nativeFlow({ _, _ -> }, { error, _ -> assertNotNull(error, "Flow should complete with an error") - val exception = error.userInfo["KotlinException"] + val exception = error.kotlinCause assertIs(exception, "Error should contain CancellationException") completionCount.incrementAndGet() }) diff --git a/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt index 174c0e7f..ba5c2b2c 100644 --- a/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt +++ b/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt @@ -58,7 +58,7 @@ class NativeSuspendTests { receivedResultCount.incrementAndGet() }, { error, _ -> assertNotNull(error, "Function should complete with an error") - val kotlinException = error.userInfo["KotlinException"] + val kotlinException = error.kotlinCause assertSame(exception, kotlinException, "Kotlin exception should be the same exception") receivedErrorCount.incrementAndGet() }) @@ -77,7 +77,7 @@ class NativeSuspendTests { receivedResultCount.incrementAndGet() }, { error, _ -> assertNotNull(error, "Function should complete with an error") - val exception = error.userInfo["KotlinException"] + val exception = error.kotlinCause assertIs(exception, "Error should contain CancellationException") receivedErrorCount.incrementAndGet() }) From c2ac88a22ff4b1333f89f3eb2b53223e3b2c6280 Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Thu, 17 Feb 2022 13:47:54 -0800 Subject: [PATCH 11/12] rename supportedTarget -> nativeCoroutines --- kmp-nativecoroutines-core/build.gradle.kts | 8 ++++---- .../rickclephas/kmp/nativecoroutines/CoroutineScope.kt | 0 .../com/rickclephas/kmp/nativecoroutines/Freezing.kt | 0 .../rickclephas/kmp/nativecoroutines/NativeCallback.kt | 0 .../rickclephas/kmp/nativecoroutines/NativeCancellable.kt | 0 .../com/rickclephas/kmp/nativecoroutines/NativeError.kt | 0 .../com/rickclephas/kmp/nativecoroutines/NativeFlow.kt | 0 .../com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt | 0 .../kmp/nativecoroutines/NativeCallbackTests.kt | 0 .../kmp/nativecoroutines/NativeCancellableTests.kt | 0 .../rickclephas/kmp/nativecoroutines/NativeFlowTests.kt | 0 .../kmp/nativecoroutines/NativeSuspendTests.kt | 0 12 files changed, 4 insertions(+), 4 deletions(-) rename kmp-nativecoroutines-core/src/{supportedTargetMain => nativeCoroutinesMain}/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetMain => nativeCoroutinesMain}/kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetMain => nativeCoroutinesMain}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetMain => nativeCoroutinesMain}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetMain => nativeCoroutinesMain}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetMain => nativeCoroutinesMain}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetMain => nativeCoroutinesMain}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetTest => nativeCoroutinesTest}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetTest => nativeCoroutinesTest}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetTest => nativeCoroutinesTest}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt (100%) rename kmp-nativecoroutines-core/src/{supportedTargetTest => nativeCoroutinesTest}/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt (100%) diff --git a/kmp-nativecoroutines-core/build.gradle.kts b/kmp-nativecoroutines-core/build.gradle.kts index 66a1f16f..863d888c 100644 --- a/kmp-nativecoroutines-core/build.gradle.kts +++ b/kmp-nativecoroutines-core/build.gradle.kts @@ -38,17 +38,17 @@ kotlin { implementation(Dependencies.Kotlinx.atomicfu) } } - val supportedTargetMain by creating { + val nativeCoroutinesMain by creating { dependsOn(commonMain) } - val supportedTargetTest by creating { + val nativeCoroutinesTest by creating { dependsOn(commonTest) } val appleMain by creating { - dependsOn(supportedTargetMain) + dependsOn(nativeCoroutinesMain) } val appleTest by creating { - dependsOn(supportedTargetTest) + dependsOn(nativeCoroutinesTest) } listOf( macosX64, macosArm64, diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/CoroutineScope.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/Freezing.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallback.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellable.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlow.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspend.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCallbackTests.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeCancellableTests.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeFlowTests.kt diff --git a/kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt similarity index 100% rename from kmp-nativecoroutines-core/src/supportedTargetTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt rename to kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeSuspendTests.kt From 7de411f889bec50272043421632a2b6b98958c63 Mon Sep 17 00:00:00 2001 From: Ankush Gupta Date: Fri, 18 Feb 2022 13:46:16 -0800 Subject: [PATCH 12/12] move NativeError.kotlinCause from main to test --- .../kmp/nativecoroutines/NSError.kt | 3 --- .../kmp/nativecoroutines/NSErrorTests.kt | 21 +++------------ .../kmp/nativecoroutines/NativeError.kt | 2 -- .../kmp/nativecoroutines/NativeErrorTests.kt | 27 +++++++++++++++++++ 4 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeErrorTests.kt diff --git a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt index df2fbc2e..ea6443cb 100644 --- a/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt +++ b/kmp-nativecoroutines-core/src/appleMain/kotlin/com/rickclephas/kmp/nativecoroutines/NSError.kt @@ -26,6 +26,3 @@ internal actual fun Throwable.asNativeError(): NativeError { } return NSError.errorWithDomain("KotlinException", 0.convert(), userInfo) } - -internal actual val NativeError.kotlinCause - get() = this.userInfo["KotlinException"] as? Throwable diff --git a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt b/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt index 96916dd3..6bd018d3 100644 --- a/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt +++ b/kmp-nativecoroutines-core/src/appleTest/kotlin/com/rickclephas/kmp/nativecoroutines/NSErrorTests.kt @@ -2,20 +2,12 @@ package com.rickclephas.kmp.nativecoroutines import kotlinx.cinterop.UnsafeNumber import kotlinx.cinterop.convert -import kotlin.native.concurrent.isFrozen import kotlin.test.* -class NSErrorTests { - - @Test - fun `ensure frozen`() { - val exception = RandomException() - assertFalse(exception.isFrozen, "Exception shouldn't be frozen yet") - val nsError = exception.asNativeError() - assertTrue(nsError.isFrozen, "NSError should be frozen") - assertTrue(exception.isFrozen, "Exception should be frozen") - } +internal actual val NativeError.kotlinCause + get() = this.userInfo["KotlinException"] as? Throwable +class NSErrorTests { @Test @OptIn(UnsafeNumber::class) fun `ensure NSError domain and code are correct`() { @@ -32,11 +24,4 @@ class NSErrorTests { assertEquals(exception.message, nsError.localizedDescription, "Localized description isn't set to message") } - - @Test - fun `ensure exception is part of user info`() { - val exception = RandomException() - val nsError = exception.asNativeError() - assertSame(exception, nsError.kotlinCause, "Exception isn't part of the user info") - } } \ No newline at end of file diff --git a/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt index 393f986c..0c44b12b 100644 --- a/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt +++ b/kmp-nativecoroutines-core/src/nativeCoroutinesMain/kotlin/com/rickclephas/kmp/nativecoroutines/NativeError.kt @@ -9,5 +9,3 @@ expect class NativeError * Converts a [Throwable] to a [NativeError]. */ internal expect fun Throwable.asNativeError(): NativeError - -internal expect val NativeError.kotlinCause: Throwable? \ No newline at end of file diff --git a/kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeErrorTests.kt b/kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeErrorTests.kt new file mode 100644 index 00000000..f9346c1f --- /dev/null +++ b/kmp-nativecoroutines-core/src/nativeCoroutinesTest/kotlin/com/rickclephas/kmp/nativecoroutines/NativeErrorTests.kt @@ -0,0 +1,27 @@ +package com.rickclephas.kmp.nativecoroutines + +import kotlin.native.concurrent.isFrozen +import kotlin.test.* + +/** + * Get the [Throwable] that is represented by the given [NativeError] + */ +internal expect val NativeError.kotlinCause: Throwable? + +class NativeErrorTests { + @Test + fun `ensure frozen`() { + val exception = RandomException() + assertFalse(exception.isFrozen, "Exception shouldn't be frozen yet") + val nsError = exception.asNativeError() + assertTrue(nsError.isFrozen, "NSError should be frozen") + assertTrue(exception.isFrozen, "Exception should be frozen") + } + + @Test + fun `ensure exception is part of user info`() { + val exception = RandomException() + val nsError = exception.asNativeError() + assertSame(exception, nsError.kotlinCause, "Exception isn't part of the NativeError") + } +}