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

Function raceAllFirst

packages/effect/src/internal/effect.ts:1534–1579  ·  view source on GitHub ↗
(
  all: Iterable<Eff>,
  options?: {
    readonly onWinner?: (options: {
      readonly fiber: Fiber.Fiber<any, any>
      readonly index: number
      readonly parentFiber: Fiber.Fiber<any, any>
    }) => void
  }
)

Source from the content-addressed store, hash-verified

1532
1533/** @internal */
1534export const raceAllFirst = <Eff extends Effect.Effect<any, any, any>>(
1535 all: Iterable<Eff>,
1536 options?: {
1537 readonly onWinner?: (options: {
1538 readonly fiber: Fiber.Fiber<any, any>
1539 readonly index: number
1540 readonly parentFiber: Fiber.Fiber<any, any>
1541 }) => void
1542 }
1543): Effect.Effect<
1544 Effect.Success<Eff>,
1545 Effect.Error<Eff>,
1546 Effect.Services<Eff>
1547> =>
1548 withFiber((parent) =>
1549 callback((resume) => {
1550 let done = false
1551 const fibers = new Set<Fiber.Fiber<any, any>>()
1552 const onExit = (exit: Exit.Exit<any, any>) => {
1553 done = true
1554 resume(
1555 fibers.size === 0
1556 ? exit
1557 : flatMap(uninterruptible(fiberInterruptAll(fibers)), () => exit)
1558 )
1559 }
1560
1561 let i = 0
1562 for (const effect of all) {
1563 if (done) break
1564 const index = i++
1565 const fiber = forkUnsafe(parent, effect, true, true, false)
1566 fibers.add(fiber)
1567 fiber.addObserver((exit) => {
1568 fibers.delete(fiber)
1569 const isWinner = !done
1570 onExit(exit)
1571 if (isWinner && options?.onWinner) {
1572 options.onWinner({ fiber, index, parentFiber: parent })
1573 }
1574 })
1575 }
1576
1577 return fiberInterruptAll(fibers)
1578 })
1579 )
1580
1581/** @internal */
1582export const race: {

Callers 1

effect.tsFile · 0.85

Calls 5

fiberInterruptAllFunction · 0.85
addObserverMethod · 0.80
callbackFunction · 0.70
onExitFunction · 0.70
addMethod · 0.65

Tested by

no test coverage detected