Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-stream-range-zero-chunk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Ensure `Stream.range` emits the full range when the chunk size is zero.
7 changes: 4 additions & 3 deletions packages/effect/src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,14 +1570,15 @@ export const range = (
chunkSize = Channel.DefaultChunkSize
): Stream<number> =>
min > max ? empty : fromPull(Effect.sync(() => {
const size = Math.max(1, chunkSize)
let start = min
let done = false
return Effect.suspend(() => {
if (done) return Cause.done()
const remaining = max - start + 1
if (remaining > chunkSize) {
const chunk = Arr.range(start, start + chunkSize - 1)
start += chunkSize
if (remaining > size) {
const chunk = Arr.range(start, start + size - 1)
start += size
return Effect.succeed(chunk)
}
const chunk = Arr.range(start, start + remaining - 1)
Expand Down
10 changes: 10 additions & 0 deletions packages/effect/test/Stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,16 @@ describe("Stream", () => {
assert.deepStrictEqual(result, [1, 2, 3])
}))

it.effect("range - zero chunk size does not change the emitted range", () =>
Effect.gen(function*() {
const result = yield* Stream.range(1, 3, 0).pipe(
Stream.take(4),
Stream.runCollect
)

assert.deepStrictEqual(result, [1, 2, 3])
}))

it.effect("service", () =>
Effect.gen(function*() {
const result = yield* Stream.service(Greeter).pipe(
Expand Down
Loading