diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-10-24 19:54:16 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-10-24 19:54:16 +0200 |
commit | 33215e5c31b55617edf0d95e47e9e720fe4b9dd6 (patch) | |
tree | 11e90cbfaacf37dbe1ce4fd8ce3eb28d02dd4867 | |
parent | 69958b1a08f46aecd6ceda54780f362019150052 (diff) | |
download | kvision-33215e5c31b55617edf0d95e47e9e720fe4b9dd6.tar.gz kvision-33215e5c31b55617edf0d95e47e9e720fe4b9dd6.tar.bz2 kvision-33215e5c31b55617edf0d95e47e9e720fe4b9dd6.zip |
Allow suspending functions for SelectRemote and TabulatorRemote components.
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<T : Any>( value: String? = null, serviceManager: KVServiceManager<T>, - function: T.(String?, String?, String?) -> List<RemoteOption>, + function: suspend T.(String?, String?, String?) -> List<RemoteOption>, stateFunction: (() -> String)? = null, name: String? = null, multiple: Boolean = false, @@ -258,7 +258,7 @@ open class SelectRemote<T : Any>( fun <T : Any> Container.selectRemote( value: String? = null, serviceManager: KVServiceManager<T>, - function: T.(String?, String?, String?) -> List<RemoteOption>, stateFunction: (() -> String)? = null, + function: suspend T.(String?, String?, String?) -> List<RemoteOption>, stateFunction: (() -> String)? = null, name: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, rich: Boolean = false, init: (SelectRemote<T>.() -> Unit)? = null ): SelectRemote<T> { 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<T : Any>( value: String? = null, serviceManager: KVServiceManager<T>, - function: T.(String?, String?, String?) -> List<RemoteOption>, + function: suspend T.(String?, String?, String?) -> List<RemoteOption>, stateFunction: (() -> String)? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, @@ -127,7 +127,7 @@ open class SelectRemoteInput<T : Any>( fun <T : Any> Container.selectRemoteInput( value: String? = null, serviceManager: KVServiceManager<T>, - function: T.(String?, String?, String?) -> List<RemoteOption>, + function: suspend T.(String?, String?, String?) -> List<RemoteOption>, 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<T : Any>(serviceClass: KClass<T>) { * 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<RemoteOption> + protected fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List<RemoteOption> ) /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver */ - protected inline fun <reified RET> bind( - noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> + protected inline fun <reified RET> bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> ) /** 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<T : Any> 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<RemoteOption> + protected actual fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List<RemoteOption> ) { 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<T : Any> 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 <reified RET> bind( - noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> + protected actual inline fun <reified RET> bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> ) { 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<T : Any> actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual fun bind( - function: T.(String?, String?, String?) -> List<RemoteOption> + protected actual fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List<RemoteOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" routes.add { @@ -455,8 +455,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual inline fun <reified RET> bind( - noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> + protected actual inline fun <reified RET> bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> ) { 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<T : Any> actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual fun bind( - function: T.(String?, String?, String?) -> List<RemoteOption> + protected actual fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List<RemoteOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" addRoute(HttpMethod.POST, "/kv/$routeDef") { @@ -486,8 +486,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual inline fun <reified RET> bind( - noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> + protected actual inline fun <reified RET> bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> ) { 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<T : Any> actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual fun bind( - function: T.(String?, String?, String?) -> List<RemoteOption> + protected actual fun bindSelectRemote( + function: suspend T.(String?, String?, String?) -> List<RemoteOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" addRoute(HttpMethod.POST, "/kv/$routeDef") { req, ctx -> @@ -557,8 +557,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: * @param function a function of the receiver */ @Suppress("TooGenericExceptionCaught") - protected actual inline fun <reified RET> bind( - noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> + protected actual inline fun <reified RET> bindTabulatorRemote( + noinline function: suspend T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET> ) { 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<T : Any, E : Any>( serviceManager: KVServiceManager<E>, - function: E.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<T>, + function: suspend E.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<T>, options: TabulatorOptions<T> = TabulatorOptions(), types: Set<TableType> = setOf(), classes: Set<String> = setOf() @@ -98,7 +98,7 @@ open class TabulatorRemote<T : Any, E : Any>( */ fun <T : Any, E : Any> Container.tabulatorRemote( serviceManager: KVServiceManager<E>, - function: E.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<T>, + function: suspend E.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<T>, options: TabulatorOptions<T> = TabulatorOptions(), types: Set<TableType> = setOf(), classes: Set<String> = 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<RemoteOption>" -> 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<RemoteOption>" -> appendln( + " override suspend fun $name(${getParameterList( + params + )}) = emptyList<RemoteOption>()" + ) + 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<RemoteOption>()") - } + 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("}") |