From 78e91fdfa6ca9982c99095361a76bf42eaaef885 Mon Sep 17 00:00:00 2001 From: Effect Bot Date: Tue, 1 Dec 2026 11:36:07 +0000 Subject: [PATCH] Memoize startup context update in Channel.updateContext and consolidate test --- packages/effect/src/Channel.ts | 13 +++--- packages/effect/test/Channel.test.ts | 59 +++++++++++----------------- 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/packages/effect/src/Channel.ts b/packages/effect/src/Channel.ts index 3f13326d74..5622411333 100644 --- a/packages/effect/src/Channel.ts +++ b/packages/effect/src/Channel.ts @@ -7981,13 +7981,14 @@ export const updateContext: { f: (context: Context.Context) => Context.Context ): Channel => fromTransform((upstream, scope) => - Effect.contextWith((context: Context.Context) => - 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) => { + const updated = f(context) + const update = (current: Context.Context) => current === context ? updated : f(current) + return Effect.map( + Effect.updateContext(toTransform(self)(Effect.updateContext(upstream, constant(context)), scope), update), + Effect.updateContext(update) ) - ) + }) )) /** diff --git a/packages/effect/test/Channel.test.ts b/packages/effect/test/Channel.test.ts index b784f83bd1..198c8bd89c 100644 --- a/packages/effect/test/Channel.test.ts +++ b/packages/effect/test/Channel.test.ts @@ -924,49 +924,36 @@ describe("Channel", () => { })) }) - describe("context", () => { + describe("updateContext", () => { class A extends Context.Service()("A") {} class B extends Context.Service()("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) => - Channel.fromEffect(Effect.map(services, (pulled) => [ - [Context.getOption(context, A), Context.getOption(context, B)], - pulled - ])) - ).pipe( - Channel.updateContext((_: Context.Context) => Context.make(B, "b")), + const readServices = Effect.all([Effect.serviceOption(A), Effect.serviceOption(B)]) + const downstream = Channel.contextWith((startup: Context.Context) => + Channel.mapEffect( + Channel.identity(), + (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) => 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) => 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) => 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()]) })) })