Skip to content

Commit

Permalink
Refactored roomlifecyclemanager, fixed properties backed by fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Oct 25, 2024
1 parent f091c71 commit 1eded83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
25 changes: 16 additions & 9 deletions chat-android/src/main/java/com/ably/chat/RoomLifecycleManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import io.ably.lib.types.ErrorInfo
import io.ably.lib.util.Log.LogHandler
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.suspendCancellableCoroutine
import io.ably.lib.realtime.Channel as AblyRealtimeChannel

/**
Expand Down Expand Up @@ -76,22 +76,27 @@ class DefaultRoomAttachmentResult : RoomAttachmentResult {
internal var _status: RoomLifecycle = RoomLifecycle.Attached
internal var _error: ErrorInfo? = null

override val failedFeature: ResolvedContributor? = _failedFeature
override val failedFeature: ResolvedContributor?
get() = _failedFeature

override val exception: AblyException = AblyException.fromErrorInfo(
_error
?: ErrorInfo("unknown error in attach", ErrorCodes.RoomLifecycleError.errorCode, 500),
)

override val status: RoomLifecycle = _status
override val error: ErrorInfo? = _error
override val status: RoomLifecycle
get() = _status

override val error: ErrorInfo?
get() = _error
}

/**
* An implementation of the `Status` interface.
* @internal
*/
class RoomLifecycleManager
(status: DefaultStatus, contributors: List<ResolvedContributor>, logger: LogHandler?) {
(status: DefaultStatus, contributors: List<ResolvedContributor>, logger: LogHandler? = null) {

/**
* The status of the room.
Expand Down Expand Up @@ -226,9 +231,9 @@ class RoomLifecycleManager
}
}

private suspend fun listenToChannelAttachOrFailure(contributor: ResolvedContributor) = suspendCoroutine { continuation ->
private suspend fun listenToChannelAttachOrFailure(contributor: ResolvedContributor) = suspendCancellableCoroutine { continuation ->
contributor.channel.once(ChannelState.attached) {
continuation.resume(true)
continuation.resume(Unit)
}
contributor.channel.once(ChannelState.failed) {
val exception = AblyException.fromErrorInfo(
Expand All @@ -254,14 +259,16 @@ class RoomLifecycleManager
RoomLifecycle.Releasing ->
throw AblyException.fromErrorInfo(
ErrorInfo(
"Can't ATTACH since room is in RELEASING state",
"unable to attach room; room is releasing",
500,
ErrorCodes.RoomIsReleasing.errorCode,
),
)
RoomLifecycle.Released ->
throw AblyException.fromErrorInfo(
ErrorInfo(
"Can't ATTACH since room is in RELEASED state",
"unable to attach room; room is released",
500,
ErrorCodes.RoomIsReleased.errorCode,
),
)
Expand Down
8 changes: 5 additions & 3 deletions chat-android/src/main/java/com/ably/chat/RoomStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,17 @@ open class RoomStatusEvenEmitter : EventEmitter<RoomLifecycle, RoomStatus.Listen
}
}

class DefaultStatus(private val logger: LogHandler?) : InternalRoomStatus, RoomStatusEvenEmitter() {
class DefaultStatus(private val logger: LogHandler? = null) : InternalRoomStatus, RoomStatusEvenEmitter() {

private val _logger = logger

private var _state = RoomLifecycle.Initializing
override val current: RoomLifecycle = _state
override val current: RoomLifecycle
get() = _state

private var _error: ErrorInfo? = null
override val error: ErrorInfo? = _error
override val error: ErrorInfo?
get() = _error

private val internalEmitter = RoomStatusEvenEmitter()

Expand Down

0 comments on commit 1eded83

Please sign in to comment.