From e664204d1d0ed5f39aed33b1941cfe9f1e470db2 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 4 Mar 2019 19:35:13 +0100 Subject: Inlining some utility functions. --- .../kotlin/pl/treksoft/kvision/utils/Snabbdom.kt | 53 +++++++++++++++------- 1 file changed, 36 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/main/kotlin/pl/treksoft/kvision/utils/Snabbdom.kt b/src/main/kotlin/pl/treksoft/kvision/utils/Snabbdom.kt index 7bb18a58..10171ae0 100644 --- a/src/main/kotlin/pl/treksoft/kvision/utils/Snabbdom.kt +++ b/src/main/kotlin/pl/treksoft/kvision/utils/Snabbdom.kt @@ -43,12 +43,26 @@ external class Object /** * Helper function for creating JavaScript objects. */ -fun obj(init: dynamic.() -> Unit): dynamic { +inline fun obj(init: dynamic.() -> Unit): dynamic { return (Object()).apply(init) } -@Suppress("UnsafeCastFromDynamic") -private fun vNodeData(): VNodeData = js("({})") +/** + * Helper function for creating JavaScript objects from dynamic constructors. + */ +@Suppress("UNUSED_VARIABLE") +inline fun Any?.createInstance(vararg args: dynamic): T { + val jsClass = this + val argsArray = (listOf(null) + args).toTypedArray() + return js("new (Function.prototype.bind.apply(jsClass, argsArray))").unsafeCast() +} + +/** + * @suppress + * Internal function + */ +@Suppress("UnsafeCastFromDynamic", "NOTHING_TO_INLINE") +inline fun vNodeData(): VNodeData = js("{}") /** * @suppress @@ -65,7 +79,7 @@ interface KvJQueryEventObject : JQueryEventObject { */ @Suppress("UnsafeCastFromDynamic") class KvEvent(type: String, eventInitDict: CustomEventInit) : CustomEvent(type, eventInitDict) { - override val detail: KvJQueryEventObject = obj {} + override val detail: KvJQueryEventObject = js("{}") } /** @@ -116,25 +130,30 @@ interface SnOn : BtOn { /** * Helper function for creating object parameters for Snabbdom. */ -fun snOpt(block: VNodeData.() -> Unit) = (vNodeData()::apply)(block) +@Suppress("NOTHING_TO_INLINE") +inline fun snOpt(noinline block: VNodeData.() -> Unit) = (vNodeData()::apply)(block) +/** + * @suppress + * Internal function. + */ @Suppress("UnsafeCastFromDynamic") internal fun on(widget: Widget): SnOn { - val obj = js("({})") + val obj = js("{}") obj["self"] = widget return obj } -@Suppress("UnsafeCastFromDynamic") -internal fun hooks(): Hooks { - return js("({})") +@Suppress("UnsafeCastFromDynamic", "NOTHING_TO_INLINE") +internal inline fun hooks(): Hooks { + return js("{}") } /** * Helper function for creating style parameters for Snabbdom. */ -@Suppress("UnsafeCastFromDynamic") -fun snStyle(pairs: List): VNodeStyle { +@Suppress("UnsafeCastFromDynamic", "NOTHING_TO_INLINE") +inline fun snStyle(pairs: List): VNodeStyle { return obj { pairs.forEach { (key, value) -> this[key] = value } } @@ -143,8 +162,8 @@ fun snStyle(pairs: List): VNodeStyle { /** * Helper function for creating properties parameters for Snabbdom. */ -@Suppress("UnsafeCastFromDynamic") -fun snProps(pairs: List): Props { +@Suppress("UnsafeCastFromDynamic", "NOTHING_TO_INLINE") +inline fun snProps(pairs: List): Props { return obj { pairs.forEach { (key, value) -> this[key] = value } } @@ -153,8 +172,8 @@ fun snProps(pairs: List): Props { /** * Helper function for creating classes parameters for Snabbdom. */ -@Suppress("UnsafeCastFromDynamic") -fun snClasses(pairs: List): Classes { +@Suppress("UnsafeCastFromDynamic", "NOTHING_TO_INLINE") +inline fun snClasses(pairs: List): Classes { return obj { pairs.forEach { (key, value) -> this[key] = value } } @@ -163,8 +182,8 @@ fun snClasses(pairs: List): Classes { /** * Helper function for creating attributes parameters for Snabbdom. */ -@Suppress("UnsafeCastFromDynamic") -fun snAttrs(pairs: List): Attrs { +@Suppress("UnsafeCastFromDynamic", "NOTHING_TO_INLINE") +inline fun snAttrs(pairs: List): Attrs { return obj { pairs.forEach { (key, value) -> this[key] = value } } -- cgit