Skip to content

Commit 1ad1f79

Browse files
committed
[ECO-5426] Refactored code as per review comments
1 parent d9f4e42 commit 1ad1f79

4 files changed

Lines changed: 12 additions & 27 deletions

File tree

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ internal fun LiveObjectsAdapter.ensureMessageSizeWithinLimit(objectMessages: Arr
3333
}
3434

3535
internal fun LiveObjectsAdapter.setChannelSerial(channelName: String, protocolMessage: ProtocolMessage) {
36-
if (protocolMessage.action == ProtocolMessage.Action.`object`) {
37-
val channelSerial = protocolMessage.channelSerial
38-
if (channelSerial.isNullOrEmpty()) return
39-
setChannelSerial(channelName, channelSerial)
40-
}
36+
if (protocolMessage.action != ProtocolMessage.Action.`object`) return
37+
val channelSerial = protocolMessage.channelSerial
38+
if (channelSerial.isNullOrEmpty()) return
39+
setChannelSerial(channelName, channelSerial)
4140
}
4241

4342
internal enum class ProtocolMessageFormat(private val value: String) {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ internal class ObjectId private constructor(
1818
/**
1919
* Creates ObjectId instance from hashed object id string.
2020
*/
21-
fun fromString(objectId: String?): ObjectId {
22-
if (objectId.isNullOrEmpty()) {
21+
fun fromString(objectId: String): ObjectId {
22+
if (objectId.isEmpty()) {
2323
throw objectError("Invalid object id: $objectId")
2424
}
2525

@@ -29,8 +29,7 @@ internal class ObjectId private constructor(
2929
throw objectError("Invalid object id: $objectId")
3030
}
3131

32-
val typeStr = parts[0]
33-
val rest = parts[1]
32+
val (typeStr, rest) = parts
3433

3534
val type = when (typeStr) {
3635
"map" -> ObjectType.Map

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ internal class ObjectsPool(
3838

3939
/**
4040
* @spec RTO3a - Pool storing all live objects by object ID
41-
* Note: This is the same as objectsPool property in DefaultLiveObjects.kt
4241
*/
4342
private val pool = mutableMapOf<String, BaseLiveObject>()
4443

@@ -88,25 +87,17 @@ internal class ObjectsPool(
8887
* Does not create a new root object, so the reference to the root object remains the same.
8988
*/
9089
internal fun resetToInitialPool(emitUpdateEvents: Boolean) {
91-
// Clear the pool first and keep the root object
92-
val root = pool[ROOT_OBJECT_ID]
93-
if (root != null) {
94-
pool.clear()
95-
set(ROOT_OBJECT_ID, root)
96-
97-
// this will only clear the remaining root object and emit update events
98-
clearObjectsData(emitUpdateEvents)
99-
} else {
100-
Log.w(tag, "Root object not found in pool during reset")
101-
}
90+
pool.entries.removeIf { (key, _) -> key != ROOT_OBJECT_ID } // only keep the root object
91+
clearObjectsData(emitUpdateEvents) // clear the root object and emit update events
10292
}
10393

10494

10595
/**
10696
* Deletes objects from the pool for which object ids are not found in the provided array of ids.
97+
* Spec: RTO5c2
10798
*/
10899
internal fun deleteExtraObjectIds(objectIds: MutableSet<String>) {
109-
pool.entries.removeIf { (key, _) -> key !in objectIds }
100+
pool.entries.removeIf { (key, _) -> key !in objectIds && key != ROOT_OBJECT_ID } // RTO5c2a - Keep root object
110101
}
111102

112103
/**

live-objects/src/test/kotlin/io/ably/lib/objects/unit/ObjectIdTest.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ class ObjectIdTest {
3737
}
3838

3939
@Test
40-
fun testNullOrEmptyObjectId() {
41-
val exception = assertThrows(AblyException::class.java) {
42-
ObjectId.fromString(null)
43-
}
44-
assertAblyExceptionError(exception)
40+
fun testEmptyObjectId() {
4541
val exception1 = assertThrows(AblyException::class.java) {
4642
ObjectId.fromString("")
4743
}

0 commit comments

Comments
 (0)