Skip to content

Commit 49d0fdc

Browse files
committed
refactor(light-model-client): LightModelClientJVM replaced with platform specific actual classes
1 parent b7d0ad9 commit 49d0fdc

File tree

7 files changed

+55
-6
lines changed

7 files changed

+55
-6
lines changed

docs/global/modules/core/pages/howto/usage-light-model-client.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Creating an instance that loads the entire model from the server can be done lik
99

1010
[source,kotlin]
1111
--
12-
val client = LightModelClientJVM.builder()
12+
val client = LightModelClient.builder()
1313
.url("ws://localhost/json/v2/test-repo/ws") // optional, by default it connects to the MPS plugin
1414
.build()
1515
--

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
systemProp.org.gradle.internal.http.socketTimeout=120000
2+
kotlin.daemon.jvmargs=-Xmx2G
23

34
kotlinVersion=1.8.20
45
kotlinCoroutinesVersion=1.6.4

light-model-client/src/commonMain/kotlin/org/modelix/client/light/LightModelClient.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.modelix.model.area.IArea
99
import org.modelix.model.area.IAreaListener
1010
import org.modelix.model.area.IAreaReference
1111
import org.modelix.model.server.api.*
12-
import kotlin.reflect.KClass
12+
import kotlin.jvm.JvmStatic
1313
import kotlin.time.Duration
1414
import kotlin.time.Duration.Companion.milliseconds
1515
import kotlin.time.Duration.Companion.seconds
@@ -660,7 +660,8 @@ class LightModelClient(val connection: IConnection, val debugName: String = "")
660660
LightClientReferenceSerializer.register()
661661
}
662662

663-
fun builder() = LightModelClientBuilder()
663+
@JvmStatic
664+
fun builder(): LightModelClientBuilder = PlatformSpecificLightModelClientBuilder()
664665
}
665666

666667
interface IConnection {
@@ -676,7 +677,9 @@ class LightModelClient(val connection: IConnection, val debugName: String = "")
676677
}
677678
}
678679

679-
class LightModelClientBuilder {
680+
expect class PlatformSpecificLightModelClientBuilder() : LightModelClientBuilder
681+
682+
abstract class LightModelClientBuilder {
680683
private var host: String = "localhost"
681684
private var port: Int = 48302
682685
private var url: String? = null
@@ -686,11 +689,13 @@ class LightModelClientBuilder {
686689
private var httpEngineFactory: HttpClientEngineFactory<*>? = null
687690
private var debugName: String = ""
688691

692+
protected abstract fun getDefaultEngineFactory(): HttpClientEngineFactory<*>
693+
689694
fun build(): LightModelClient {
690695
return LightModelClient(
691696
connection ?: (
692697
WebsocketConnection((httpClient ?: (
693-
httpEngine?.let { HttpClient(it) } ?: httpEngineFactory?.let { HttpClient(it) } ?: HttpClient()
698+
httpEngine?.let { HttpClient(it) } ?: (httpEngineFactory ?: getDefaultEngineFactory()).let { HttpClient(it) }
694699
)
695700
).config {
696701
install(WebSockets)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.modelix.client.light
15+
16+
import io.ktor.client.engine.*
17+
import io.ktor.client.engine.js.*
18+
19+
actual class PlatformSpecificLightModelClientBuilder actual constructor() : LightModelClientBuilder() {
20+
override fun getDefaultEngineFactory(): HttpClientEngineFactory<*> = Js
21+
}

light-model-client/src/jvmMain/kotlin/org/modelix/client/light/LightModelClientJVM.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package org.modelix.client.light
1515

1616
import io.ktor.client.engine.cio.*
1717

18+
@Deprecated("Use LightModelClient", ReplaceWith("LightModelClient"))
1819
object LightModelClientJVM {
1920
@JvmStatic
2021
fun builder() = LightModelClient.builder().also { it.httpEngine(CIO) }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.modelix.client.light
15+
16+
import io.ktor.client.engine.*
17+
import io.ktor.client.engine.cio.*
18+
19+
actual class PlatformSpecificLightModelClientBuilder : LightModelClientBuilder() {
20+
override fun getDefaultEngineFactory(): HttpClientEngineFactory<*> = CIO
21+
}

light-model-client/src/jvmTest/kotlin/org/modelix/client/light/LightModelClientTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class LightModelClientTest {
6363
//println("init: $response")
6464

6565
val createClient: suspend (debugName: String)->LightModelClient = { debugName ->
66-
val client = LightModelClientJVM.builder()
66+
val client = LightModelClient.builder()
6767
.httpClient(httpClient)
6868
.url("ws://localhost/json/v2/test-repo/ws")
6969
.debugName(debugName)

0 commit comments

Comments
 (0)