Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Add fiber integration + docs #86

Merged
merged 7 commits into from
Mar 6, 2020
Merged

Conversation

nomisRev
Copy link
Member

@nomisRev nomisRev commented Mar 5, 2020

Solves #84

This PR adds a combinator to launch a Fiber and attach it's cancelation system to KotlinX's CoroutineScope.

This allows for automatic cancelation of fibers with structured concurrency, useful for scoping a fiber to the render scope on Android for example.

Additionally I noticed we don't check isActive, I'd like to add some tests for this before I promote the PR but it's ready to review already.

TODO: add tests for isActive.

Copy link
Member

@aballano aballano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice changes, thanks for adding the docs!

val promise = UnsafePromise<A>()

if (newContext.isActive) {
val disposable = IO.unit.continueOn(newContext).flatMap { fix() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a difference of doing newContext.shift().followedBy(fix()) instead? Mainly wondering because I've seen both codes in different places but didn't really grasp the diff if any 😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No difference, newContext.shift() <==> IO.unit.continueOn(newContext).

*/
suspend fun <A> IOOf<A>.suspendCancellable(): A =
suspendCancellableCoroutine { cont ->
if (cont.isActive) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, we should not even run our thing if the enclosing scope is already cancelled 👍

fun <A> IOOf<A>.unsafeRunScoped(
scope: CoroutineScope,
f: (Either<Throwable, A>) -> Unit
cb: (Either<Throwable, A>) -> Unit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these part of the work for homogenizing names across the library?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, but that was the reason I renamed here.

So far all callbacks in Arrow Fx have the shape (Either<Throwable, A>) -> Unit with param name cb.

@aballano aballano linked an issue Mar 6, 2020 that may be closed by this pull request
Copy link
Member

@aballano aballano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

@nomisRev nomisRev marked this pull request as ready for review March 6, 2020 16:27
@nomisRev nomisRev merged commit b44662e into master Mar 6, 2020
@aballano aballano deleted the sv-kotlinx-fiber-integration branch March 9, 2020 09:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fork a Fiber that participates in structured concurrency
4 participants