From 33215e5c31b55617edf0d95e47e9e720fe4b9dd6 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Thu, 24 Oct 2019 19:54:16 +0200 Subject: Allow suspending functions for SelectRemote and TabulatorRemote components. --- .../treksoft/kvision/form/select/SelectRemote.kt | 4 +-- .../kvision/form/select/SelectRemoteInput.kt | 4 +-- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 8 ++--- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 8 ++--- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 8 ++--- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 8 ++--- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 8 ++--- .../treksoft/kvision/tabulator/TabulatorRemote.kt | 4 +-- .../pl/treksoft/kvision/plugin/KVProcessor.kt | 41 +++++++++++++--------- 9 files changed, 50 insertions(+), 43 deletions(-) diff --git a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt index 1cabb5dd..74cb3694 100644 --- a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt +++ b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt @@ -51,7 +51,7 @@ import pl.treksoft.kvision.utils.SnOn open class SelectRemote( value: String? = null, serviceManager: KVServiceManager, - function: T.(String?, String?, String?) -> List, + function: suspend T.(String?, String?, String?) -> List, stateFunction: (() -> String)? = null, name: String? = null, multiple: Boolean = false, @@ -258,7 +258,7 @@ open class SelectRemote( fun Container.selectRemote( value: String? = null, serviceManager: KVServiceManager, - function: T.(String?, String?, String?) -> List, stateFunction: (() -> String)? = null, + function: suspend T.(String?, String?, String?) -> List, stateFunction: (() -> String)? = null, name: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, rich: Boolean = false, init: (SelectRemote.() -> Unit)? = null ): SelectRemote { diff --git a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt index e44c63d5..af3f1a52 100644 --- a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt +++ b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt @@ -54,7 +54,7 @@ external fun decodeURIComponent(encodedURI: String): String open class SelectRemoteInput( value: String? = null, serviceManager: KVServiceManager, - function: T.(String?, String?, String?) -> List, + function: suspend T.(String?, String?, String?) -> List, stateFunction: (() -> String)? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, @@ -127,7 +127,7 @@ open class SelectRemoteInput( fun Container.selectRemoteInput( value: String? = null, serviceManager: KVServiceManager, - function: T.(String?, String?, String?) -> List, + function: suspend T.(String?, String?, String?) -> List, stateFunction: (() -> String)? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 9f07948d..3d0b5115 100644 --- a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -114,16 +114,16 @@ expect open class KVServiceManager(serviceClass: KClass) { * Binds a given function of the receiver as a select options source * @param function a function of the receiver */ - protected fun bind( - function: T.(String?, String?, String?) -> List + protected fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List ) /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver */ - protected inline fun bind( - noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + protected inline fun bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List?, List?) -> RemoteData ) /** diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index dd54a979..5c7cac7f 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -132,8 +132,8 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl * Binds a given function of the receiver as a select options source * @param function a function of the receiver */ - protected actual fun bind( - function: T.(String?, String?, String?) -> List + protected actual fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List ) { val routeDef = "route${this::class.simpleName}${counter++}" calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", HttpMethod.POST) @@ -143,8 +143,8 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver */ - protected actual inline fun bind( - noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + protected actual inline fun bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List?, List?) -> RemoteData ) { val routeDef = "route${this::class.simpleName}${counter++}" calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", HttpMethod.POST) diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index fd95be3e..6bb28059 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -416,8 +416,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual fun bind( - function: T.(String?, String?, String?) -> List + protected actual fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List ) { val routeDef = "route${this::class.simpleName}${counter++}" routes.add { @@ -455,8 +455,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual inline fun bind( - noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + protected actual inline fun bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List?, List?) -> RemoteData ) { val routeDef = "route${this::class.simpleName}${counter++}" routes.add { diff --git a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index e550be9c..4d19e301 100644 --- a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -442,8 +442,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual fun bind( - function: T.(String?, String?, String?) -> List + protected actual fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List ) { val routeDef = "route${this::class.simpleName}${counter++}" addRoute(HttpMethod.POST, "/kv/$routeDef") { @@ -486,8 +486,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual inline fun bind( - noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + protected actual inline fun bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List?, List?) -> RemoteData ) { val routeDef = "route${this::class.simpleName}${counter++}" addRoute(HttpMethod.POST, "/kv/$routeDef") { diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 25ad52ed..295ec8fb 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -506,8 +506,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual fun bind( - function: T.(String?, String?, String?) -> List + protected actual fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List ) { val routeDef = "route${this::class.simpleName}${counter++}" addRoute(HttpMethod.POST, "/kv/$routeDef") { req, ctx -> @@ -557,8 +557,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual inline fun bind( - noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + protected actual inline fun bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List?, List?) -> RemoteData ) { val routeDef = "route${this::class.simpleName}${counter++}" addRoute(HttpMethod.POST, "/kv/$routeDef") { req, ctx -> diff --git a/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/TabulatorRemote.kt b/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/TabulatorRemote.kt index d37f3ea9..33cf44b3 100644 --- a/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/TabulatorRemote.kt +++ b/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/TabulatorRemote.kt @@ -49,7 +49,7 @@ import pl.treksoft.kvision.utils.JSON @UseExperimental(ImplicitReflectionSerializer::class) open class TabulatorRemote( serviceManager: KVServiceManager, - function: E.(Int?, Int?, List?, List?) -> RemoteData, + function: suspend E.(Int?, Int?, List?, List?) -> RemoteData, options: TabulatorOptions = TabulatorOptions(), types: Set = setOf(), classes: Set = setOf() @@ -98,7 +98,7 @@ open class TabulatorRemote( */ fun Container.tabulatorRemote( serviceManager: KVServiceManager, - function: E.(Int?, Int?, List?, List?) -> RemoteData, + function: suspend E.(Int?, Int?, List?, List?) -> RemoteData, options: TabulatorOptions = TabulatorOptions(), types: Set = setOf(), classes: Set = setOf(), diff --git a/kvision-tools/kvision-compiler-plugin/src/main/kotlin/pl/treksoft/kvision/plugin/KVProcessor.kt b/kvision-tools/kvision-compiler-plugin/src/main/kotlin/pl/treksoft/kvision/plugin/KVProcessor.kt index 0e62b959..bf01b463 100644 --- a/kvision-tools/kvision-compiler-plugin/src/main/kotlin/pl/treksoft/kvision/plugin/KVProcessor.kt +++ b/kvision-tools/kvision-compiler-plugin/src/main/kotlin/pl/treksoft/kvision/plugin/KVProcessor.kt @@ -28,7 +28,6 @@ import de.jensklingenberg.mpapt.model.Element import de.jensklingenberg.mpapt.model.RoundEnvironment import de.jensklingenberg.mpapt.utils.KotlinPlatformValues import org.jetbrains.kotlin.backend.common.descriptors.allParameters -import org.jetbrains.kotlin.backend.common.descriptors.isSuspend import org.jetbrains.kotlin.cli.common.config.kotlinSourceRoots import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.ParameterDescriptor @@ -85,7 +84,11 @@ class KVProcessor : AbstractProcessor() { appendln(" init {") appendln(" GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {") cl.methods().forEach { - appendln(" bind($iName::${it.name})") + when { + it.returnType.toString().startsWith("RemoteData") -> appendln(" bindTabulatorRemote($iName::${it.name})") + it.returnType.toString() == "List" -> appendln(" bindSelectRemote($iName::${it.name})") + else -> appendln(" bind($iName::${it.name})") + } } appendln(" }") appendln(" }") @@ -125,29 +128,33 @@ class KVProcessor : AbstractProcessor() { if (params.size == 2) params.first().type.toString().startsWith("ReceiveChannel") else false - if (it.isSuspend) { - if (!wsMethod) { - if (params.isNotEmpty()) { - appendln( + if (!wsMethod) { + if (params.isNotEmpty()) { + when { + it.returnType.toString().startsWith("RemoteData") -> appendln( + " override suspend fun $name(${getParameterList( + params + )}) = ${it.returnType.toString()}()" + ) + it.returnType.toString() == "List" -> appendln( + " override suspend fun $name(${getParameterList( + params + )}) = emptyList()" + ) + else -> appendln( " override suspend fun $name(${getParameterList(params)}) = call($iName::$name, ${getParameterNames( params )})" ) - } else { - appendln(" override suspend fun $name() = call($iName::$name)") } } else { - appendln(" override suspend fun $name(${getParameterList(params)}) {}") - val type1 = params[0].type.toString().replace("ReceiveChannel", "SendChannel") - val type2 = params[1].type.toString().replace("SendChannel", "ReceiveChannel") - appendln(" suspend fun $name(handler: suspend ($type1, $type2) -> Unit) = webSocket($iName::$name, handler)") + appendln(" override suspend fun $name() = call($iName::$name)") } } else { - if (it.returnType.toString().startsWith("RemoteData")) { - appendln(" override fun $name(${getParameterList(params)}) = ${it.returnType.toString()}()") - } else { - appendln(" override fun $name(${getParameterList(params)}) = emptyList()") - } + appendln(" override suspend fun $name(${getParameterList(params)}) {}") + val type1 = params[0].type.toString().replace("ReceiveChannel", "SendChannel") + val type2 = params[1].type.toString().replace("SendChannel", "ReceiveChannel") + appendln(" suspend fun $name(handler: suspend ($type1, $type2) -> Unit) = webSocket($iName::$name, handler)") } } appendln("}") -- cgit