effect 3.17.10 → 3.17.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,6 @@ import type * as Clock from "../Clock.js"
6
6
  import type { ConfigProvider } from "../ConfigProvider.js"
7
7
  import * as Context from "../Context.js"
8
8
  import type { DefaultServices } from "../DefaultServices.js"
9
- import * as Deferred from "../Deferred.js"
10
9
  import type * as Duration from "../Duration.js"
11
10
  import type * as Effect from "../Effect.js"
12
11
  import * as Effectable from "../Effectable.js"
@@ -2630,112 +2629,54 @@ export const raceAll: <Eff extends Effect.Effect<any, any, any>>(
2630
2629
  A,
2631
2630
  E,
2632
2631
  R
2633
- >(all: Iterable<Effect.Effect<A, E, R>>): Effect.Effect<A, E, R> => {
2634
- const list = Chunk.fromIterable(all)
2635
- if (!Chunk.isNonEmpty(list)) {
2636
- return core.dieSync(() => new core.IllegalArgumentException(`Received an empty collection of effects`))
2637
- }
2638
- const self = Chunk.headNonEmpty(list)
2639
- const effects = Chunk.tailNonEmpty(list)
2640
- const inheritAll = (res: readonly [A, Fiber.Fiber<A, E>]) =>
2641
- pipe(
2642
- internalFiber.inheritAll(res[1]),
2643
- core.as(res[0])
2644
- )
2645
- return pipe(
2646
- core.deferredMake<readonly [A, Fiber.Fiber<A, E>], E>(),
2647
- core.flatMap((done) =>
2648
- pipe(
2649
- Ref.make(effects.length),
2650
- core.flatMap((fails) =>
2651
- core.uninterruptibleMask<A, E, R>((restore) =>
2652
- pipe(
2653
- fork(core.interruptible(self)),
2654
- core.flatMap((head) =>
2655
- pipe(
2656
- effects,
2657
- core.forEachSequential((effect) => fork(core.interruptible(effect))),
2658
- core.map((fibers) => Chunk.unsafeFromArray(fibers)),
2659
- core.map((tail) => pipe(tail, Chunk.prepend(head)) as Chunk.Chunk<Fiber.RuntimeFiber<A, E>>),
2660
- core.tap((fibers) =>
2661
- pipe(
2662
- fibers,
2663
- RA.reduce(core.void, (effect, fiber) =>
2664
- pipe(
2665
- effect,
2666
- core.zipRight(
2667
- pipe(
2668
- internalFiber._await(fiber),
2669
- core.flatMap(raceAllArbiter(fibers, fiber, done, fails)),
2670
- fork,
2671
- core.asVoid
2672
- )
2673
- )
2674
- ))
2675
- )
2676
- ),
2677
- core.flatMap((fibers) =>
2678
- pipe(
2679
- restore(pipe(Deferred.await(done), core.flatMap(inheritAll))),
2680
- core.onInterrupt(() =>
2681
- pipe(
2682
- fibers,
2683
- RA.reduce(
2684
- core.void,
2685
- (effect, fiber) => pipe(effect, core.zipLeft(core.interruptFiber(fiber)))
2686
- )
2687
- )
2688
- )
2689
- )
2690
- )
2691
- )
2692
- )
2693
- )
2694
- )
2632
+ >(all: Iterable<Effect.Effect<A, E, R>>): Effect.Effect<A, E, R> =>
2633
+ core.withFiberRuntime((state, status) =>
2634
+ core.async<A, E, R>((resume) => {
2635
+ const fibers = new Set<FiberRuntime<A, E>>()
2636
+ let winner: FiberRuntime<A, E> | undefined
2637
+ let failures: Cause.Cause<E> = internalCause.empty
2638
+ const interruptAll = () => {
2639
+ for (const fiber of fibers) {
2640
+ fiber.unsafeInterruptAsFork(state.id())
2641
+ }
2642
+ }
2643
+ let latch = false
2644
+ let empty = true
2645
+ for (const self of all) {
2646
+ empty = false
2647
+ const fiber = unsafeFork(
2648
+ core.interruptible(self),
2649
+ state,
2650
+ status.runtimeFlags
2695
2651
  )
2696
- )
2697
- )
2652
+ fibers.add(fiber)
2653
+ fiber.addObserver((exit) => {
2654
+ fibers.delete(fiber)
2655
+ if (!winner) {
2656
+ if (exit._tag === "Success") {
2657
+ latch = true
2658
+ winner = fiber
2659
+ failures = internalCause.empty
2660
+ interruptAll()
2661
+ } else {
2662
+ failures = internalCause.parallel(exit.cause, failures)
2663
+ }
2664
+ }
2665
+ if (latch && fibers.size === 0) {
2666
+ resume(
2667
+ winner ? core.zipRight(internalFiber.inheritAll(winner), winner.unsafePoll()!) : core.failCause(failures)
2668
+ )
2669
+ }
2670
+ })
2671
+ if (winner) break
2672
+ }
2673
+ if (empty) {
2674
+ return resume(core.dieSync(() => new core.IllegalArgumentException(`Received an empty collection of effects`)))
2675
+ }
2676
+ latch = true
2677
+ return internalFiber.interruptAllAs(fibers, state.id())
2678
+ })
2698
2679
  )
2699
- }
2700
-
2701
- const raceAllArbiter = <E, E1, A, A1>(
2702
- fibers: Iterable<Fiber.Fiber<A | A1, E | E1>>,
2703
- winner: Fiber.Fiber<A | A1, E | E1>,
2704
- deferred: Deferred.Deferred<readonly [A | A1, Fiber.Fiber<A | A1, E | E1>], E | E1>,
2705
- fails: Ref.Ref<number>
2706
- ) =>
2707
- (exit: Exit.Exit<A | A1, E | E1>): Effect.Effect<void> =>
2708
- core.exitMatchEffect(exit, {
2709
- onFailure: (cause) =>
2710
- pipe(
2711
- Ref.modify(fails, (fails) =>
2712
- [
2713
- fails === 0 ?
2714
- pipe(core.deferredFailCause(deferred, cause), core.asVoid) :
2715
- core.void,
2716
- fails - 1
2717
- ] as const),
2718
- core.flatten
2719
- ),
2720
- onSuccess: (value): Effect.Effect<void> =>
2721
- pipe(
2722
- core.deferredSucceed(deferred, [value, winner] as const),
2723
- core.flatMap((set) =>
2724
- set ?
2725
- pipe(
2726
- Chunk.fromIterable(fibers),
2727
- RA.reduce(
2728
- core.void,
2729
- (effect, fiber) =>
2730
- fiber === winner ?
2731
- effect :
2732
- pipe(effect, core.zipLeft(core.interruptFiber(fiber)))
2733
- )
2734
- ) :
2735
- core.void
2736
- )
2737
- )
2738
- })
2739
2680
 
2740
2681
  /* @internal */
2741
2682
  export const reduceEffect = dual<
@@ -1,4 +1,4 @@
1
- let moduleVersion = "3.17.10"
1
+ let moduleVersion = "3.17.11"
2
2
 
3
3
  export const getCurrentVersion = () => moduleVersion
4
4