Memoize startup context update in Channel.updateContext and consolidate test

This commit is contained in:
Effect Bot
2026-12-01 11:36:07 +00:00
parent 7869f6867c
commit 78e91fdfa6
2 changed files with 30 additions and 42 deletions
+7 -6
View File
@@ -7981,13 +7981,14 @@ export const updateContext: {
f: (context: Context.Context<R2>) => Context.Context<Env>
): Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, R2> =>
fromTransform((upstream, scope) =>
Effect.contextWith((context: Context.Context<R2>) =>
Effect.map(
// the upstream is not part of the channel: it is pulled in the context around it
Effect.updateContext(toTransform(self)(Effect.updateContext(upstream, () => context), scope), f),
Effect.updateContext(f)
Effect.contextWith((context: Context.Context<R2>) => {
const updated = f(context)
const update = (current: Context.Context<R2>) => current === context ? updated : f(current)
return Effect.map(
Effect.updateContext(toTransform(self)(Effect.updateContext(upstream, constant(context)), scope), update),
Effect.updateContext(update)
)
)
})
))
/**
+23 -36
View File
@@ -924,49 +924,36 @@ describe("Channel", () => {
}))
})
describe("context", () => {
describe("updateContext", () => {
class A extends Context.Service<A, string>()("A") {}
class B extends Context.Service<B, string>()("B") {}
it.effect("updateContext provides the context it returns, not more", () =>
it.effect("replaces context at startup and on pulls without affecting upstream or caller", () =>
Effect.gen(function*() {
const services = Effect.all([Effect.serviceOption(A), Effect.serviceOption(B)])
const result = yield* Channel.contextWith((context: Context.Context<never>) =>
Channel.fromEffect(Effect.map(services, (pulled) => [
[Context.getOption(context, A), Context.getOption(context, B)],
pulled
]))
).pipe(
Channel.updateContext((_: Context.Context<never>) => Context.make(B, "b")),
const readServices = Effect.all([Effect.serviceOption(A), Effect.serviceOption(B)])
const downstream = Channel.contextWith((startup: Context.Context<never>) =>
Channel.mapEffect(
Channel.identity<unknown, never, void>(),
(upstream) =>
Effect.map(readServices, (pull) => ({
startup: [Context.getOption(startup, A), Context.getOption(startup, B)],
upstream,
pull
}))
)
)
const [emitted, after] = yield* Channel.fromEffect(readServices).pipe(
Channel.pipeTo(Channel.updateContext(downstream, (_: Context.Context<never>) => Context.make(B, "b"))),
Channel.runCollect,
Effect.zip(readServices),
Effect.provideService(A, "a")
)
// A is seen neither when the channel starts nor when it is pulled
assert.deepStrictEqual(result, [[
[Option.none(), Option.some("b")],
[Option.none(), Option.some("b")]
]])
}))
it.effect("updateContext leaves the context of the upstream as it is", () =>
Effect.gen(function*() {
const result = yield* Channel.fromEffect(Effect.serviceOption(A)).pipe(
Channel.pipeTo(Channel.updateContext(Channel.identity(), (_: Context.Context<never>) => Context.empty())),
Channel.runCollect,
Effect.provideService(A, "a")
)
assert.deepStrictEqual(result, [Option.some("a")])
}))
it.effect("updateContext leaves the context of the caller as it is", () =>
Effect.gen(function*() {
const result = yield* Channel.fromEffect(Effect.serviceOption(A)).pipe(
Channel.updateContext((_: Context.Context<never>) => Context.empty()),
Channel.runCollect,
Effect.andThen(Effect.serviceOption(A)),
Effect.provideService(A, "a")
)
assert.deepStrictEqual(result, Option.some("a"))
assert.deepStrictEqual(emitted, [{
startup: [Option.none(), Option.some("b")],
upstream: [Option.some("a"), Option.none()],
pull: [Option.none(), Option.some("b")]
}])
assert.deepStrictEqual(after, [Option.some("a"), Option.none()])
}))
})