Update rust sdk : start branching new SyncService (wip)

This commit is contained in:
ganfra 2023-07-17 22:26:21 +02:00
parent 2488432805
commit 9b96bd427c
10 changed files with 37 additions and 30 deletions

View file

@ -73,10 +73,12 @@ import java.io.File
import org.matrix.rustcomponents.sdk.CreateRoomParameters as RustCreateRoomParameters
import org.matrix.rustcomponents.sdk.RoomPreset as RustRoomPreset
import org.matrix.rustcomponents.sdk.RoomVisibility as RustRoomVisibility
import org.matrix.rustcomponents.sdk.SyncService as ClientSyncService
@OptIn(ExperimentalCoroutinesApi::class)
class RustMatrixClient constructor(
private val client: Client,
private val syncService: ClientSyncService,
private val sessionStore: SessionStore,
appCoroutineScope: CoroutineScope,
private val dispatchers: CoroutineDispatchers,
@ -86,14 +88,11 @@ class RustMatrixClient constructor(
) : MatrixClient {
override val sessionId: UserId = UserId(client.userId())
private val app = client.app().use { builder ->
builder.finish()
}
private val roomListService = app.roomListService()
private val roomListService = syncService.roomListService()
private val sessionDispatcher = dispatchers.io.limitedParallelism(64)
private val sessionCoroutineScope = appCoroutineScope.childScope(dispatchers.main, "Session-${sessionId}")
private val verificationService = RustSessionVerificationService()
private val syncService = RustSyncService(app, roomListService.stateFlow(), sessionCoroutineScope)
private val rustSyncService = RustSyncService(syncService, roomListService.stateFlow(), sessionCoroutineScope)
private val pushersService = RustPushersService(
client = client,
dispatchers = dispatchers,
@ -131,7 +130,7 @@ class RustMatrixClient constructor(
init {
client.setDelegate(clientDelegate)
syncService.syncState
rustSyncService.syncState
.onEach { syncState ->
if (syncState == SyncState.Syncing) {
onSlidingSyncUpdate()
@ -245,7 +244,7 @@ class RustMatrixClient constructor(
}
}
override fun syncService(): SyncService = syncService
override fun syncService(): SyncService = rustSyncService
override fun sessionVerificationService(): SessionVerificationService = verificationService
@ -257,7 +256,7 @@ class RustMatrixClient constructor(
sessionCoroutineScope.cancel()
client.setDelegate(null)
verificationService.destroy()
app.destroy()
syncService.destroy()
roomListService.destroy()
notificationClient.destroy()
client.destroy()

View file

@ -180,9 +180,11 @@ class RustMatrixAuthenticationService @Inject constructor(
*/
}
private fun createMatrixClient(client: Client): MatrixClient {
private suspend fun createMatrixClient(client: Client): MatrixClient {
val syncService = client.syncService().finish()
return RustMatrixClient(
client = client,
syncService = syncService,
sessionStore = sessionStore,
appCoroutineScope = appCoroutineScope,
dispatchers = coroutineDispatchers,

View file

@ -17,8 +17,8 @@
package io.element.android.libraries.matrix.impl.sync
import io.element.android.libraries.matrix.api.sync.SyncState
import org.matrix.rustcomponents.sdk.AppState
import org.matrix.rustcomponents.sdk.RoomListServiceState
import org.matrix.rustcomponents.sdk.SyncServiceState
internal fun RoomListServiceState.toSyncState(): SyncState {
return when (this) {
@ -30,10 +30,11 @@ internal fun RoomListServiceState.toSyncState(): SyncState {
}
}
internal fun AppState.toSyncState(): SyncState {
internal fun SyncServiceState.toSyncState(): SyncState {
return when (this) {
AppState.RUNNING -> SyncState.Syncing
AppState.TERMINATED -> SyncState.Terminated
AppState.ERROR -> SyncState.InError
SyncServiceState.IDLE -> SyncState.Idle
SyncServiceState.RUNNING -> SyncState.Syncing
SyncServiceState.TERMINATED -> SyncState.Terminated
SyncServiceState.ERROR -> SyncState.InError
}
}

View file

@ -26,24 +26,24 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import org.matrix.rustcomponents.sdk.App
import org.matrix.rustcomponents.sdk.RoomListServiceState
import org.matrix.rustcomponents.sdk.SyncServiceInterface
import timber.log.Timber
class RustSyncService(
private val app: App,
private val innerSyncService: SyncServiceInterface,
roomListStateFlow: Flow<RoomListServiceState>,
sessionCoroutineScope: CoroutineScope
) : SyncService {
override fun startSync() = runCatching {
override suspend fun startSync() = runCatching {
Timber.v("Start sync")
app.start()
innerSyncService.start()
}
override fun stopSync() = runCatching {
Timber.v("Stop sync")
app.pause()
innerSyncService.pause()
}
override val syncState: StateFlow<SyncState> =

View file

@ -21,14 +21,14 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.buffer
import org.matrix.rustcomponents.sdk.App
import org.matrix.rustcomponents.sdk.AppState
import org.matrix.rustcomponents.sdk.AppStateObserver
import org.matrix.rustcomponents.sdk.SyncService
import org.matrix.rustcomponents.sdk.SyncServiceState
import org.matrix.rustcomponents.sdk.SyncServiceStateObserver
fun App.stateFlow(): Flow<AppState> =
fun SyncService.stateFlow(): Flow<SyncServiceState> =
mxCallbackFlow {
val listener = object : AppStateObserver {
override fun onUpdate(state: AppState) {
val listener = object : SyncServiceStateObserver {
override fun onUpdate(state: SyncServiceState) {
trySendBlocking(state)
}
}