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

Function mapEffectConcurrent

packages/effect/src/Channel.ts:2274–2373  ·  view source on GitHub ↗
(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  f: (o: OutElem, i: number) => Effect.Effect<OutElem2, EX, RX>,
  options: {
    readonly concurrency: Types.Concurrency
    readonly unordered?: boolean | undefined
  }
)

Source from the content-addressed store, hash-verified

2272 })
2273
2274const mapEffectConcurrent = <
2275 OutElem,
2276 OutErr,
2277 OutDone,
2278 InElem,
2279 InErr,
2280 InDone,
2281 Env,
2282 OutElem2,
2283 EX,
2284 RX
2285>(
2286 self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
2287 f: (o: OutElem, i: number) => Effect.Effect<OutElem2, EX, RX>,
2288 options: {
2289 readonly concurrency: Types.Concurrency
2290 readonly unordered?: boolean | undefined
2291 }
2292): Channel<OutElem2, OutErr | EX, OutDone, InElem, InErr, InDone, Env | RX> =>
2293 fromTransformBracket(
2294 Effect.fnUntraced(function*(upstream, scope, forkedScope) {
2295 let i = 0
2296 const pull = yield* toTransform(self)(upstream, scope)
2297 const concurrencyN = options.concurrency === "unbounded"
2298 ? Number.MAX_SAFE_INTEGER
2299 : Math.min(options.concurrency, Number.MAX_SAFE_INTEGER)
2300 const queue = yield* Queue.bounded<OutElem2, OutErr | EX | Cause.Done<OutDone>>(0)
2301 yield* Scope.addFinalizer(forkedScope, Queue.shutdown(queue))
2302
2303 const runFork = Effect.runForkWith(yield* Effect.context<RX>())
2304 const trackFiber = Fiber.runIn(forkedScope)
2305
2306 if (options.unordered) {
2307 const semaphore = Semaphore.makeUnsafe(concurrencyN)
2308 const release = constant(semaphore.release(1))
2309 const handle = Effect.matchCauseEffect({
2310 onFailure: (cause: Cause.Cause<EX>) => Effect.flatMap(Queue.failCause(queue, cause), release),
2311 onSuccess: (value: OutElem2) => Effect.flatMap(Queue.offer(queue, value), release)
2312 })
2313 yield* semaphore.take(1).pipe(
2314 Effect.flatMap(() => pull),
2315 Effect.flatMap((value) => {
2316 const index = i++
2317 trackFiber(runFork(handle(Effect.suspend(() => f(value, index)))))
2318 return Effect.void
2319 }),
2320 Effect.forever({ disableYield: true }),
2321 Effect.catchCause((cause) =>
2322 semaphore.withPermits(concurrencyN - 1)(
2323 Queue.failCause(queue, cause)
2324 )
2325 ),
2326 Effect.forkIn(forkedScope)
2327 )
2328 } else {
2329 // capacity is n - 2 because
2330 // - 1 for the offer *after* starting a fiber
2331 // - 1 for the current processing fiber

Callers 1

Channel.tsFile · 0.85

Calls 15

constantFunction · 0.90
fromTransformBracketFunction · 0.85
toTransformFunction · 0.85
runForkFunction · 0.85
addFinalizerMethod · 0.80
contextMethod · 0.80
addObserverMethod · 0.80
releaseMethod · 0.65
pipeMethod · 0.65
takeMethod · 0.65
withPermitsMethod · 0.65
handleFunction · 0.50

Tested by

no test coverage detected