( all: Iterable<Eff> )
| 1408 | * @category sequencing |
| 1409 | */ |
| 1410 | export 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, |
no test coverage detected
searching dependent graphs…