Skip to content

Commit c50eab2

Browse files
committed
Fix compilation errors
1 parent 6bcbaf3 commit c50eab2

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

core/src/commonIntegrationTest/kotlin/com/powersync/DatabaseTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import co.touchlab.kermit.Logger
66
import co.touchlab.kermit.Severity
77
import co.touchlab.kermit.TestConfig
88
import co.touchlab.kermit.TestLogWriter
9-
import com.powersync.db.PowerSyncDatabaseImpl
9+
import com.powersync.db.ActiveDatabaseGroup
1010
import com.powersync.db.schema.Schema
1111
import com.powersync.testutils.UserRow
1212
import com.powersync.testutils.waitFor
@@ -122,7 +122,7 @@ class DatabaseTest {
122122
waitFor {
123123
assertNotNull(
124124
logWriter.logs.find {
125-
it.message == PowerSyncDatabaseImpl.multipleInstancesMessage
125+
it.message == ActiveDatabaseGroup.multipleInstancesMessage
126126
},
127127
)
128128
}

core/src/commonIntegrationTest/kotlin/com/powersync/SyncIntegrationTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class SyncIntegrationTest {
120120
fun testPartialSync() =
121121
runTest {
122122
val syncStream = syncStream()
123-
database.connect(syncStream, 1000L)
123+
database.connectInternal(syncStream, 1000L)
124124

125125
val checksums =
126126
buildList {
@@ -214,7 +214,7 @@ class SyncIntegrationTest {
214214
fun testRemembersLastPartialSync() =
215215
runTest {
216216
val syncStream = syncStream()
217-
database.connect(syncStream, 1000L)
217+
database.connectInternal(syncStream, 1000L)
218218

219219
syncLines.send(
220220
SyncLine.FullCheckpoint(
@@ -253,7 +253,7 @@ class SyncIntegrationTest {
253253
fun setsDownloadingState() =
254254
runTest {
255255
val syncStream = syncStream()
256-
database.connect(syncStream, 1000L)
256+
database.connectInternal(syncStream, 1000L)
257257

258258
turbineScope(timeout = 10.0.seconds) {
259259
val turbine = database.currentStatus.asFlow().testIn(this)
@@ -291,7 +291,7 @@ class SyncIntegrationTest {
291291
val syncStream = syncStream()
292292
val turbine = database.currentStatus.asFlow().testIn(this)
293293

294-
database.connect(syncStream, 1000L)
294+
database.connectInternal(syncStream, 1000L)
295295
turbine.waitFor { it.connecting }
296296

297297
database.disconnect()
@@ -308,7 +308,7 @@ class SyncIntegrationTest {
308308
fun testMultipleSyncsDoNotCreateMultipleStatusEntries() =
309309
runTest {
310310
val syncStream = syncStream()
311-
database.connect(syncStream, 1000L)
311+
database.connectInternal(syncStream, 1000L)
312312

313313
turbineScope(timeout = 10.0.seconds) {
314314
val turbine = database.currentStatus.asFlow().testIn(this)

core/src/commonJava/kotlin/com/powersync/db/ActiveInstanceStore.commonJava.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.powersync.db
22

3+
internal actual fun disposeWhenDeallocated(resource: ActiveDatabaseResource): Any {
4+
// We can't do this on Java 8 :(
5+
return object {}
6+
}
7+
8+
// This would require Java 9+
9+
/*
310
import java.lang.ref.Cleaner
411
512
internal actual fun disposeWhenDeallocated(resource: ActiveDatabaseResource): Any {
@@ -16,3 +23,4 @@ private class CleanableWrapper {
1623
val cleaner: Cleaner = Cleaner.create()
1724
}
1825
}
26+
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ internal class ActiveDatabaseGroup(val identifier: String) {
7171
}
7272

7373
internal class ActiveDatabaseResource(val group: ActiveDatabaseGroup) {
74-
var disposed = atomic(false)
74+
val disposed = atomic(false)
7575

7676
fun dispose() {
7777
if (!disposed.getAndSet(true)) {

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import kotlinx.coroutines.flow.filter
3434
import kotlinx.coroutines.flow.first
3535
import kotlinx.coroutines.launch
3636
import kotlinx.coroutines.runBlocking
37+
import kotlinx.coroutines.sync.Mutex
38+
import kotlinx.coroutines.sync.withLock
3739
import kotlinx.datetime.Instant
3840
import kotlinx.datetime.LocalDateTime
3941
import kotlinx.datetime.TimeZone
@@ -82,10 +84,9 @@ internal class PowerSyncDatabaseImpl(
8284
*/
8385
override val currentStatus: SyncStatus = SyncStatus()
8486

87+
private val mutex = Mutex()
8588
private var syncStream: SyncStream? = null
86-
8789
private var syncJob: Job? = null
88-
8990
private var uploadJob: Job? = null
9091

9192
init {
@@ -117,10 +118,10 @@ internal class PowerSyncDatabaseImpl(
117118
crudThrottleMs: Long,
118119
retryDelayMs: Long,
119120
params: Map<String, JsonParam?>,
120-
) = exclusiveMethod("connect") {
121-
disconnect()
121+
) = mutex.withLock {
122+
disconnectInternal()
122123

123-
connect(
124+
connectInternal(
124125
SyncStream(
125126
bucketStorage = bucketStorage,
126127
connector = connector,
@@ -134,7 +135,7 @@ internal class PowerSyncDatabaseImpl(
134135
}
135136

136137
@OptIn(FlowPreview::class)
137-
internal fun connect(
138+
internal fun connectInternal(
138139
stream: SyncStream,
139140
crudThrottleMs: Long,
140141
) {
@@ -327,7 +328,9 @@ internal class PowerSyncDatabaseImpl(
327328
}
328329
}
329330

330-
override suspend fun disconnect() {
331+
override suspend fun disconnect() = mutex.withLock { disconnectInternal() }
332+
333+
private suspend fun disconnectInternal() {
331334
if (syncJob != null && syncJob!!.isActive) {
332335
syncJob?.cancelAndJoin()
333336
}
@@ -421,11 +424,11 @@ internal class PowerSyncDatabaseImpl(
421424
}
422425

423426
override suspend fun close() =
424-
exclusiveMethod("close") {
427+
mutex.withLock {
425428
if (closed) {
426-
return@exclusiveMethod
429+
return@withLock
427430
}
428-
disconnect()
431+
disconnectInternal()
429432
internalDb.close()
430433
resource.dispose()
431434
closed = true

0 commit comments

Comments
 (0)