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

@ -139,7 +139,9 @@ class LoggedInFlowNode @AssistedInject constructor(
}
},
onResume = {
syncService.startSync()
lifecycleScope.launch {
syncService.startSync()
}
},
onPause = {
syncService.stopSync()

View file

@ -145,7 +145,7 @@ jsoup = { module = "org.jsoup:jsoup", version.ref = "jsoup" }
appyx_core = { module = "com.bumble.appyx:core", version.ref = "appyx" }
molecule-runtime = { module = "app.cash.molecule:molecule-runtime", version.ref = "molecule" }
timber = "com.jakewharton.timber:timber:5.0.1"
matrix_sdk = "org.matrix.rustcomponents:sdk-android:0.1.31"
matrix_sdk = "org.matrix.rustcomponents:sdk-android:0.1.32"
sqldelight-driver-android = { module = "com.squareup.sqldelight:android-driver", version.ref = "sqldelight" }
sqldelight-driver-jvm = { module = "com.squareup.sqldelight:sqlite-driver", version.ref = "sqldelight" }
sqldelight-coroutines = { module = "com.squareup.sqldelight:coroutines-extensions", version.ref = "sqldelight" }

View file

@ -22,7 +22,7 @@ interface SyncService {
/**
* Tries to start the sync. If already syncing it has no effect.
*/
fun startSync(): Result<Unit>
suspend fun startSync(): Result<Unit>
/**
* Tries to stop the sync. If service is not syncing it has no effect.

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)
}
}

View file

@ -29,7 +29,7 @@ class FakeSyncService : SyncService {
syncStateFlow.value = SyncState.InError
}
override fun startSync(): Result<Unit> {
override suspend fun startSync(): Result<Unit> {
syncStateFlow.value = SyncState.Syncing
return Result.success(Unit)
}

View file

@ -40,6 +40,7 @@ import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.room.RoomMembershipObserver
import io.element.android.services.toolbox.impl.strings.AndroidStringProvider
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
@ -103,7 +104,9 @@ class RoomListScreen(
DisposableEffect(Unit) {
Timber.w("Start sync!")
matrixClient.syncService().startSync()
runBlocking {
matrixClient.syncService().startSync()
}
onDispose {
Timber.w("Stop sync!")
matrixClient.syncService().stopSync()