MCPcopy Create free account
hub / github.com/Effect-TS/effect / raceAll

Function raceAll

packages/effect/src/Micro.ts:1411–1447  ·  view source on GitHub ↗
(
  all: Iterable<Eff>
)

Source from the content-addressed store, hash-verified

1409 * @category sequencing
1410 */
1411export const raceAll = <Eff extends Micro<any, any, any>>(
1412 all: Iterable<Eff>
1413): Micro<Micro.Success<Eff>, Micro.Error<Eff>, Micro.Context<Eff>> =>
1414 withMicroFiber((parent) =>
1415 async((resume) => {
1416 const effects = Arr.fromIterable(all)
1417 const len = effects.length
1418 let doneCount = 0
1419 let done = false
1420 const fibers = new Set<MicroFiber<any, any>>()
1421 const causes: Array<MicroCause<any>> = []
1422 const onExit = (exit: MicroExit<any, any>) => {
1423 doneCount++
1424 if (exit._tag === "Failure") {
1425 causes.push(exit.cause)
1426 if (doneCount >= len) {
1427 resume(failCause(causes[0]))
1428 }
1429 return
1430 }
1431 done = true
1432 resume(fibers.size === 0 ? exit : flatMap(uninterruptible(fiberInterruptAll(fibers)), () => exit))
1433 }
1434
1435 for (let i = 0; i < len; i++) {
1436 if (done) break
1437 const fiber = unsafeFork(parent, interruptible(effects[i]), true, true)
1438 fibers.add(fiber)
1439 fiber.addObserver((exit) => {
1440 fibers.delete(fiber)
1441 onExit(exit)
1442 })
1443 }
1444
1445 return fiberInterruptAll(fibers)
1446 })
1447 )
1448
1449/**
1450 * Returns an effect that races all the specified effects,

Callers 1

Micro.tsFile · 0.70

Calls 7

asyncFunction · 0.85
fiberInterruptAllFunction · 0.85
unsafeForkFunction · 0.70
interruptibleFunction · 0.70
onExitFunction · 0.70
addMethod · 0.65
addObserverMethod · 0.65

Tested by

no test coverage detected