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
|
||||
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import moe.lava.banksia.client.datasource.local.RouteLocalDataSource
|
||||
import moe.lava.banksia.client.datasource.remote.RouteRemoteDataSource
|
||||
|
||||
|
|
@ -7,7 +9,8 @@ class RouteRepository(
|
|||
private val local: RouteLocalDataSource,
|
||||
private val remote: RouteRemoteDataSource,
|
||||
) {
|
||||
suspend fun getAll() =
|
||||
private val mutex = Mutex()
|
||||
suspend fun getAll() = mutex.withLock {
|
||||
local
|
||||
.getAll()
|
||||
.map { it.asModel() }
|
||||
|
|
@ -16,6 +19,7 @@ class RouteRepository(
|
|||
.getAll()
|
||||
.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
|
||||
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import moe.lava.banksia.client.datasource.local.StopLocalDataSource
|
||||
import moe.lava.banksia.client.datasource.remote.StopRemoteDataSource
|
||||
|
||||
|
|
@ -7,11 +9,14 @@ class StopRepository(
|
|||
private val local: StopLocalDataSource,
|
||||
private val remote: StopRemoteDataSource,
|
||||
) {
|
||||
suspend fun get(id: String) = local.get(id)?.asModel() ?: remote.get(id)
|
||||
suspend fun getByRoute(id: String) =
|
||||
private val mutex = Mutex()
|
||||
|
||||
suspend fun get(id: String) = mutex.withLock { local.get(id)?.asModel() ?: remote.get(id) }
|
||||
suspend fun getByRoute(id: String) = mutex.withLock {
|
||||
local
|
||||
.getByRoute(id)
|
||||
.map { it.asModel() }
|
||||
.ifEmpty { null }
|
||||
?: remote.getByRoute(id)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue