File tree Expand file tree Collapse file tree
main/resources/templates/kotlin
test/resources/smoke/async_decorators
output/android-kotlin/com/example/smoke Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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")
4849lambda EngineWorkCompletedCallback = (engineError: EngineError?, engineResult: List<String>?) -> Void
4950
5051// An engine class, which offers users some asynchronous processing.
5152class 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}
Original file line number Diff line number Diff line change 99package com.example.smoke
1010
1111import 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
You can’t perform that action at this time.
0 commit comments