Skip to content

Commit 85da9c6

Browse files
DominicGBauerDominicGBauer
andauthored
feat: add wait for first sync (#65)
* feat: add waitForFirstSync function * chore: remove logs * chore: cleanup --------- Co-authored-by: DominicGBauer <[email protected]>
1 parent d4b7725 commit 85da9c6

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public interface PowerSyncDatabase : Queries {
2323
*/
2424
public val currentStatus: SyncStatus
2525

26+
/**
27+
* Suspend function that resolves when the first sync has occurred
28+
*/
29+
public suspend fun waitForFirstSync()
30+
2631
/**
2732
* Connect to the PowerSync service, and keep the databases in sync.
2833
*

core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import kotlinx.coroutines.Job
2929
import kotlinx.coroutines.cancelAndJoin
3030
import kotlinx.coroutines.flow.Flow
3131
import kotlinx.coroutines.flow.debounce
32+
import kotlinx.coroutines.flow.first
3233
import kotlinx.coroutines.launch
3334
import kotlinx.coroutines.runBlocking
3435
import kotlinx.datetime.Instant
@@ -319,6 +320,16 @@ internal class PowerSyncDatabaseImpl(
319320
}
320321
}
321322

323+
override suspend fun waitForFirstSync() {
324+
if (currentStatus.hasSynced == true) {
325+
return
326+
}
327+
328+
currentStatus.asFlow().first { status ->
329+
status.hasSynced == true
330+
}
331+
}
332+
322333
/**
323334
* Check that a supported version of the powersync extension is loaded.
324335
*/

core/src/commonMain/kotlin/com/powersync/sync/SyncStatus.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public data class SyncStatus internal constructor(
150150
get() = data.downloadError
151151

152152
override fun toString(): String {
153-
return "SyncStatus(connected=$connected, connecting=$connecting, downloading=$downloading, uploading=$uploading, lastSyncedAt=$lastSyncedAt, hasSynced: $hasSynced, error=$anyError)"
153+
return "SyncStatus(connected=$connected, connecting=$connecting, downloading=$downloading, uploading=$uploading, lastSyncedAt=$lastSyncedAt, hasSynced=$hasSynced, error=$anyError)"
154154
}
155155

156156
public companion object {

0 commit comments

Comments
 (0)