@@ -3,6 +3,7 @@ package io.ably.lib.objects
33import io.ably.lib.types.Callback
44import io.ably.lib.types.ProtocolMessage
55import io.ably.lib.util.Log
6+ import java.util.*
67
78internal 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