Skip to content
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
5 changes: 5 additions & 0 deletions core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading