Skip to content

Commit

Permalink
Fix formatting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-ramotar committed Dec 8, 2023
1 parent de91a81 commit 9aae858
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,4 @@ class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id
fun invalidKeyErrorMessage(key: Any) =
"Expected StoreKey.Single or StoreKey.Collection, but received ${key::class}"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ enum class InsertionStrategy {
APPEND,
PREPEND,
REPLACE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package org.mobilenativefoundation.store.core5
interface KeyProvider<Id : Any, Single : StoreData.Single<Id>> {
fun fromCollection(key: StoreKey.Collection<Id>, value: Single): StoreKey.Single<Id>
fun fromSingle(key: StoreKey.Single<Id>, value: Single): StoreKey.Collection<Id>
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.mobilenativefoundation.store.core5


/**
* An interface that defines items that can be uniquely identified.
* Every item that implements the [StoreData] interface must have a means of identification.
Expand Down Expand Up @@ -32,4 +31,4 @@ interface StoreData<out Id : Any> {
*/
fun insertItems(strategy: InsertionStrategy, items: List<S>): Collection<Id, S>
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ interface StoreKey<out Id : Any> {
interface Filter<Value : Any> {
operator fun invoke(items: List<Value>): List<Value>
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
@file:Suppress("UNCHECKED_CAST")


package org.mobilenativefoundation.store.paging5


import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.mobilenativefoundation.store.core5.StoreData
import org.mobilenativefoundation.store.core5.StoreKey
import org.mobilenativefoundation.store.store5.*
import org.mobilenativefoundation.store.store5.ExperimentalStoreApi
import org.mobilenativefoundation.store.store5.MutableStore
import org.mobilenativefoundation.store.store5.Store
import org.mobilenativefoundation.store.store5.StoreReadRequest
import org.mobilenativefoundation.store.store5.StoreReadResponse

private class StopProcessingException : Exception()


/**
* Initializes and returns a [StateFlow] that reflects the state of the Store, updating by a flow of provided keys.
* @param scope A [CoroutineScope].
Expand All @@ -28,7 +34,6 @@ private fun <Id : Any, Key : StoreKey<Id>, Output : StoreData<Id>> launchPagingS
): StateFlow<StoreReadResponse<Output>> {
val stateFlow = MutableStateFlow<StoreReadResponse<Output>>(StoreReadResponse.Initial)


scope.launch {

try {
Expand All @@ -50,9 +55,7 @@ private fun <Id : Any, Key : StoreKey<Id>, Output : StoreData<Id>> launchPagingS
throw StopProcessingException()
}
}

} catch (_: StopProcessingException) {

}

keys.drop(1).collect { key ->
Expand Down Expand Up @@ -93,7 +96,6 @@ fun <Id : Any, Key : StoreKey<Id>, Output : StoreData<Id>> MutableStore<Key, Out
}
}


private fun <Id : Any, Key : StoreKey.Collection<Id>, Output : StoreData<Id>> joinData(
key: Key,
prevResponse: StoreReadResponse<Output>,
Expand All @@ -109,4 +111,3 @@ private fun <Id : Any, Key : StoreKey.Collection<Id>, Output : StoreData<Id>> jo
val joinedOutput = (lastOutput?.insertItems(key.insertionStrategy, currentData.items) ?: currentData) as Output
return StoreReadResponse.Data(joinedOutput, currentResponse.origin)
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.mobilenativefoundation.store.paging5.util.*
import org.mobilenativefoundation.store.store5.*
import org.mobilenativefoundation.store.paging5.util.FakePostApi
import org.mobilenativefoundation.store.paging5.util.FakePostDatabase
import org.mobilenativefoundation.store.paging5.util.PostApi
import org.mobilenativefoundation.store.paging5.util.PostData
import org.mobilenativefoundation.store.paging5.util.PostDatabase
import org.mobilenativefoundation.store.paging5.util.PostKey
import org.mobilenativefoundation.store.paging5.util.PostPutRequestResult
import org.mobilenativefoundation.store.paging5.util.PostStoreFactory
import org.mobilenativefoundation.store.store5.ExperimentalStoreApi
import org.mobilenativefoundation.store.store5.MutableStore
import org.mobilenativefoundation.store.store5.StoreReadRequest
import org.mobilenativefoundation.store.store5.StoreReadResponse
import org.mobilenativefoundation.store.store5.StoreReadResponseOrigin
import org.mobilenativefoundation.store.store5.StoreWriteRequest
import kotlin.test.assertEquals
import kotlin.test.assertIs

Expand Down Expand Up @@ -132,7 +144,6 @@ class LaunchPagingStoreTests {
assertIs<PostData.Post>(data2)
assertEquals("2", data2.title)


store.write(StoreWriteRequest.of(PostKey.Single("2"), PostData.Post("2", "2-modified")))

val cached3 = store.stream<PostPutRequestResult>(StoreReadRequest.cached(PostKey.Single("2"), refresh = false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ class FakePostApi : PostApi {
posts.put(post.id, post)
return PostPutRequestResult.Data(post)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ class FakePostDatabase(private val userId: String) : PostDatabase {
override fun findFeedByUserId(cursor: String?, size: Int): PostData.Feed? {
val feed = feeds[userId]
return feed

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ sealed class FeedGetRequestResult {
data class Message(val error: String) : Error()
data class Exception(val error: kotlin.Exception) : Error()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ interface PostApi {
suspend fun get(postId: String): PostGetRequestResult
suspend fun get(cursor: String?, size: Int): FeedGetRequestResult
suspend fun put(post: PostData.Post): PostPutRequestResult
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@ sealed class PostData : StoreData<String> {
}
}
}



Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ interface PostDatabase {
fun add(feed: PostData.Feed)
fun findPostByPostId(postId: String): PostData.Post?
fun findFeedByUserId(cursor: String?, size: Int): PostData.Feed?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ sealed class PostGetRequestResult {
data class Message(val error: String) : Error()
data class Exception(val error: kotlin.Exception) : Error()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ sealed class PostKey : StoreKey<String> {
data class Single(
override val id: String
) : StoreKey.Single<String>, PostKey()

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ sealed class PostPutRequestResult {
data class Message(val error: String) : Error()
data class Exception(val error: kotlin.Exception) : Error()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class PostStoreFactory(private val api: PostApi, private val db: PostDatabase) {
throw Throwable(result.error)
}
}

}

is PostKey.Cursor -> {
Expand Down Expand Up @@ -121,7 +120,6 @@ class PostStoreFactory(private val api: PostApi, private val db: PostDatabase) {
val cursor = (floor(id.toDouble() / 10) * 10) + 1
return PostKey.Cursor(cursor.toInt().toString(), 10)
}

}

private fun createMemoryCache(): Cache<PostKey, PostData> =
Expand All @@ -137,4 +135,4 @@ class PostStoreFactory(private val api: PostApi, private val db: PostDatabase) {
updater = createUpdater(),
bookkeeper = null
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package org.mobilenativefoundation.store.store5.impl.extensions

import kotlinx.coroutines.flow.filterNot
import kotlinx.coroutines.flow.first
import org.mobilenativefoundation.store.store5.*
import org.mobilenativefoundation.store.store5.Bookkeeper
import org.mobilenativefoundation.store.store5.ExperimentalStoreApi
import org.mobilenativefoundation.store.store5.MutableStore
import org.mobilenativefoundation.store.store5.Store
import org.mobilenativefoundation.store.store5.StoreReadRequest
import org.mobilenativefoundation.store.store5.StoreReadResponse
import org.mobilenativefoundation.store.store5.Updater
import org.mobilenativefoundation.store.store5.impl.RealMutableStore
import org.mobilenativefoundation.store.store5.impl.RealStore

Expand Down Expand Up @@ -30,6 +36,7 @@ suspend fun <Key : Any, Output : Any> Store<Key, Output>.fresh(key: Key) =
.first()
.requireData()

@OptIn(ExperimentalStoreApi::class)
@Suppress("UNCHECKED_CAST")
fun <Key : Any, Network : Any, Output : Any, Local : Any, Response : Any> Store<Key, Output>.asMutableStore(
updater: Updater<Key, Output, Response>,
Expand All @@ -45,17 +52,16 @@ fun <Key : Any, Network : Any, Output : Any, Local : Any, Response : Any> Store<
)
}


@OptIn(ExperimentalStoreApi::class)
suspend fun <Key : Any, Output : Any, Response: Any> MutableStore<Key, Output>.get(key: Key) =
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.get(key: Key) =
stream<Response>(StoreReadRequest.cached(key, refresh = false))
.filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData }
.first()
.requireData()

@OptIn(ExperimentalStoreApi::class)
suspend fun <Key : Any, Output : Any, Response: Any> MutableStore<Key, Output>.fresh(key: Key) =
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.fresh(key: Key) =
stream<Response>(StoreReadRequest.fresh(key))
.filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData }
.first()
.requireData()
.requireData()

0 comments on commit 9aae858

Please sign in to comment.