Skip to content

Commit 41e9706

Browse files
committed
smoke-tests: showcase the usage of async decorator
When async decorator and async metadata is used, then additional suspending overload is generated. Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
1 parent 224e8f5 commit 41e9706

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

gluecodium/src/main/resources/templates/kotlin/KotlinSuspendingFunction.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ suspend fun {{resolveName}}({{!!
2828
}}{{/asyncFunctionDecorator.inputParameters}}) { {{#asyncFunctionDecorator.asyncCallback.parameters}}{{resolveName}}{{#if iter.hasNext}}, {{/if}}{{!!
2929
}}{{/asyncFunctionDecorator.asyncCallback.parameters}} ->
3030
if ({{resolveName asyncFunctionDecorator.asyncCallback.errorField}} != null) {
31-
continuation.resumeWithException(Exception({{resolveName asyncFunctionDecorator.asyncCallback.errorField}}))
31+
continuation.resumeWithException(Exception({{resolveName asyncFunctionDecorator.asyncCallback.errorField}}.toString()))
3232
} else {
33-
continuation.resume({{resolveName asyncFunctionDecorator.asyncCallback.resultFields.0}})
33+
continuation.resume({{resolveName asyncFunctionDecorator.asyncCallback.resultFields.0}}!!)
3434
}
3535
}
3636

gluecodium/src/test/resources/smoke/async_decorators/input/AsyncDecorator.lime

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ enum EngineError {
4545
// Outcome of engine work is list of strings.
4646
// @param[engineError] The error in case of a failure. It is `null` for an operation that succeeds.
4747
// @param[engineResult] The result from engine. It is `null` in case of an error.
48+
@AsyncCallbackMetadata(ErrorField="engineError")
4849
lambda EngineWorkCompletedCallback = (engineError: EngineError?, engineResult: List<String>?) -> Void
4950

5051
// An engine class, which offers users some asynchronous processing.
5152
class CoolEngine {
5253
// A function, which downloads some 'cool labels' from the given url.
5354
// When it finishes, the 'callback' is called.
5455
// It returns the 'AsyncTaskHandle' which allows checking the status and cancelling the async task.
56+
@AsyncDecorator(CancelFunction="cancel")
5557
fun downloadCoolLabelsAsync(url: String, callback: EngineWorkCompletedCallback): AsyncTaskHandle
5658
}

gluecodium/src/test/resources/smoke/async_decorators/output/android-kotlin/com/example/smoke/CoolEngine.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
package com.example.smoke
1010

1111
import com.example.NativeBase
12+
import kotlin.coroutines.resume
13+
import kotlin.coroutines.resumeWithException
14+
import kotlinx.coroutines.suspendCancellableCoroutine
1215

1316
/**
1417
* An engine class, which offers users some asynchronous processing.
@@ -39,6 +42,18 @@ class CoolEngine : NativeBase {
3942

4043
external fun downloadCoolLabelsAsync(url: String, callback: EngineWorkCompletedCallback) : AsyncTaskHandle
4144

45+
suspend fun downloadCoolLabelsAsync(url: String): List<String> = suspendCancellableCoroutine { continuation ->
46+
val asyncTaskHandle = downloadCoolLabelsAsync(url) { engineError, engineResult ->
47+
if (engineError != null) {
48+
continuation.resumeWithException(Exception(engineError.toString()))
49+
} else {
50+
continuation.resume(engineResult!!)
51+
}
52+
}
53+
54+
continuation.invokeOnCancellation { asyncTaskHandle.cancel() }
55+
}
56+
4257

4358

4459

0 commit comments

Comments
 (0)