Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the results of synchronously executed Mutations. #45

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ interface MutationClient {
}

typealias MutationOptionsOverride = (MutationOptions) -> MutationOptions
typealias MutationCallback<T> = (Result<T>) -> Unit
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ suspend fun <T, S> MutationCommand.Context<T>.mutate(
*/
suspend inline fun <T, S> MutationCommand.Context<T>.dispatchMutateResult(
key: MutationKey<T, S>,
variable: S
variable: S,
noinline callback: MutationCallback<T>?
) {
mutate(key, variable)
.onSuccess { data ->
Expand All @@ -98,6 +99,9 @@ suspend inline fun <T, S> MutationCommand.Context<T>.dispatchMutateResult(
}
.onFailure { dispatch(MutationAction.MutateFailure(it)) }
.onFailure { options.onError?.invoke(it, state, key.id) }
.also {
callback?.invoke(it)
}
}

internal fun <T> MutationCommand.Context<T>.onRetryCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package soil.query

import soil.query.internal.vvv
import kotlin.coroutines.cancellation.CancellationException

/**
* Mutation commands are used to update the [mutation state][MutationState].
Expand All @@ -24,16 +25,18 @@ sealed class MutationCommands<T> : MutationCommand<T> {
data class Mutate<T, S>(
val key: MutationKey<T, S>,
val variable: S,
val revision: String
val revision: String,
val callback: MutationCallback<T>? = null
) : MutationCommands<T>() {

override suspend fun handle(ctx: MutationCommand.Context<T>) {
if (!ctx.shouldMutate(revision)) {
ctx.options.vvv(key.id) { "skip mutation(shouldMutate=false)" }
callback?.invoke(Result.failure(CancellationException("skip mutation")))
return
}
ctx.dispatch(MutationAction.Mutating)
ctx.dispatchMutateResult(key, variable)
ctx.dispatchMutateResult(key, variable, callback)
}
}

Expand Down
17 changes: 5 additions & 12 deletions soil-query-core/src/commonMain/kotlin/soil/query/MutationRef.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

package soil.query

import kotlinx.coroutines.flow.dropWhile
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.CompletableDeferred
import soil.query.internal.toResultCallback

/**
* A reference to a [Mutation] for [MutationKey].
Expand All @@ -28,16 +28,9 @@ class MutationRef<T, S>(
* @return The result of the mutation.
*/
suspend fun mutate(variable: S): T {
mutateAsync(variable)
val submittedAt = state.value.submittedAt
val result = state.dropWhile { it.submittedAt <= submittedAt }.first()
if (result.isSuccess) {
return result.data!!
} else if (result.isFailure) {
throw result.error!!
} else {
error("Unexpected ${result.status}")
}
val deferred = CompletableDeferred<T>()
command.send(MutationCommands.Mutate(key, variable, state.value.revision, deferred.toResultCallback()))
return deferred.await()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Soil Contributors
// SPDX-License-Identifier: Apache-2.0

package soil.query.internal

import kotlinx.coroutines.CompletableDeferred

internal fun <T> CompletableDeferred<T>.toResultCallback(): (Result<T>) -> Unit {
return { result ->
result
.onSuccess { complete(it) }
.onFailure { completeExceptionally(it) }
}
}