Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2021 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.realm.kotlin.mongodb.internal
import io.realm.kotlin.internal.InternalConfiguration
import io.realm.kotlin.internal.NotificationToken
import io.realm.kotlin.internal.RealmImpl
import io.realm.kotlin.internal.interop.CoreError
import io.realm.kotlin.internal.interop.ErrorCode
import io.realm.kotlin.internal.interop.RealmInterop
import io.realm.kotlin.internal.interop.RealmSyncSessionPointer
import io.realm.kotlin.internal.interop.SyncSessionTransferCompletionCallback
import io.realm.kotlin.internal.interop.sync.CoreConnectionState
import io.realm.kotlin.internal.interop.sync.CoreSyncSessionState
import io.realm.kotlin.internal.interop.sync.ProgressDirection
import io.realm.kotlin.internal.interop.sync.SyncError
import io.realm.kotlin.internal.util.Validation
import io.realm.kotlin.internal.util.trySendWithBufferOverflowCheck
import io.realm.kotlin.mongodb.User
import io.realm.kotlin.mongodb.sync.ConnectionState
import io.realm.kotlin.mongodb.sync.ConnectionStateChange
import io.realm.kotlin.mongodb.sync.Direction
import io.realm.kotlin.mongodb.sync.Progress
import io.realm.kotlin.mongodb.sync.ProgressMode
import io.realm.kotlin.mongodb.sync.SyncConfiguration
import io.realm.kotlin.mongodb.sync.SyncSession
import io.realm.kotlin.notifications.internal.Cancellable
import io.realm.kotlin.notifications.internal.Cancellable.Companion.NO_OP_NOTIFICATION_TOKEN
import kotlinx.atomicfu.AtomicRef
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import kotlin.time.Duration
internal open class SyncSessionImpl(
initializerRealm: RealmImpl?,
internal val nativePointer: RealmSyncSessionPointer
) : SyncSession {
// Constructor used when there is no Realm available, e.g. in the SyncSessionErrorHandler.
// Without a Realm reference, it is impossible to track shared state between the public
// Realm and the SyncSession. This impacts `downloadAllServerChanges()`.
// Since there probably isn't a use case where you ever is going to call
// `downloadAllServerChanges` inside the error handler, we are just going to disallow it by
// throwing an IllegalStateException. Mostly because that is by far the easiest with the
// current implementation.
constructor(ptr: RealmSyncSessionPointer) : this(null, ptr)
private val _realm: RealmImpl? = initializerRealm
private val realm: RealmImpl
get() = _realm ?: throw IllegalStateException("Operation is not allowed inside a `SyncSession.ErrorHandler`.")
override val configuration: SyncConfiguration
// TODO Get the sync config w/o ever throwing
get() = realm.configuration as SyncConfiguration
override val user: User
get() = configuration.user
override val state: SyncSession.State
get() {
val state = RealmInterop.realm_sync_session_state(nativePointer)
return SyncSessionImpl.stateFrom(state)
}
override val connectionState: ConnectionState
get() = connectionStateFrom(RealmInterop.realm_sync_connection_state(nativePointer))
private enum class TransferDirection {
UPLOAD, DOWNLOAD
}
override suspend fun downloadAllServerChanges(timeout: Duration): Boolean {
return waitForChanges(TransferDirection.DOWNLOAD, timeout)
}
override suspend fun uploadAllLocalChanges(timeout: Duration): Boolean {
return waitForChanges(TransferDirection.UPLOAD, timeout)
}
override fun pause() {
RealmInterop.realm_sync_session_pause(nativePointer)
}
override fun resume() {
RealmInterop.realm_sync_session_resume(nativePointer)
}
@Suppress("invisible_member", "invisible_reference")
override fun progressAsFlow(
direction: Direction,
progressMode: ProgressMode,
): Flow