Skip to content

Commit

Permalink
Revert XxxRef from Extension Function to Interface Function
Browse files Browse the repository at this point in the history
Reverted the `XxxRef` implementation from an extension function back to an interface-based function due to unexpected
behavior when combining `PreviewClient` for Compose with `QueryCachingStrategy.NetworkFirst`. By reverting to an
interface-based function, PreviewClient can handle the `resume` function properly, ensuring expected behavior across all
combinations.

refs: #70
  • Loading branch information
ogaclejapan committed Aug 24, 2024
1 parent de6d696 commit 231fb7c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import soil.query.QueryStatus
import soil.query.core.getOrThrow
import soil.query.core.isNone
import soil.query.core.map
import soil.query.invalidate
import soil.query.loadMore

/**
* Remember a [InfiniteQueryObject] and subscribes to the query state of [key].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import soil.query.MutationKey
import soil.query.MutationRef
import soil.query.MutationState
import soil.query.MutationStatus
import soil.query.mutate
import soil.query.mutateAsync
import soil.query.reset

/**
* Remember a [MutationObject] and subscribes to the mutation state of [key].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import soil.query.QueryState
import soil.query.annotation.ExperimentalSoilQueryApi
import soil.query.core.UniqueId
import soil.query.core.isNone
import soil.query.resume

/**
* A mechanism to finely adjust the behavior of the query results on a component basis in Composable functions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import soil.query.QueryState
import soil.query.QueryStatus
import soil.query.core.isNone
import soil.query.core.map
import soil.query.invalidate

/**
* Remember a [QueryObject] and subscribes to the query state of [key].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class QueryPreviewClient(
) : QueryRef<T> {
override fun launchIn(scope: CoroutineScope): Job = Job()
override suspend fun send(command: QueryCommand<T>) = Unit
override suspend fun resume() = Unit
override suspend fun invalidate() = Unit
}

private class SnapshotInfiniteQuery<T, S>(
Expand All @@ -71,5 +73,8 @@ class QueryPreviewClient(
) : InfiniteQueryRef<T, S> {
override fun launchIn(scope: CoroutineScope): Job = Job()
override suspend fun send(command: InfiniteQueryCommand<T, S>) = Unit
override suspend fun resume() = Unit
override suspend fun loadMore(param: S) = Unit
override suspend fun invalidate() = Unit
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,34 @@ interface InfiniteQueryRef<T, S> : Actor {
* Sends a [QueryCommand] to the Actor.
*/
suspend fun send(command: InfiniteQueryCommand<T, S>)
}

/**
* Invalidates the Query.
*
* Calling this function will invalidate the retrieved data of the Query,
* setting [QueryModel.isInvalidated] to `true` until revalidation is completed.
*/
suspend fun <T, S> InfiniteQueryRef<T, S>.invalidate() {
val deferred = CompletableDeferred<QueryChunks<T, S>>()
send(InfiniteQueryCommands.Invalidate(key, state.value.revision, deferred::completeWith))
deferred.awaitOrNull()
}
/**
* Resumes the Query.
*/
suspend fun resume() {
val deferred = CompletableDeferred<QueryChunks<T, S>>()
send(InfiniteQueryCommands.Connect(key, state.value.revision, deferred::completeWith))
deferred.awaitOrNull()
}

/**
* Resumes the Query.
*/
suspend fun <T, S> InfiniteQueryRef<T, S>.resume() {
val deferred = CompletableDeferred<QueryChunks<T, S>>()
send(InfiniteQueryCommands.Connect(key, state.value.revision, deferred::completeWith))
deferred.awaitOrNull()
}
/**
* Fetches data for the [InfiniteQueryKey] using the value of [param].
*/
suspend fun loadMore(param: S) {
val deferred = CompletableDeferred<QueryChunks<T, S>>()
send(InfiniteQueryCommands.LoadMore(key, param, deferred::completeWith))
deferred.awaitOrNull()
}

