Skip to content

Commit

Permalink
Initial work to encode/decode data by providing Strada a custom app e…
Browse files Browse the repository at this point in the history
…ncoder
  • Loading branch information
jayohms committed Aug 9, 2023
1 parent b9e41a7 commit fb848b7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 23 deletions.
28 changes: 22 additions & 6 deletions strada/src/main/kotlin/dev/hotwire/strada/BridgeComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,18 @@ abstract class BridgeComponent<in D : BridgeDestination>(
*/
protected abstract fun onReceive(message: Message)

/**
* Returns the last received message for a given `event`, if available.
*/
protected fun receivedMessageFor(event: String): Message? {
return receivedMessages[event]
}

/**
* Reply to the web with a received message, optionally replacing its
* `event` or `jsonData`.
*/
protected fun replyWith(message: Message): Boolean {
fun replyWith(message: Message): Boolean {
return reply(message)
}

Expand All @@ -54,7 +61,7 @@ abstract class BridgeComponent<in D : BridgeDestination>(
* NOTE: If a message has not been received for the given `event`, the
* reply will be ignored.
*/
protected fun replyTo(event: String): Boolean {
fun replyTo(event: String): Boolean {
val message = receivedMessageFor(event) ?: run {
logEvent("bridgeMessageFailedToReply", "message for event '$event' was not received")
return false
Expand All @@ -70,7 +77,7 @@ abstract class BridgeComponent<in D : BridgeDestination>(
* NOTE: If a message has not been received for the given `event`, the
* reply will be ignored.
*/
protected fun replyTo(event: String, jsonData: String): Boolean {
fun replyTo(event: String, jsonData: String): Boolean {
val message = receivedMessageFor(event) ?: run {
logEvent("bridgeMessageFailedToReply", "message for event '$event' was not received")
return false
Expand All @@ -80,10 +87,19 @@ abstract class BridgeComponent<in D : BridgeDestination>(
}

/**
* Returns the last received message for a given `event`, if available.
* Reply to the web with the last received message for a given `event`,
* replacing its `jsonData` with encoded json from the provided `data`
* object.
*
* NOTE: If a message has not been received for the given `event`, the
* reply will be ignored.
*/
protected fun receivedMessageFor(event: String): Message? {
return receivedMessages[event]
inline fun <reified T> replyTo(event: String, data: T): Boolean {
val encoder = requireNotNull(Strada.config.jsonEncoder) {
"A Strada.config.jsonEncoder must be set to decode and encode data"
}

return replyTo(event, jsonData = encoder.toJson(data, T::class.java))
}

private fun reply(message: Message): Boolean {
Expand Down
19 changes: 19 additions & 0 deletions strada/src/main/kotlin/dev/hotwire/strada/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ data class Message internal constructor(
metadata = this.metadata,
jsonData = jsonData
)

inline fun <reified T> replacing(
event: String = this.event,
data: T
): Message {
val encoder = requireNotNull(Strada.config.jsonEncoder) {
"A Strada.config.jsonEncoder must be set to decode and encode data"
}

return replacing(event, encoder.toJson(data, T::class.java))
}

inline fun <reified T> data(): T? {
val encoder = requireNotNull(Strada.config.jsonEncoder) {
"A Strada.config.jsonEncoder must be set to decode and encode data"
}

return encoder.toObject(jsonData, T::class.java)
}
}

data class Metadata(
Expand Down
5 changes: 5 additions & 0 deletions strada/src/main/kotlin/dev/hotwire/strada/Strada.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.hotwire.strada

object Strada {
val config: StradaConfig = StradaConfig()
}
5 changes: 5 additions & 0 deletions strada/src/main/kotlin/dev/hotwire/strada/StradaConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.hotwire.strada

class StradaConfig {
var jsonEncoder: StradaJsonEncoder? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.hotwire.strada

abstract class StradaJsonEncoder {
abstract fun <T> toObject(jsonData: String, type: Class<T>): T?
abstract fun <T> toJson(data: T, type: Class<T>): String
}
10 changes: 5 additions & 5 deletions strada/src/test/kotlin/dev/hotwire/strada/BridgeComponentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class BridgeComponentTest {
val newJsonData = """{"title":"Page-title"}"""
val newMessage = message.replacing(jsonData = newJsonData)

val replied = component.replyWithPublic(newMessage)
val replied = component.replyWith(newMessage)
assertEquals(true, replied)
verify(bridge).replyWith(eq(newMessage))
}
Expand All @@ -75,7 +75,7 @@ class BridgeComponentTest {
fun replyTo() {
component.didReceive(message)

val replied = component.replyToPublic("connect")
val replied = component.replyTo("connect")
assertEquals(true, replied)
verify(bridge).replyWith(eq(message))
}
Expand All @@ -87,14 +87,14 @@ class BridgeComponentTest {

component.didReceive(message)

val replied = component.replyToPublic("connect", newJsonData)
val replied = component.replyTo("connect", newJsonData)
assertEquals(true, replied)
verify(bridge).replyWith(eq(newMessage))
}

@Test
fun replyToIgnoresNotReceived() {
val replied = component.replyToPublic("connect")
val replied = component.replyTo("connect")
assertEquals(false, replied)
verify(bridge, never()).replyWith(any())
}
Expand All @@ -103,7 +103,7 @@ class BridgeComponentTest {
fun replyToReplacingDataIgnoresNotReceived() {
val newJsonData = """{"title":"Page-title"}"""

val replied = component.replyToPublic("connect", newJsonData)
val replied = component.replyTo("connect", newJsonData)
assertEquals(false, replied)
verify(bridge, never()).replyWith(any())
}
Expand Down
12 changes: 0 additions & 12 deletions strada/src/test/kotlin/dev/hotwire/strada/TestData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ object TestData {

override fun onReceive(message: Message) {}

fun replyWithPublic(message: Message): Boolean {
return replyWith(message)
}

fun replyToPublic(event: String): Boolean {
return replyTo(event)
}

fun replyToPublic(event: String, jsonData: String): Boolean {
return replyTo(event, jsonData)
}

fun receivedMessageForPublic(event: String): Message? {
return receivedMessageFor(event)
}
Expand Down

0 comments on commit fb848b7

Please sign in to comment.