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

Function forEach

packages/effect/src/Micro.ts:3879–3971  ·  view source on GitHub ↗
(iterable: Iterable<A>, f: (a: A, index: number) => Micro<B, E, R>, options?: {
  readonly concurrency?: Concurrency | undefined
  readonly discard?: boolean | undefined
})

Source from the content-addressed store, hash-verified

3877 readonly discard: true
3878 }): Micro<void, E, R>
3879} = <
3880 A,
3881 B,
3882 E,
3883 R
3884>(iterable: Iterable<A>, f: (a: A, index: number) => Micro<B, E, R>, options?: {
3885 readonly concurrency?: Concurrency | undefined
3886 readonly discard?: boolean | undefined
3887}): Micro<any, E, R> =>
3888 withMicroFiber((parent) => {
3889 const concurrencyOption = options?.concurrency === "inherit"
3890 ? parent.getRef(CurrentConcurrency)
3891 : options?.concurrency ?? 1
3892 const concurrency = concurrencyOption === "unbounded"
3893 ? Number.POSITIVE_INFINITY
3894 : Math.max(1, concurrencyOption)
3895
3896 const items = Arr.fromIterable(iterable)
3897 let length = items.length
3898 if (length === 0) {
3899 return options?.discard ? void_ : succeed([])
3900 }
3901
3902 const out: Array<B> | undefined = options?.discard ? undefined : new Array(length)
3903 let index = 0
3904
3905 if (concurrency === 1) {
3906 return as(
3907 whileLoop({
3908 while: () => index < items.length,
3909 body: () => f(items[index], index),
3910 step: out ?
3911 (b) => out[index++] = b :
3912 (_) => index++
3913 }),
3914 out as any
3915 )
3916 }
3917 return async((resume) => {
3918 const fibers = new Set<MicroFiber<unknown, unknown>>()
3919 let result: MicroExit<any, any> | undefined = undefined
3920 let inProgress = 0
3921 let doneCount = 0
3922 let pumping = false
3923 let interrupted = false
3924 function pump() {
3925 pumping = true
3926 while (inProgress < concurrency && index < length) {
3927 const currentIndex = index
3928 const item = items[currentIndex]
3929 index++
3930 inProgress++
3931 try {
3932 const child = unsafeFork(parent, f(item, currentIndex), true, true)
3933 fibers.add(child)
3934 child.addObserver((exit) => {
3935 fibers.delete(child)
3936 if (interrupted) {

Callers 4

closeMethod · 0.70
allFunction · 0.70
filterMapFunction · 0.70
SortedSet.tsFile · 0.70

Calls 9

asFunction · 0.85
whileLoopFunction · 0.85
asyncFunction · 0.85
pumpFunction · 0.85
fiberInterruptAllFunction · 0.85
getRefMethod · 0.80
fFunction · 0.70
suspendFunction · 0.70
succeedFunction · 0.50

Tested by

no test coverage detected