Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #198 from Derek52/master
Browse files Browse the repository at this point in the history
Fix issue that prevented callJSFunctions from running with no arguments.
  • Loading branch information
Derek authored May 17, 2021
2 parents 610438e + a7c32c4 commit 9376fbf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
7 changes: 3 additions & 4 deletions src/main/kotlin/kweb/Kweb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,9 @@ class Kweb private constructor(
logger.debug("Temporarily storing message for ${server2ClientMessage.yourId} in threadlocal outboundMessageCatcher")
val jsFunction = JsFunction(server2ClientMessage.jsId!!, server2ClientMessage.arguments!!)
outboundMessageCatcher.add(jsFunction)
//If we have an outboundMessageCatcher, we do not want to execute the jsCode in this message.
//We still need to send the Server2ClientMessage, to cache the jsCode.
//So, we take the message, and set arguments to null, so the server knows not to run this code.
server2ClientMessage.arguments = null
//Setting `shouldExecute` to false tells the server not to add this jsFunction to the client's cache,
//but to not actually run the code. This is used to pre-cache functions on initial page render.
server2ClientMessage.shouldExecute = false
wsClientData.send(server2ClientMessage)
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/kweb/client/Server2ClientMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ data class Server2ClientMessage(
val js: String? = null,
val parameters: String? = null,
val callbackId: Int? = null,
var arguments: List<JsonElement>? = emptyList()
val arguments: List<JsonElement> = emptyList(),
var shouldExecute: Boolean = true
)
6 changes: 1 addition & 5 deletions src/main/kotlin/kweb/html/events/OnImmediateReceiver.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kweb.html.events

import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.decodeFromJsonElement
import kweb.*
import kweb.plugins.fomanticUI.fomantic
Expand Down Expand Up @@ -139,11 +140,6 @@ fun main() {
label.onImmediate.click {
label.text("Clicked!")
}
val clickButton = button(fomantic.ui.button).text("Click").apply {
on.click {
label.text("Button was clicked")
}
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/kweb/kweb_bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ function handleInboundMessage(msg) {
}
}

//if arguments is null, do not execute the function in this inbound message
if (args == null) return
if (msg["shouldExecute"] === false) return

if (callbackId !== undefined) {
try {
Expand Down

0 comments on commit 9376fbf

Please sign in to comment.