Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit adc689c

Browse files
shamaglureau
authored andcommitted
Handle an interface with suspend that returns a JS friendly type
If we are KustomExporting an interface that contains a suspend that returns a JS friendly type (e.g. Long <-> Double), we want to await the promise result before importing the JS type back to the Kotlin type. > promise.toLong().await() becomes: > promise.await().toLong()
1 parent 0726ff6 commit adc689c

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

compiler/src/main/kotlin/deezer/kustomexport/compiler/js/pattern/SharedTransformer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ fun FunctionDescriptor.buildWrappingFunctionBody(
141141
body += "val result = $callStr("
142142
body += if (parameters.isNotEmpty()) "\n" else ""
143143
body += params
144-
body += if (parameters.isNotEmpty()) ")\n" else ")\n"
144+
body += if (parameters.isNotEmpty()) ")" else ")"
145+
146+
body += if (import && isSuspend) ".%M()\n".toFormatString(coroutinesAwait) else "\n".toFormatString()
145147

146148
body += if (import || !isSuspend) "return·" else ""
147149
body += returnType.portMethod(import, "result".toFormatString())
148-
body += if (import && isSuspend) ".%M()".toFormatString(coroutinesAwait) else "".toFormatString()
149150

150151
if (!import && isSuspend) body += "\n}"
151152
return body
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package sample.coroutines
2+
3+
import deezer.kustomexport.KustomExport
4+
5+
@KustomExport
6+
interface IThingDoer {
7+
suspend fun doThings(): List<Long>
8+
}
9+
10+
class NonWebThingDoer : IThingDoer {
11+
override suspend fun doThings(): List<Long> {
12+
return listOf(1, 2, 3)
13+
}
14+
}
15+
16+
@KustomExport
17+
class WebThingDoer : IThingDoer {
18+
override suspend fun doThings(): List<Long> {
19+
return listOf(3, 2, 1)
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { runTest } from "../shared_ts/RunTest"
2+
import { assert, assertEquals, assertQuiet, assertEqualsQuiet } from "../shared_ts/Assert"
3+
import { sample } from '@kustom/Samples'
4+
5+
runTest("InterfaceWithSuspend", async () : Promise<void> => {
6+
var neverAbortController = new AbortController()
7+
var neverAbortSignal = neverAbortController.signal
8+
9+
const thingDoer = new sample.coroutines.js.WebThingDoer()
10+
const res = await thingDoer.doThings(neverAbortSignal)
11+
assertEquals([3, 2, 1].join(","), res.join(","), "executes suspend function with JS friendly types")
12+
})

0 commit comments

Comments
 (0)