Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DROID-2966 Chats | Tech | Ensure io thread for chats #1902

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlin.collections.isNotEmpty
import kotlin.collections.toList
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.emitAll
Expand All @@ -32,13 +33,11 @@ class ChatContainer @Inject constructor(
) {
private val payloads = MutableSharedFlow<List<Event.Command.Chats>>()

private val attachments = MutableSharedFlow<Set<Id>>(replay = 0)
private val replies = MutableSharedFlow<Set<Id>>(replay = 0)
private val attachments = MutableStateFlow<Set<Id>>(emptySet())
private val replies = MutableStateFlow<Set<Id>>(emptySet())

@Deprecated("Naive implementation. Add caching logic - maybe store for wrappers")
fun fetchAttachments(space: Space) : Flow<Map<Id, ObjectWrapper.Basic>> {
return attachments
.distinctUntilChanged()
.map { ids ->
if (ids.isNotEmpty()) {
repo.searchObjects(
Expand Down Expand Up @@ -68,7 +67,6 @@ class ChatContainer @Inject constructor(
@Deprecated("Naive implementation. Add caching logic")
fun fetchReplies(chat: Id) : Flow<Map<Id, Chat.Message>> {
return replies
.distinctUntilChanged()
.map { ids ->
if (ids.isNotEmpty()) {
repo.getChatMessagesByIds(
Expand Down Expand Up @@ -96,8 +94,8 @@ class ChatContainer @Inject constructor(
repliesIds.add(msg.replyToMessageId.orEmpty())
}
}
attachments.emit(attachmentsIds)
replies.emit(repliesIds)
attachments.value = attachmentsIds
replies.value = repliesIds
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import javax.inject.Inject
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.combineLatest
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import timber.log.Timber

Expand Down Expand Up @@ -108,7 +111,6 @@ class DiscussionViewModel @Inject constructor(
}
}

// TODO move to IO thread.
private suspend fun proceedWithObservingChatMessages(
account: Id,
chat: Id
Expand Down Expand Up @@ -219,7 +221,7 @@ class DiscussionViewModel @Inject constructor(
)
}.reversed()
}
// .flowOn(dispatchers.io)
.flowOn(dispatchers.io)
.collect { result ->
messages.value = result
}
Expand Down
Loading