Skip to content

Commit e8fc3b6

Browse files
committed
- updates to LocalSource interface and implementations
1 parent a27dc91 commit e8fc3b6

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Diff for: core-data/src/commonMain/kotlin/com/coderwise/core/data/arch/DataStoreLocalSource.kt

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.coderwise.core.data.arch
33
import androidx.datastore.core.DataStore
44
import com.coderwise.core.domain.arch.Outcome
55
import com.coderwise.core.domain.arch.dataOrNull
6+
import com.coderwise.core.domain.arch.tryOutcome
67
import kotlinx.coroutines.flow.Flow
78
import kotlinx.coroutines.flow.first
89
import kotlinx.coroutines.flow.map
@@ -32,11 +33,15 @@ open class DataStoreLocalSource<Entity, Id, Record : Any>(
3233
?: throw NoSuchElementException()
3334

3435
emit(Outcome.Success(entity))
35-
} catch (_: NoSuchElementException) {
36-
// NOOP Outcome.Error(NoSuchElementException())
36+
} catch (e: NoSuchElementException) {
37+
emit(Outcome.Error(e))
3738
}
3839
}
3940

41+
override suspend fun findById(id: Id): Outcome<Entity> = tryOutcome {
42+
dataStore.data.first().list.findById(id, identify, recordToEntity)!!
43+
}
44+
4045
override suspend fun update(entity: Entity): Outcome<Id> {
4146
val entityId = identify(entity)
4247
dataStore.updateData { data ->

Diff for: core-data/src/commonMain/kotlin/com/coderwise/core/data/arch/LocalSource.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import kotlinx.coroutines.flow.Flow
66
interface LocalSource<Entity, Id> {
77
val flow: Flow<Outcome<List<Entity>>>
88
fun flowById(id: Id): Flow<Outcome<Entity>>
9+
suspend fun findById(id: Id): Outcome<Entity>
910
suspend fun update(entity: Entity): Outcome<Id>
1011
suspend fun delete(id: Id): Outcome<Unit>
1112
suspend fun merge(list: List<Entity>): Outcome<Unit>
1213
suspend fun reset(list: List<Entity>): Outcome<Unit>
1314
suspend fun isEmpty(): Boolean
14-
suspend fun clear(): Outcome<Unit> = merge(emptyList())
15+
suspend fun clear(): Outcome<Unit> = reset(emptyList())
1516
}

Diff for: core-data/src/commonMain/kotlin/com/coderwise/core/data/arch/MemoryLocalSource.kt

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.coderwise.core.data.arch
22

33
import com.coderwise.core.data.utils.MemCache
44
import com.coderwise.core.domain.arch.Outcome
5+
import com.coderwise.core.domain.arch.tryOutcome
56
import kotlinx.coroutines.flow.Flow
67
import kotlinx.coroutines.flow.map
78
import kotlinx.coroutines.flow.transform
@@ -25,6 +26,8 @@ open class MemoryLocalSource<Entity, Id>(
2526
emit(Outcome.Success(it))
2627
}
2728

29+
override suspend fun findById(id: Id): Outcome<Entity> = tryOutcome { memCache.find(id)!! }
30+
2831
override suspend fun delete(id: Id): Outcome<Unit> {
2932
memCache.delete(id)
3033
return Outcome.Success(Unit)

0 commit comments

Comments
 (0)