diff --git a/core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt b/core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt index 55a99c54..47bc6ec8 100644 --- a/core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt +++ b/core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt @@ -23,6 +23,11 @@ public interface PowerSyncDatabase : Queries { */ public val currentStatus: SyncStatus + /** + * Suspend function that resolves when the first sync has occurred + */ + public suspend fun waitForFirstSync() + /** * Connect to the PowerSync service, and keep the databases in sync. * diff --git a/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt b/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt index 2bdd9e86..5edcfc1c 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt @@ -29,6 +29,7 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.debounce +import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.datetime.Instant @@ -319,6 +320,16 @@ internal class PowerSyncDatabaseImpl( } } + override suspend fun waitForFirstSync() { + if (currentStatus.hasSynced == true) { + return + } + + currentStatus.asFlow().first { status -> + status.hasSynced == true + } + } + /** * Check that a supported version of the powersync extension is loaded. */ diff --git a/core/src/commonMain/kotlin/com/powersync/sync/SyncStatus.kt b/core/src/commonMain/kotlin/com/powersync/sync/SyncStatus.kt index 11510524..2c68c8e7 100644 --- a/core/src/commonMain/kotlin/com/powersync/sync/SyncStatus.kt +++ b/core/src/commonMain/kotlin/com/powersync/sync/SyncStatus.kt @@ -150,7 +150,7 @@ public data class SyncStatus internal constructor( get() = data.downloadError override fun toString(): String { - return "SyncStatus(connected=$connected, connecting=$connecting, downloading=$downloading, uploading=$uploading, lastSyncedAt=$lastSyncedAt, hasSynced: $hasSynced, error=$anyError)" + return "SyncStatus(connected=$connected, connecting=$connecting, downloading=$downloading, uploading=$uploading, lastSyncedAt=$lastSyncedAt, hasSynced=$hasSynced, error=$anyError)" } public companion object {