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 34add29
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 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
71 changes: 61 additions & 10 deletions chat-android/src/main/java/com/ably/chat/RoomStatus.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
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.
*/
val current: RoomLifecycle
var current: RoomLifecycle

/**
* The current error, if any, that caused the room to enter the current status.
*/
val error: ErrorInfo?
var error: ErrorInfo?

/**
* Registers a listener that will be called whenever the room status changes.
* @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,19 +41,69 @@ 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?
}

override fun on(listener: IRoomStatus.Listener): Subscription {
listeners.add(listener)
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)

/**
* 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(override var current: RoomLifecycle, override var error: ErrorInfo?) : InternalRoomStatus,
ChatEventEmitter<RoomLifecycle, RoomStatus.Listener>() {

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.current = change.current
this.error = change.error
this.internalEmitter.emit(change.current, change)
this.emit(change.current, change)
}
}

Expand Down

0 comments on commit 34add29

Please sign in to comment.