-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Within the Composable functions provided by the Query library, recomposition occurs due to changes in the internal state related to data loading. To reduce unnecessary state updates in the UI, we have implemented a RecompositionOptimizer that omits certain state changes based on conditions. This RecompositionOptimizer can be configured on a per-Composable function basis, and the optimization is enabled by default. If you need to reference the omitted state items in the UI, you can disable the optimization through the configuration settings.
- Loading branch information
1 parent
a372f6e
commit 7b07346
Showing
33 changed files
with
2,153 additions
and
100 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
71 changes: 71 additions & 0 deletions
71
...y-compose/src/commonMain/kotlin/soil/query/compose/InfiniteQueryRecompositionOptimizer.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,71 @@ | ||
// Copyright 2024 Soil Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package soil.query.compose | ||
|
||
import soil.query.QueryChunks | ||
import soil.query.QueryState | ||
import soil.query.QueryStatus | ||
|
||
/** | ||
* A recomposition optimizer for [QueryState] with [QueryChunks]. | ||
*/ | ||
interface InfiniteQueryRecompositionOptimizer { | ||
|
||
/** | ||
* Omit the specified keys from the [QueryState] with [QueryChunks]. | ||
* | ||
* @param state The infinite query state. | ||
* @return The optimized infinite query state. | ||
*/ | ||
fun <T, S> omit(state: QueryState<QueryChunks<T, S>>): QueryState<QueryChunks<T, S>> | ||
|
||
companion object | ||
} | ||
|
||
/** | ||
* Optimizer implementation for [InfiniteQueryStrategy.Companion.Default]. | ||
*/ | ||
val InfiniteQueryRecompositionOptimizer.Companion.Default: InfiniteQueryRecompositionOptimizer | ||
get() = DefaultInfiniteQueryRecompositionOptimizer | ||
|
||
private object DefaultInfiniteQueryRecompositionOptimizer : InfiniteQueryRecompositionOptimizer { | ||
override fun <T, S> omit(state: QueryState<QueryChunks<T, S>>): QueryState<QueryChunks<T, S>> { | ||
val keys = buildSet { | ||
add(QueryState.OmitKey.replyUpdatedAt) | ||
add(QueryState.OmitKey.staleAt) | ||
when (state.status) { | ||
QueryStatus.Pending -> { | ||
add(QueryState.OmitKey.errorUpdatedAt) | ||
add(QueryState.OmitKey.fetchStatus) | ||
} | ||
|
||
QueryStatus.Success -> { | ||
add(QueryState.OmitKey.errorUpdatedAt) | ||
if (!state.isInvalidated) { | ||
add(QueryState.OmitKey.fetchStatus) | ||
} | ||
} | ||
|
||
QueryStatus.Failure -> { | ||
if (!state.isInvalidated) { | ||
add(QueryState.OmitKey.fetchStatus) | ||
} | ||
} | ||
} | ||
} | ||
return state.omit(keys) | ||
} | ||
} | ||
|
||
/** | ||
* Option that performs no optimization. | ||
*/ | ||
val InfiniteQueryRecompositionOptimizer.Companion.Disabled: InfiniteQueryRecompositionOptimizer | ||
get() = DisabledInfiniteQueryRecompositionOptimizer | ||
|
||
private object DisabledInfiniteQueryRecompositionOptimizer : InfiniteQueryRecompositionOptimizer { | ||
override fun <T, S> omit(state: QueryState<QueryChunks<T, S>>): QueryState<QueryChunks<T, S>> { | ||
return state | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
...-query-compose/src/commonMain/kotlin/soil/query/compose/MutationRecompositionOptimizer.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,68 @@ | ||
// Copyright 2024 Soil Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package soil.query.compose | ||
|
||
import soil.query.MutationState | ||
import soil.query.MutationStatus | ||
|
||
/** | ||
* A recomposition optimizer for [MutationState]. | ||
*/ | ||
interface MutationRecompositionOptimizer { | ||
|
||
/** | ||
* Omit the specified keys from the [MutationState]. | ||
* | ||
* @param state The mutation state. | ||
* @return The optimized mutation state. | ||
*/ | ||
fun <T> omit(state: MutationState<T>): MutationState<T> | ||
|
||
companion object | ||
} | ||
|
||
/** | ||
* Optimizer implementation for [MutationStrategy.Companion.Default]. | ||
*/ | ||
val MutationRecompositionOptimizer.Companion.Default: MutationRecompositionOptimizer | ||
get() = DefaultMutationRecompositionOptimizer | ||
|
||
private object DefaultMutationRecompositionOptimizer : MutationRecompositionOptimizer { | ||
override fun <T> omit(state: MutationState<T>): MutationState<T> { | ||
val keys = buildSet { | ||
add(MutationState.OmitKey.replyUpdatedAt) | ||
add(MutationState.OmitKey.mutatedCount) | ||
when (state.status) { | ||
MutationStatus.Idle -> { | ||
add(MutationState.OmitKey.errorUpdatedAt) | ||
} | ||
|
||
MutationStatus.Pending -> { | ||
if (state.error == null) { | ||
add(MutationState.OmitKey.errorUpdatedAt) | ||
} | ||
} | ||
|
||
MutationStatus.Success -> { | ||
add(MutationState.OmitKey.errorUpdatedAt) | ||
} | ||
|
||
MutationStatus.Failure -> Unit | ||
} | ||
} | ||
return state.omit(keys) | ||
} | ||
} | ||
|
||
/** | ||
* Option that performs no optimization. | ||
*/ | ||
val MutationRecompositionOptimizer.Companion.Disabled: MutationRecompositionOptimizer | ||
get() = DisabledMutationRecompositionOptimizer | ||
|
||
private object DisabledMutationRecompositionOptimizer : MutationRecompositionOptimizer { | ||
override fun <T> omit(state: MutationState<T>): MutationState<T> { | ||
return state | ||
} | ||
} |
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
Oops, something went wrong.