Skip to content

Commit a2d2883

Browse files
authored
Fix kotlin/js code and samples (#179)
1 parent 92cb410 commit a2d2883

File tree

15 files changed

+77
-30
lines changed

15 files changed

+77
-30
lines changed

build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import util.kotlinVersionParsed
77

88
plugins {
99
alias(libs.plugins.serialization) apply false
10-
alias(libs.plugins.ksp) apply false
1110
alias(libs.plugins.kotlinx.rpc) apply false
1211
alias(libs.plugins.atomicfu) apply false
1312
alias(libs.plugins.conventions.kover)

core/src/jsMain/kotlin/kotlinx/rpc/internal/WithRPCStubObject.js.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ public actual fun <R : Any> findRPCStubProvider(kClass: KClass<*>, resultKClass:
3939
)
4040
}
4141

42-
private val KClass<*>.jClass get(): JsClass<*> = asDynamic().jClass_1.unsafeCast<JsClass<*>>()
42+
/**
43+
* [KClassImpl] is internal in kjs stdlib, and it's jClass property is obfuscated by the webpack.
44+
* Hence, we need to find it manually.
45+
*/
46+
private val KClass<*>.jClass get(): JsClass<*> {
47+
return Object.entries(this)
48+
// key (_) is obfuscated here
49+
.firstOrNull { (_, value) -> value != null && Object.hasOwn(value,"\$metadata\$") }
50+
?.component2()
51+
?.unsafeCast<JsClass<*>>()
52+
?: error("jClass property was not found")
53+
}
4354

4455
/**
4556
* Workaround for bugs in [findAssociatedObject]

gradle-conventions-settings/src/main/kotlin/util/KotlinVersion.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ inline fun ExtensionAware.whenKotlinIsAtLeast(
9494
major: Int,
9595
minor: Int,
9696
patch: Int = 0,
97-
action: () -> Unit,
97+
action: () -> Unit = {},
9898
): ActionApplied {
9999
val kotlinVersion: KotlinVersion by extra
100100

gradle.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ systemProp.org.gradle.kotlin.dsl.precompiled.accessors.strict=true
2929

3030
# uncomment to debug compilation process
3131
#kotlin.compiler.execution.strategy=in-process
32+
33+
# Otherwise produces:
34+
# w: A compileOnly dependency is used in targets: Kotlin/JS.
35+
# Dependencies:
36+
# - org.jetbrains.kotlinx:atomicfu:0.22.0 (source sets: jsMain)
37+
#
38+
# Using compileOnly dependencies in these targets is not currently supported
39+
# because compileOnly dependencies must be present during the compilation of projects that depend on this project.
40+
kotlin.suppressGradlePluginWarnings=IncorrectCompileOnlyDependencyWarning

publishLocal.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ set -euxo pipefail
88

99
./gradlew publishAllPublicationsToBuildRepoRepository
1010
./gradlew -p compiler-plugin publishAllPublicationsToBuildRepoRepository
11-
./gradlew -p ksp-plugin publishAllPublicationsToBuildRepoRepository
1211
./gradlew -p gradle-plugin publishAllPublicationsToBuildRepoRepository
12+
13+
if [ -x "./gradlew -p ksp-plugin publishAllPublicationsToBuildRepoRepository" ]; then
14+
echo "KSP is on"
15+
else
16+
echo "KSP is off"
17+
fi;

samples/ktor-web-app/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
**/.gradle
33

44
local.properties
5-
server/src/main/resources/static
5+
server/src/main/resources/static/*
6+
!server/src/main/resources/static/index.html

samples/ktor-web-app/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ To run server without compiling frontend, simply run `main` function in [Applica
1313

1414
To Run server with latest frontend use this command:
1515
```bash
16-
./gradlew server:run
16+
./gradlew server:runApp
1717
```
1818
Note that this configuration uses production distribution of frontend app, which makes each build slower, as it takes more time to compile production webpack.

samples/ktor-web-app/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ plugins {
66
alias(libs.plugins.kotlin.multiplatform) apply false
77
alias(libs.plugins.kotlinx.rpc) apply false
88
alias(libs.plugins.kotlinx.rpc.platform) apply false
9-
alias(libs.plugins.ksp) apply false
109
}
1110

1211
allprojects {

samples/ktor-web-app/frontend/src/jsMain/kotlin/App.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
import kotlinx.coroutines.flow.flow
65
import kotlinx.rpc.RPCClient
76
import kotlinx.rpc.withService
8-
import kotlinx.rpc.streamScoped
97
import react.FC
108
import react.Props
119
import react.dom.html.ReactHTML.div
@@ -39,21 +37,13 @@ val AppContainer = FC<AppContainerProps> { props ->
3937

4038
useEffectOnceAsync {
4139
val greeting = service.hello("Alex", UserData("Berlin", "Smith"))
42-
data = WelcomeData(
43-
greeting,
44-
flow {
45-
streamScoped {
46-
service.subscribeToNews().collect {
47-
emit(it)
48-
}
49-
}
50-
},
51-
)
40+
data = WelcomeData(greeting)
5241
}
5342

5443
data?.also { welcomeData ->
5544
Welcome {
5645
this.data = welcomeData
46+
this.service = service
5747
}
5848
} ?: run {
5949
div {

samples/ktor-web-app/frontend/src/jsMain/kotlin/Welcome.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import emotion.react.css
66
import kotlinx.coroutines.flow.Flow
7+
import kotlinx.rpc.streamScoped
78
import react.FC
89
import react.Props
910
import react.dom.html.ReactHTML.div
@@ -16,30 +17,32 @@ import web.cssom.px
1617

1718
data class WelcomeData(
1819
val serverGreeting: String,
19-
val news: Flow<String>,
2020
)
2121

2222
external interface WelcomeProps : Props {
23+
var service: MyService
2324
var data: WelcomeData
2425
}
2526

26-
fun useArticles(data: WelcomeData): List<String> {
27+
fun useArticles(service: MyService): List<String> {
2728
var articles by useState(emptyList<String>())
2829

2930
useEffectOnceAsync {
3031
var localArticles = articles
31-
data.news.collect {
32-
@Suppress("SuspiciousCollectionReassignment")
33-
localArticles += it
34-
articles = localArticles
32+
streamScoped {
33+
service.subscribeToNews().collect {
34+
@Suppress("SuspiciousCollectionReassignment")
35+
localArticles += it
36+
articles = localArticles
37+
}
3538
}
3639
}
3740

3841
return articles
3942
}
4043

4144
val Welcome = FC<WelcomeProps> { props ->
42-
val articles = useArticles(props.data)
45+
val articles = useArticles(props.service)
4346

4447
div {
4548
css {

0 commit comments

Comments
 (0)