/**
* Fetches data for the [InfiniteQueryKey] using the value of [param].
*/
suspend fun <T, S> InfiniteQueryRef<T, S>.loadMore(param: S) {
val deferred = CompletableDeferred<QueryChunks<T, S>>()
send(InfiniteQueryCommands.LoadMore(key, param, deferred::completeWith))
deferred.awaitOrNull()
/**
* Invalidates the Query.
*
* Calling this function will invalidate the retrieved data of the Query,
* setting [QueryModel.isInvalidated] to `true` until revalidation is completed.
*/
suspend fun invalidate() {
val deferred = CompletableDeferred<QueryChunks<T, S>>()
send(InfiniteQueryCommands.Invalidate(key, state.value.revision, deferred::completeWith))
deferred.awaitOrNull()
}
}
50 changes: 25 additions & 25 deletions soil-query-core/src/commonMain/kotlin/soil/query/MutationRef.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ interface MutationRef<T, S> : Actor {
* Sends a [MutationCommand] to the Actor.
*/
suspend fun send(command: MutationCommand<T>)
}

/**
* Mutates the variable.
*
* @param variable The variable to be mutated.
* @return The result of the mutation.
*/
suspend fun <T, S> MutationRef<T, S>.mutate(variable: S): T {
val deferred = CompletableDeferred<T>()
send(MutationCommands.Mutate(key, variable, state.value.revision, deferred::completeWith))
return deferred.await()
}
/**
* Mutates the variable.
*
* @param variable The variable to be mutated.
* @return The result of the mutation.
*/
suspend fun mutate(variable: S): T {
val deferred = CompletableDeferred<T>()
send(MutationCommands.Mutate(key, variable, state.value.revision, deferred::completeWith))
return deferred.await()
}

/**
* Mutates the variable asynchronously.
*
* @param variable The variable to be mutated.
*/
suspend fun <T, S> MutationRef<T, S>.mutateAsync(variable: S) {
send(MutationCommands.Mutate(key, variable, state.value.revision))
}
/**
* Mutates the variable asynchronously.
*
* @param variable The variable to be mutated.
*/
suspend fun mutateAsync(variable: S) {
send(MutationCommands.Mutate(key, variable, state.value.revision))
}

/**
* Resets the mutation state.
*/
suspend fun <T, S> MutationRef<T, S>.reset() {
send(MutationCommands.Reset())
/**
* Resets the mutation state.
*/
suspend fun reset() {
send(MutationCommands.Reset())
}
}
38 changes: 19 additions & 19 deletions soil-query-core/src/commonMain/kotlin/soil/query/QueryRef.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ interface QueryRef<T> : Actor {
* Sends a [QueryCommand] to the Actor.
*/
suspend fun send(command: QueryCommand<T>)
}

/**
* Invalidates the Query.
*
* Calling this function will invalidate the retrieved data of the Query,
* setting [QueryModel.isInvalidated] to `true` until revalidation is completed.
*/
suspend fun <T> QueryRef<T>.invalidate() {
val deferred = CompletableDeferred<T>()
send(QueryCommands.Invalidate(key, state.value.revision, deferred::completeWith))
deferred.awaitOrNull()
}
/**
* Resumes the Query.
*/
suspend fun resume() {
val deferred = CompletableDeferred<T>()
send(QueryCommands.Connect(key, state.value.revision, deferred::completeWith))
deferred.awaitOrNull()
}

/**
* Resumes the Query.
*/
suspend fun <T> QueryRef<T>.resume() {
val deferred = CompletableDeferred<T>()
send(QueryCommands.Connect(key, state.value.revision, deferred::completeWith))
deferred.awaitOrNull()
/**
* Invalidates the Query.
*
* Calling this function will invalidate the retrieved data of the Query,
* setting [QueryModel.isInvalidated] to `true` until revalidation is completed.
*/
suspend fun invalidate() {
val deferred = CompletableDeferred<T>()
send(QueryCommands.Invalidate(key, state.value.revision, deferred::completeWith))
deferred.awaitOrNull()
}
}

0 comments on commit 231fb7c

Please sign in to comment.