-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate suspending variant of "binding" in favour of "coroutineBind…
…ing" This matches the internally-called function named coroutineScope, and helps consumers distinguish between the blocking variant that is otherwise only differing in package name. This should also help convey to readers that structured concurrency will occur within the block.
- Loading branch information
1 parent
3615705
commit 219b634
Showing
5 changed files
with
95 additions
and
79 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
70 changes: 70 additions & 0 deletions
70
...utines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/CoroutineBinding.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,70 @@ | ||
package com.github.michaelbull.result.coroutines | ||
|
||
import com.github.michaelbull.result.Err | ||
import com.github.michaelbull.result.Ok | ||
import com.github.michaelbull.result.Result | ||
import kotlinx.coroutines.CancellationException | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* Suspending variant of [binding][com.github.michaelbull.result.binding]. | ||
* The suspendable [block] runs in a new [CoroutineScope], inheriting the parent [CoroutineContext]. | ||
* This new scope is [cancelled][CoroutineScope.cancel] once a failing bind is encountered, eagerly cancelling all | ||
* child [jobs][Job]. | ||
*/ | ||
public suspend inline fun <V, E> coroutineBinding(crossinline block: suspend CoroutineBindingScope<E>.() -> V): Result<V, E> { | ||
contract { | ||
callsInPlace(block, InvocationKind.EXACTLY_ONCE) | ||
} | ||
|
||
lateinit var receiver: CoroutineBindingScopeImpl<E> | ||
|
||
return try { | ||
coroutineScope { | ||
receiver = CoroutineBindingScopeImpl(this) | ||
|
||
with(receiver) { | ||
Ok(block()) | ||
} | ||
} | ||
} catch (ex: BindCancellationException) { | ||
receiver.result!! | ||
} | ||
} | ||
|
||
internal object BindCancellationException : CancellationException(null as String?) | ||
|
||
public interface CoroutineBindingScope<E> : CoroutineScope { | ||
public suspend fun <V> Result<V, E>.bind(): V | ||
} | ||
|
||
@PublishedApi | ||
internal class CoroutineBindingScopeImpl<E>( | ||
delegate: CoroutineScope, | ||
) : CoroutineBindingScope<E>, CoroutineScope by delegate { | ||
|
||
private val mutex = Mutex() | ||
var result: Result<Nothing, E>? = null | ||
|
||
override suspend fun <V> Result<V, E>.bind(): V { | ||
return when (this) { | ||
is Ok -> value | ||
is Err -> mutex.withLock { | ||
if (result == null) { | ||
result = this | ||
coroutineContext.cancel(BindCancellationException) | ||
} | ||
|
||
throw BindCancellationException | ||
} | ||
} | ||
} | ||
} |
74 changes: 10 additions & 64 deletions
74
.../commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.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 |
---|---|---|
@@ -1,76 +1,22 @@ | ||
package com.github.michaelbull.result.coroutines.binding | ||
|
||
import com.github.michaelbull.result.Err | ||
import com.github.michaelbull.result.Ok | ||
import com.github.michaelbull.result.Result | ||
import kotlinx.coroutines.CancellationException | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
import kotlin.coroutines.CoroutineContext | ||
import com.github.michaelbull.result.coroutines.CoroutineBindingScope | ||
import com.github.michaelbull.result.coroutines.coroutineBinding | ||
|
||
/** | ||
* Suspending variant of [binding][com.github.michaelbull.result.binding]. | ||
* The suspendable [block] runs in a new [CoroutineScope], inheriting the parent [CoroutineContext]. | ||
* This new scope is [cancelled][CoroutineScope.cancel] once a failing bind is encountered, eagerly cancelling all | ||
* child [jobs][Job]. | ||
*/ | ||
@Deprecated( | ||
message = "Use coroutineBinding instead", | ||
replaceWith = ReplaceWith( | ||
expression = "coroutineBinding(block)", | ||
imports = ["com.github.michaelbull.result.coroutines.coroutineBinding"] | ||
) | ||
) | ||
public suspend inline fun <V, E> binding(crossinline block: suspend CoroutineBindingScope<E>.() -> V): Result<V, E> { | ||
contract { | ||
callsInPlace(block, InvocationKind.EXACTLY_ONCE) | ||
} | ||
|
||
lateinit var receiver: CoroutineBindingScopeImpl<E> | ||
|
||
return try { | ||
coroutineScope { | ||
receiver = CoroutineBindingScopeImpl(this) | ||
|
||
with(receiver) { | ||
Ok(block()) | ||
} | ||
} | ||
} catch (ex: BindCancellationException) { | ||
receiver.result!! | ||
} | ||
return coroutineBinding(block) | ||
} | ||
|
||
internal object BindCancellationException : CancellationException(null as String?) | ||
|
||
@Deprecated( | ||
message = "Use CoroutineBindingScope instead", | ||
replaceWith = ReplaceWith("CoroutineBindingScope<E>") | ||
) | ||
public typealias SuspendableResultBinding<E> = CoroutineBindingScope<E> | ||
|
||
public interface CoroutineBindingScope<E> : CoroutineScope { | ||
public suspend fun <V> Result<V, E>.bind(): V | ||
} | ||
|
||
@PublishedApi | ||
internal class CoroutineBindingScopeImpl<E>( | ||
delegate: CoroutineScope, | ||
) : CoroutineBindingScope<E>, CoroutineScope by delegate { | ||
|
||
private val mutex = Mutex() | ||
var result: Result<Nothing, E>? = null | ||
|
||
override suspend fun <V> Result<V, E>.bind(): V { | ||
return when (this) { | ||
is Ok -> value | ||
is Err -> mutex.withLock { | ||
if (result == null) { | ||
result = this | ||
coroutineContext.cancel(BindCancellationException) | ||
} | ||
|
||
throw BindCancellationException | ||
} | ||
} | ||
} | ||
} |
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