-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: interruptibility of zipWithC (via ZIO)
- Loading branch information
Showing
5 changed files
with
85 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fncts/io": patch | ||
--- | ||
|
||
fix: interruptibility of zipWithC (via ZIO) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,69 @@ | ||
import { tuple } from "@fncts/base/data/function"; | ||
import { AtomicReference } from "@fncts/base/internal/AtomicReference"; | ||
|
||
/** | ||
* @tsplus fluent fncts.io.IO zipC | ||
*/ | ||
export function zipC_<R, E, A, R1, E1, B>(self: IO<R, E, A>, that: IO<R1, E1, B>): IO<R & R1, E | E1, readonly [A, B]> { | ||
return self.zipWithC(that, tuple); | ||
} | ||
|
||
/** | ||
* @tsplus fluent fncts.io.IO zipWithC | ||
*/ | ||
export function zipWithC_<R, E, A, R1, E1, B, C>( | ||
self: IO<R, E, A>, | ||
that: IO<R1, E1, B>, | ||
f: (a: A, b: B) => C, | ||
): IO<R & R1, E | E1, C> { | ||
return IO.descriptorWith((descriptor) => | ||
IO.uninterruptibleMask(({ restore }) => { | ||
const future = Future.unsafeMake<void, C>(FiberId.none); | ||
const ref = new AtomicReference<Maybe<Either<A, B>>>(Nothing()); | ||
return IO.transplant((graft) => | ||
graft( | ||
restore(self).matchCauseIO( | ||
(cause) => future.fail(undefined) > IO.failCauseNow(cause), | ||
(a) => | ||
ref.getAndSet(Just(Either.left(a))).match( | ||
() => IO.unit, | ||
(value) => | ||
value.match( | ||
() => IO.unit, | ||
(b) => future.succeed(f(a, b)).asUnit, | ||
), | ||
), | ||
), | ||
) | ||
.forkDaemon.zip( | ||
graft( | ||
restore(that).matchCauseIO( | ||
(cause) => future.fail(undefined) > IO.failCauseNow(cause), | ||
(b) => | ||
ref.getAndSet(Just(Either.right(b))).match( | ||
() => IO.unit, | ||
(value) => | ||
value.match( | ||
(a) => future.succeed(f(a, b)).asUnit, | ||
() => IO.unit, | ||
), | ||
), | ||
), | ||
).forkDaemon, | ||
) | ||
.flatMap(([left, right]) => | ||
restore(future.await).matchCauseIO( | ||
(cause) => | ||
left | ||
.interruptAs(descriptor.id) | ||
.zipC(right.interruptAs(descriptor.id)) | ||
.flatMap(([left, right]) => | ||
left.zipC(right).match(IO.failCauseNow, () => IO.failCauseNow(cause.stripFailures)), | ||
), | ||
(c) => left.inheritRefs.zip(right.inheritRefs).as(c), | ||
), | ||
), | ||
); | ||
}), | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
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