Skip to content

Commit a67ea0c

Browse files
committed
[ECO-5426] Implemented snippet to populate missing fields for objects
1 parent 4e25624 commit a67ea0c

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

live-objects/src/main/kotlin/io/ably/lib/objects/DefaultLiveObjects.kt

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.ably.lib.objects
33
import io.ably.lib.types.Callback
44
import io.ably.lib.types.ProtocolMessage
55
import io.ably.lib.util.Log
6+
import java.util.*
67

78
internal class DefaultLiveObjects(private val channelName: String, private val adapter: LiveObjectsAdapter): LiveObjects {
89
private val tag = DefaultLiveObjects::class.simpleName
@@ -47,14 +48,37 @@ internal class DefaultLiveObjects(private val channelName: String, private val a
4748
TODO("Not yet implemented")
4849
}
4950

50-
fun handle(msg: ProtocolMessage) {
51+
/**
52+
* Handles a ProtocolMessage containing proto action as `object` or `object_sync`.
53+
*/
54+
fun handle(protocolMessage: ProtocolMessage) {
5155
// RTL15b
52-
msg.channelSerial?.let {
53-
if (msg.action === ProtocolMessage.Action.`object`) {
54-
Log.v(tag, "Setting channel serial for channelName: $channelName, value: ${msg.channelSerial}")
55-
adapter.setChannelSerial(channelName, msg.channelSerial)
56-
}
56+
if (protocolMessage.action === ProtocolMessage.Action.`object`) {
57+
setChannelSerial(protocolMessage.channelSerial)
5758
}
59+
60+
if (protocolMessage.state == null || protocolMessage.state.isEmpty()) {
61+
Log.w(tag, "Received ProtocolMessage with null or empty object state, ignoring")
62+
return
63+
}
64+
65+
// OM2 - Populate missing fields from parent
66+
val objects = protocolMessage.state.filterIsInstance<ObjectMessage>().mapIndexed { index, objMsg ->
67+
objMsg.copy(
68+
connectionId = objMsg.connectionId ?: protocolMessage.connectionId, // OM2c
69+
timestamp = objMsg.timestamp ?: protocolMessage.timestamp, // OM2e
70+
id = objMsg.id ?: (protocolMessage.id + ':' + index) // OM2a
71+
)
72+
}
73+
}
74+
75+
private fun setChannelSerial(channelSerial: String?) {
76+
if (channelSerial.isNullOrEmpty()) {
77+
Log.w(tag, "setChannelSerial called with null or empty value, ignoring")
78+
return
79+
}
80+
Log.v(tag, "Setting channel serial for channelName: $channelName, value: $channelSerial")
81+
adapter.setChannelSerial(channelName, channelSerial)
5882
}
5983

6084
fun dispose() {

0 commit comments

Comments
 (0)