feat(client): use mutexes for repositories
This commit is contained in:
parent
50d312049b
commit
57e10f697a
2 changed files with 13 additions and 4 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
package moe.lava.banksia.client.repository
|
package moe.lava.banksia.client.repository
|
||||||
|
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import moe.lava.banksia.client.datasource.local.RouteLocalDataSource
|
import moe.lava.banksia.client.datasource.local.RouteLocalDataSource
|
||||||
import moe.lava.banksia.client.datasource.remote.RouteRemoteDataSource
|
import moe.lava.banksia.client.datasource.remote.RouteRemoteDataSource
|
||||||
|
|
||||||
|
|
@ -7,7 +9,8 @@ class RouteRepository(
|
||||||
private val local: RouteLocalDataSource,
|
private val local: RouteLocalDataSource,
|
||||||
private val remote: RouteRemoteDataSource,
|
private val remote: RouteRemoteDataSource,
|
||||||
) {
|
) {
|
||||||
suspend fun getAll() =
|
private val mutex = Mutex()
|
||||||
|
suspend fun getAll() = mutex.withLock {
|
||||||
local
|
local
|
||||||
.getAll()
|
.getAll()
|
||||||
.map { it.asModel() }
|
.map { it.asModel() }
|
||||||
|
|
@ -16,6 +19,7 @@ class RouteRepository(
|
||||||
.getAll()
|
.getAll()
|
||||||
.also { local.save(*it.toTypedArray()) }
|
.also { local.save(*it.toTypedArray()) }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun get(id: String) = local.get(id)?.asModel() ?: remote.get(id)
|
suspend fun get(id: String) = mutex.withLock { local.get(id)?.asModel() ?: remote.get(id) }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package moe.lava.banksia.client.repository
|
package moe.lava.banksia.client.repository
|
||||||
|
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import moe.lava.banksia.client.datasource.local.StopLocalDataSource
|
import moe.lava.banksia.client.datasource.local.StopLocalDataSource
|
||||||
import moe.lava.banksia.client.datasource.remote.StopRemoteDataSource
|
import moe.lava.banksia.client.datasource.remote.StopRemoteDataSource
|
||||||
|
|
||||||
|
|
@ -7,11 +9,14 @@ class StopRepository(
|
||||||
private val local: StopLocalDataSource,
|
private val local: StopLocalDataSource,
|
||||||
private val remote: StopRemoteDataSource,
|
private val remote: StopRemoteDataSource,
|
||||||
) {
|
) {
|
||||||
suspend fun get(id: String) = local.get(id)?.asModel() ?: remote.get(id)
|
private val mutex = Mutex()
|
||||||
suspend fun getByRoute(id: String) =
|
|
||||||
|
suspend fun get(id: String) = mutex.withLock { local.get(id)?.asModel() ?: remote.get(id) }
|
||||||
|
suspend fun getByRoute(id: String) = mutex.withLock {
|
||||||
local
|
local
|
||||||
.getByRoute(id)
|
.getByRoute(id)
|
||||||
.map { it.asModel() }
|
.map { it.asModel() }
|
||||||
.ifEmpty { null }
|
.ifEmpty { null }
|
||||||
?: remote.getByRoute(id)
|
?: remote.getByRoute(id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue