[Chat][Bug] Fix the crash when network is disconnected. (#687)

This commit is contained in:
ShaunaSong 2022-12-14 16:37:55 -08:00 коммит произвёл GitHub
Родитель af8220ec7c
Коммит d0c7a3ca1d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 53 добавлений и 30 удалений

Просмотреть файл

@ -35,12 +35,18 @@ internal class TestChatSDK(
chatStatusStateFlow.value = status
}
override fun initialization() {
override fun initialization(): CompletableFuture<Void> {
val future = CompletableFuture<Void>()
// coroutine to make sure requests are not blocking
coroutineScope.launch {
future.complete(null)
}
return future
}
override fun destroy() {}
override fun getAdminUserId(): String? {
override fun getAdminUserId(): String {
return ""
}

Просмотреть файл

@ -178,14 +178,22 @@ internal class ChatActionHandler(private val chatService: ChatService) {
}
private fun initialization(dispatch: Dispatch, threadId: String) {
try {
chatService.initialize()
chatService.getAdminUserId()?.let {
dispatch.invoke(ParticipantAction.ParticipantToHideReceived(it))
chatService.initialize().whenComplete { _, error ->
if (error != null) {
// TODO: lets use only one action and state to fire error for timing
// TODO: while working on error stories, we can create separate states for every error
dispatch(
ErrorAction.ChatStateErrorOccurred(
chatCompositeErrorEvent = ChatCompositeErrorEvent(
threadId,
ChatCompositeErrorCode.JOIN_FAILED,
error,
)
)
)
} else {
dispatch.invoke(ParticipantAction.ParticipantToHideReceived(chatService.getAdminUserId()))
}
} catch (ex: Exception) {
val error = ChatCompositeErrorEvent(threadId, ChatCompositeErrorCode.JOIN_FAILED, ex)
dispatch(ErrorAction.ChatStateErrorOccurred(chatCompositeErrorEvent = error))
}
}

Просмотреть файл

@ -15,9 +15,9 @@ import kotlinx.coroutines.flow.StateFlow
import org.threeten.bp.OffsetDateTime
internal interface ChatSDK {
fun initialization()
fun initialization(): CompletableFuture<Void>
fun destroy()
fun getAdminUserId(): String?
fun getAdminUserId(): String
fun requestPreviousPage()
fun requestChatParticipants()

Просмотреть файл

@ -74,6 +74,7 @@ internal class ChatSDKWrapper(
private val options = ListChatMessagesOptions().apply { maxPageSize = PAGE_MESSAGES_SIZE }
private var pagingContinuationToken: String? = null
private var adminUserId: String = ""
@Volatile
private var allPagesFetched: Boolean = false
@ -90,24 +91,33 @@ internal class ChatSDKWrapper(
override fun getChatEventSharedFlow(): SharedFlow<ChatEventModel> =
chatEventModelSharedFlow
override fun initialization() {
chatStatusStateFlow.value = ChatStatus.INITIALIZATION
createChatClient()
createChatThreadClient()
// TODO: initialize polling or try to get first message here to make sure SDK can establish connection with thread
// TODO: above will make sure, network is connected as well
onChatEventReceived(
infoModel = ChatEventModel(
eventType = ChatEventType.CHAT_THREAD_PROPERTIES_UPDATED,
ChatThreadInfoModel(
topic = threadClient.properties.topic,
receivedOn = threadClient.properties.createdOn
),
eventReceivedOffsetDateTime = null
override fun initialization(): CompletableFuture<Void> {
val future = CompletableFuture<Void>()
try {
chatStatusStateFlow.value = ChatStatus.INITIALIZATION
createChatClient()
createChatThreadClient()
// TODO: initialize polling or try to get first message here to make sure SDK can establish connection with thread
// TODO: above will make sure, network is connected as well
onChatEventReceived(
infoModel = ChatEventModel(
eventType = ChatEventType.CHAT_THREAD_PROPERTIES_UPDATED,
ChatThreadInfoModel(
topic = threadClient.properties.topic,
receivedOn = threadClient.properties.createdOn
),
eventReceivedOffsetDateTime = null
)
)
)
chatStatusStateFlow.value = ChatStatus.INITIALIZED
adminUserId = threadClient.properties.createdByCommunicationIdentifier.into().id
chatStatusStateFlow.value = ChatStatus.INITIALIZED
future.complete(null)
} catch (ex: Exception) {
future.completeExceptionally(ex)
logger.debug("sendMessage failed.", ex)
}
return future
}
override fun destroy() {
@ -118,9 +128,8 @@ internal class ChatSDKWrapper(
chatFetchNotificationHandler.stop()
}
override fun getAdminUserId(): String? {
if (!this::threadClient.isInitialized) return null
return threadClient.properties.createdByCommunicationIdentifier.into().id
override fun getAdminUserId(): String {
return adminUserId
}
override fun requestPreviousPage() {