-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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()
- Loading branch information
Showing
3 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
samples/src/commonMain/kotlin/sample/coroutines/InterfaceWithSuspend.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sample.coroutines | ||
|
||
import deezer.kustomexport.KustomExport | ||
|
||
@KustomExport | ||
interface IThingDoer { | ||
suspend fun doThings(): List<Long> | ||
} | ||
|
||
class NonWebThingDoer : IThingDoer { | ||
override suspend fun doThings(): List<Long> { | ||
return listOf(1, 2, 3) | ||
} | ||
} | ||
|
||
@KustomExport | ||
class WebThingDoer : IThingDoer { | ||
override suspend fun doThings(): List<Long> { | ||
return listOf(3, 2, 1) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
samples/src/commonMain/kotlin/sample/coroutines/InterfaceWithSuspend.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { runTest } from "../shared_ts/RunTest" | ||
import { assert, assertEquals, assertQuiet, assertEqualsQuiet } from "../shared_ts/Assert" | ||
import { sample } from '@kustom/Samples' | ||
|
||
runTest("InterfaceWithSuspend", async () : Promise<void> => { | ||
var neverAbortController = new AbortController() | ||
var neverAbortSignal = neverAbortController.signal | ||
|
||
const thingDoer = new sample.coroutines.js.WebThingDoer() | ||
const res = await thingDoer.doThings(neverAbortSignal) | ||
assertEquals([3, 2, 1].join(","), res.join(","), "executes suspend function with JS friendly types") | ||
}) |