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

Function raceAll

packages/effect/src/Stream.ts:3952–3979  ·  view source on GitHub ↗
(
  ...streams: S
)

Source from the content-addressed store, hash-verified

3950 * @since 3.5.0
3951 */
3952export const raceAll = <S extends ReadonlyArray<Stream<any, any, any>>>(
3953 ...streams: S
3954): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>> =>
3955 fromChannel(Channel.fromTransform((_, scope) =>
3956 Effect.sync(() => {
3957 let winner:
3958 | Pull.Pull<Arr.NonEmptyReadonlyArray<Success<S[number]>>, Error<S[number]>, void, Services<S[number]>>
3959 | undefined
3960 const race = Effect.raceAll(streams.map((stream) => {
3961 const childScope = Scope.forkUnsafe(scope)
3962 return Channel.toPullScoped(stream.channel, childScope).pipe(
3963 Effect.flatMap((pull) => Effect.zip(Effect.succeed(pull), pull)),
3964 Effect.onExit((exit) => {
3965 if (exit._tag === "Success") {
3966 if (winner) {
3967 return Scope.close(childScope, exit)
3968 }
3969 winner = exit.value[0]
3970 return Effect.void
3971 }
3972 return Scope.close(childScope, exit)
3973 }),
3974 Effect.map(([, chunk]) => chunk)
3975 )
3976 }))
3977 return Effect.suspend(() => winner ?? race)
3978 })
3979 ))
3980
3981/**
3982 * Runs both streams concurrently until one stream emits its first value, then

Callers 1

Stream.tsFile · 0.70

Calls 6

fromChannelFunction · 0.70
pipeMethod · 0.65
closeMethod · 0.65
syncMethod · 0.45
mapMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected