Skip to content

Commit

Permalink
Refactored room interfaces, implemented DefaultRoomStatus class that
Browse files Browse the repository at this point in the history
extends internal eventemitter
  • Loading branch information
sacOO7 committed Oct 11, 2024
1 parent 3067774 commit 8ffabb1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
4 changes: 2 additions & 2 deletions chat-android/src/main/java/com/ably/chat/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface Room {
*
* @returns The status observable.
*/
val status: IRoomStatus
val status: RoomStatus

/**
* Returns the room options.
Expand Down Expand Up @@ -92,7 +92,7 @@ internal class DefaultRoom(
override val options: RoomOptions,
realtimeClient: RealtimeClient,
chatApi: ChatApi,
override val status: IRoomStatus = RoomStatus(RoomLifecycle.Initialized, null),
override val status: RoomStatus = DefaultRoomStatus(RoomLifecycle.Initialized, null),
val logger: LogHandler?,
) : Room {

Expand Down
80 changes: 72 additions & 8 deletions chat-android/src/main/java/com/ably/chat/RoomStatus.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.ably.chat

import io.ably.lib.types.ErrorInfo
import io.ably.lib.util.EventEmitter

/**
* Represents the status of a Room.
*/
interface IRoomStatus {
interface RoomStatus {
/**
* The current status of the room.
*/
Expand All @@ -21,7 +22,7 @@ interface IRoomStatus {
* @param listener The function to call when the status changes.
* @returns An object that can be used to unregister the listener.
*/
fun on(listener: Listener): Subscription
fun onChange(listener: Listener): Subscription

/**
* An interface for listening to changes for the room status
Expand All @@ -40,26 +41,89 @@ interface IRoomStatus {
fun offAll();
}

class RoomStatus(override val current: RoomLifecycle, override val error: ErrorInfo?) : IRoomStatus {
/**
* A new room status that can be set.
*/
interface NewRoomStatus {
/**
* The new status of the room.
*/
val status: RoomLifecycle;

private var listeners = mutableListOf<IRoomStatus.Listener>()
/**
* An error that provides a reason why the room has
* entered the new status, if applicable.
*/
val error: ErrorInfo?
}

interface InternalRoomStatus: RoomStatus {
/**
* Registers a listener that will be called once when the room status changes.
* @param listener The function to call when the status changes.
*/
fun onChangeOnce(listener: RoomStatus.Listener)

override fun on(listener: IRoomStatus.Listener): Subscription {
listeners.add(listener)
/**
* Sets the status of the room.
*
* @param params The new status of the room.
*/
fun setStatus(params: NewRoomStatus)
}

open class ChatEventEmitter<Event, Listener>: EventEmitter<Event, Listener>() {
override fun apply(listener: Listener, event: Event, vararg args: Any?) {
TODO("Not yet implemented")
}
}

class DefaultRoomStatus() : InternalRoomStatus,
ChatEventEmitter<RoomLifecycle, RoomStatus.Listener>() {

private var _state = RoomLifecycle.Initializing
private var _error: ErrorInfo? = null

private val internalEmitter = ChatEventEmitter<RoomLifecycle, RoomStatus.Listener>()

override fun onChange(listener: RoomStatus.Listener): Subscription {
this.on(listener);
return Subscription {
listeners.remove(listener)
this.off(listener)
}
}

override fun offAll() {
listeners.clear();
this.offAll()
}

override fun onChangeOnce(listener: RoomStatus.Listener) {
internalEmitter.once(listener)
}

override fun setStatus(params: NewRoomStatus) {
val change = RoomStatusChange(params.status, current, params.error);
this._state = change.current
this._error = change.error
this.internalEmitter.emit(change.current, change)
this.emit(change.current, change)
}

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

/**
* The different states that a room can be in throughout its lifecycle.
*/
enum class RoomLifecycle(val stateName: String) {
/**
* The library is currently initializing the room.
*/
Initializing("initializing"),

/**
* A temporary state for when the library is first initialized.
*/
Expand Down

0 comments on commit 8ffabb1

Please sign in to comment.