mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-08 05:47:05 +09:00
DROID-3233 App | Tech | Add observer for StoreOfObjectType (#2316)
This commit is contained in:
parent
f3f9cb9301
commit
bc1f114129
1 changed files with 19 additions and 0 deletions
|
@ -10,12 +10,19 @@ import com.anytypeio.anytype.domain.`object`.unset
|
|||
import com.anytypeio.anytype.domain.objects.StoreOfObjectTypes.TrackedEvent
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
interface StoreOfObjectTypes {
|
||||
val size: Int
|
||||
|
||||
suspend fun observe(id: Id) : Flow<ObjectWrapper.Type>
|
||||
suspend fun get(id: Id): ObjectWrapper.Type?
|
||||
suspend fun getByKey(key: Key): ObjectWrapper.Type?
|
||||
suspend fun getAll(): List<ObjectWrapper.Type>
|
||||
|
@ -43,6 +50,18 @@ class DefaultStoreOfObjectTypes : StoreOfObjectTypes {
|
|||
|
||||
override val size: Int get() = store.size
|
||||
|
||||
override suspend fun observe(id: Id): Flow<ObjectWrapper.Type> = flow {
|
||||
val init = get(id)
|
||||
if (init != null && init.isValid) {
|
||||
emit(init)
|
||||
}
|
||||
emitAll(
|
||||
trackChanges()
|
||||
.mapNotNull { get(id) }
|
||||
.filter { it.isValid }
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun get(id: Id): ObjectWrapper.Type? = mutex.withLock {
|
||||
store[id]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue