Skip to content

Commit

Permalink
Extract to files
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-ramotar committed Feb 17, 2024
1 parent 6af0312 commit 2463962
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.mobilenativefoundation.store.paging5

import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
import org.mobilenativefoundation.store.core5.StoreData
import org.mobilenativefoundation.store.core5.StoreKey

@ExperimentalStoreApi
interface Joiner<Id : Any, K : StoreKey<Id>, SO : StoreData.Single<Id>> {
suspend operator fun invoke(data: Map<K, PagingData<Id, SO>>): PagingData<Id, SO>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.mobilenativefoundation.store.paging5

import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
import org.mobilenativefoundation.store.core5.StoreKey

@ExperimentalStoreApi
interface KeyFactory<Id : Any, SK : StoreKey.Single<Id>> {
fun createFor(id: Id): SK
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.mobilenativefoundation.store.paging5

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
import org.mobilenativefoundation.store.core5.StoreData
import org.mobilenativefoundation.store.core5.StoreKey
import org.mobilenativefoundation.store.store5.MutableStore
import org.mobilenativefoundation.store.store5.Store
import org.mobilenativefoundation.store.store5.StoreReadRequest
import org.mobilenativefoundation.store.store5.StoreReadResponse

@ExperimentalStoreApi
interface Pager<Id : Any, K : StoreKey<Id>, SO : StoreData.Single<Id>> {
val state: StateFlow<PagingData<Id, SO>>
fun load(key: K)

companion object {
fun <Id : Any, SK : StoreKey.Single<Id>, K : StoreKey<Id>, SO : StoreData.Single<Id>, O : StoreData<Id>> create(
scope: CoroutineScope,
store: Store<K, O>,
joiner: Joiner<Id, K, SO>,
keyFactory: KeyFactory<Id, SK>
): Pager<Id, K, SO> {

val streamer = object : Streamer<Id, K, O> {
override fun invoke(key: K): Flow<StoreReadResponse<O>> {
return store.stream(StoreReadRequest.fresh(key))
}
}

return RealPager(
scope,
streamer,
joiner,
keyFactory
)
}

fun <Id : Any, SK : StoreKey.Single<Id>, K : StoreKey<Id>, SO : StoreData.Single<Id>, O : StoreData<Id>> create(
scope: CoroutineScope,
store: MutableStore<K, O>,
joiner: Joiner<Id, K, SO>,
keyFactory: KeyFactory<Id, SK>
): Pager<Id, K, SO> {

val streamer = object : Streamer<Id, K, O> {
override fun invoke(key: K): Flow<StoreReadResponse<O>> {
return store.stream<Any>(StoreReadRequest.fresh(key))
}
}

return RealPager(
scope,
streamer,
joiner,
keyFactory
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.mobilenativefoundation.store.paging5

import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
import org.mobilenativefoundation.store.core5.StoreData

@ExperimentalStoreApi
data class PagingData<Id : Any, SO : StoreData.Single<Id>>(
val items: List<SO>
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package org.mobilenativefoundation.store.paging5

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -15,84 +14,11 @@ import kotlinx.coroutines.sync.withLock
import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
import org.mobilenativefoundation.store.core5.StoreData
import org.mobilenativefoundation.store.core5.StoreKey
import org.mobilenativefoundation.store.store5.MutableStore
import org.mobilenativefoundation.store.store5.Store
import org.mobilenativefoundation.store.store5.StoreReadRequest
import org.mobilenativefoundation.store.store5.StoreReadResponse


@ExperimentalStoreApi
data class PagingData<Id : Any, SO : StoreData.Single<Id>>(
val items: List<SO>
)

@ExperimentalStoreApi
interface Pager<Id : Any, K : StoreKey<Id>, SO : StoreData.Single<Id>> {
val state: StateFlow<PagingData<Id, SO>>
fun load(key: K)

companion object {
fun <Id : Any, SK : StoreKey.Single<Id>, K : StoreKey<Id>, SO : StoreData.Single<Id>, O : StoreData<Id>> create(
scope: CoroutineScope,
store: Store<K, O>,
joiner: Joiner<Id, K, SO>,
keyFactory: KeyFactory<Id, SK>
): Pager<Id, K, SO> {

val streamer = object : Streamer<Id, K, O> {
override fun invoke(key: K): Flow<StoreReadResponse<O>> {
return store.stream(StoreReadRequest.fresh(key))
}
}

return RealPager(
scope,
streamer,
joiner,
keyFactory
)
}

fun <Id : Any, SK : StoreKey.Single<Id>, K : StoreKey<Id>, SO : StoreData.Single<Id>, O : StoreData<Id>> create(
scope: CoroutineScope,
store: MutableStore<K, O>,
joiner: Joiner<Id, K, SO>,
keyFactory: KeyFactory<Id, SK>
): Pager<Id, K, SO> {

val streamer = object : Streamer<Id, K, O> {
override fun invoke(key: K): Flow<StoreReadResponse<O>> {
return store.stream<Any>(StoreReadRequest.fresh(key))
}
}

return RealPager(
scope,
streamer,
joiner,
keyFactory
)
}
}
}

@ExperimentalStoreApi
interface Joiner<Id : Any, K : StoreKey<Id>, SO : StoreData.Single<Id>> {
suspend operator fun invoke(data: Map<K, PagingData<Id, SO>>): PagingData<Id, SO>
}

@ExperimentalStoreApi
interface Streamer<Id : Any, K : StoreKey<Id>, O : StoreData<Id>> {
operator fun invoke(key: K): Flow<StoreReadResponse<O>>
}

@ExperimentalStoreApi
interface KeyFactory<Id : Any, SK : StoreKey.Single<Id>> {
fun createFor(id: Id): SK
}

@ExperimentalStoreApi
class RealPager<Id : Any, SK : StoreKey.Single<Id>, K : StoreKey<Id>, SO : StoreData.Single<Id>, O : StoreData<Id>>(
internal class RealPager<Id : Any, SK : StoreKey.Single<Id>, K : StoreKey<Id>, SO : StoreData.Single<Id>, O : StoreData<Id>>(
private val scope: CoroutineScope,
private val streamer: Streamer<Id, K, O>,
private val joiner: Joiner<Id, K, SO>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.mobilenativefoundation.store.paging5

import kotlinx.coroutines.flow.Flow
import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
import org.mobilenativefoundation.store.core5.StoreData
import org.mobilenativefoundation.store.core5.StoreKey
import org.mobilenativefoundation.store.store5.StoreReadResponse

@ExperimentalStoreApi
internal interface Streamer<Id : Any, K : StoreKey<Id>, O : StoreData<Id>> {
operator fun invoke(key: K): Flow<StoreReadResponse<O>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalStoreApi::class, ExperimentalCoroutinesApi::class)
class LaunchPagingStoreTests {
class RealPagerTest {
private val testScope = TestScope()

private val userId = "123"
Expand Down

0 comments on commit 2463962

Please sign in to comment.