-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from soil-kt/query-compose-util
Move `Util.kt` to `query.compose.util` package
- Loading branch information
Showing
6 changed files
with
65 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
soil-query-compose/src/commonMain/kotlin/soil/query/compose/util/NamespaceGeneration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2024 Soil Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package soil.query.compose.util | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.saveable.Saver | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import soil.query.core.Namespace | ||
import soil.query.core.uuid | ||
|
||
/** | ||
* Automatically generated value for mutationId and subscriptionId. | ||
* | ||
* This function is useful for generating a unique namespace when a key, such as MutationKey or SubscriptionKey, is used in a single Composable function. | ||
* | ||
* @see Namespace | ||
*/ | ||
@Composable | ||
fun Namespace.Companion.auto(): Namespace { | ||
return rememberSaveable(saver = Namespace.Saver) { Namespace("auto/${uuid()}") } | ||
} | ||
|
||
internal val Namespace.Companion.Saver | ||
get() = Saver<Namespace, String>( | ||
save = { it.value }, | ||
restore = { Namespace(it) } | ||
) |
31 changes: 31 additions & 0 deletions
31
soil-query-compose/src/commonMain/kotlin/soil/query/compose/util/QueriesErrorReset.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2024 Soil Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package soil.query.compose.util | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import soil.query.ResumeQueriesFilter | ||
import soil.query.SwrClient | ||
import soil.query.compose.LocalSwrClient | ||
|
||
|
||
typealias QueriesErrorReset = () -> Unit | ||
|
||
/** | ||
* Remember a [QueriesErrorReset] to resume all queries with [filter] matched. | ||
* | ||
* @param filter The filter to match queries. | ||
* @param client The [SwrClient] to resume queries. By default, it uses the [LocalSwrClient]. | ||
* @return A [QueriesErrorReset] to resume queries. | ||
*/ | ||
@Composable | ||
fun rememberQueriesErrorReset( | ||
filter: ResumeQueriesFilter = remember { ResumeQueriesFilter(predicate = { it.isFailure }) }, | ||
client: SwrClient = LocalSwrClient.current | ||
): QueriesErrorReset { | ||
val reset = remember<() -> Unit>(client) { | ||
{ client.perform { resumeQueries(filter) } } | ||
} | ||
return reset | ||
} |