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/tidy-cats-stream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Preserve lexical ordering in streaming template interpolation.
11 changes: 6 additions & 5 deletions packages/effect/src/unstable/http/Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ export function stream<A extends ReadonlyArray<InterpolatedWithStream>>(
buffer = ""
}

return Stream.flatMap(
Stream.fromIterable(chunks),
(chunk) =>
typeof chunk === "string" ? Stream.succeed(chunk) : Effect.isEffect(chunk) ? Stream.fromEffect(chunk) : chunk,
{ concurrency: "unbounded" }
return Stream.fromIterable(chunks).pipe(
Stream.mapEffect(
(chunk) => Effect.isEffect(chunk) ? chunk : Effect.succeed(chunk),
{ concurrency: "unbounded" }
),
Stream.flatMap((chunk) => typeof chunk === "string" ? Stream.succeed(chunk) : chunk)
)
}

Expand Down
38 changes: 38 additions & 0 deletions packages/effect/test/unstable/http/Template.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { assert, describe, it } from "@effect/vitest"
import { Deferred, Effect, Fiber, Stream } from "effect"
import { TestClock } from "effect/testing"
import { Template } from "effect/unstable/http"

describe("Template", () => {
it.effect("preserves template segment order", () =>
Effect.gen(function*() {
const fiber = yield* Stream.runCollect(
Template.stream`a${Effect.delay(Effect.succeed("slow"), "1 second")}b${"fast"}c`
).pipe(Effect.forkChild)
yield* Effect.yieldNow
yield* TestClock.adjust("1 second")
const chunks = yield* Fiber.join(fiber)
assert.strictEqual(chunks.join(""), "aslowbfastc")
}))

it.effect("evaluates effect interpolations concurrently", () =>
Effect.gen(function*() {
const firstStarted = yield* Deferred.make<void>()
const secondStarted = yield* Deferred.make<void>()
const releaseFirst = yield* Deferred.make<void>()
const fiber = yield* Template.stream`${
Deferred.succeed(firstStarted, void 0).pipe(
Effect.andThen(Deferred.await(releaseFirst)),
Effect.as("first")
)
}${Deferred.succeed(secondStarted, void 0).pipe(Effect.as("second"))}`.pipe(
Stream.runCollect,
Effect.forkChild
)
yield* Deferred.await(firstStarted)
yield* Effect.yieldNow
assert.isTrue(yield* Deferred.isDone(secondStarted))
yield* Deferred.succeed(releaseFirst, void 0)
assert.deepStrictEqual(yield* Fiber.join(fiber), ["first", "second"])
}))
})
Loading