| 3950 | * @since 3.5.0 |
| 3951 | */ |
| 3952 | export 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 |