Skip to content

Commit

Permalink
fix Micro.forEach for empty iterables (#3545)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Sep 4, 2024
1 parent dcb9ec0 commit 79aa6b1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-rings-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

fix Micro.forEach for empty iterables
6 changes: 6 additions & 0 deletions packages/effect/src/Micro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2594,9 +2594,11 @@ export const withTrace: {
const f = (name: string) => (self: Micro<any, any, any>) =>
unsafeMakeOptions(function(env, onExit) {
self[runSymbol](env, function(exit) {
console.log(name, exit)
onExit(exit._tag === "Left" ? Either.left(generate(name, exit.left)) : exit)
})
}, false)
console.log(arguments.length)
if (arguments.length === 2) {
return f(arguments[1])(arguments[0])
}
Expand Down Expand Up @@ -3465,6 +3467,9 @@ export const forEach: {
let result: MicroExit<any, any> | undefined = undefined
const items = Array.from(iterable)
let length = items.length
if (length === 0) {
return onExit(Either.right(options?.discard ? undefined : []))
}
const out: Array<B> | undefined = options?.discard ? undefined : new Array(length)
let index = 0
let inProgress = 0
Expand Down Expand Up @@ -3949,6 +3954,7 @@ export const runSyncExit = <A, E>(effect: Micro<A, E>): MicroExit<A, E> => {
const handle = runFork(effect, { scheduler })
scheduler.flush()
const exit = handle.unsafePoll()
console.log(exit)
if (exit === null) {
return exitDie(handle)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/effect/test/Micro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ describe.concurrent("Micro", () => {
assert.deepStrictEqual(result, Micro.exitFail("error"))
assert.deepStrictEqual(done, [1, 2, 3])
}).pipe(Micro.runPromise))

it("length = 0", () =>
Micro.gen(function*() {
const results = yield* Micro.forEach([], (_) => Micro.succeed(_))
assert.deepStrictEqual(results, [])
}).pipe(Micro.runPromise))
})

describe("all", () => {
Expand Down

0 comments on commit 79aa6b1

Please sign in to comment.