MCPcopy
hub / github.com/Effect-TS/effect / raceAll

Function raceAll

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

Source from the content-addressed store, hash-verified

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