Skip to content

Commit 4605db6

Browse files
authored
EFF-706 Refactor multiple ServiceMap mutations to ServiceMap.mutate (#1740)
1 parent 398ac3e commit 4605db6

File tree

6 files changed

+45
-32
lines changed

6 files changed

+45
-32
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"effect": patch
3+
"@effect/opentelemetry": patch
4+
---
5+
6+
Refactor call sites with multiple `ServiceMap` mutations to use `ServiceMap.mutate` for batched updates.

packages/effect/src/unstable/cluster/Entity.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,11 @@ export const makeTestClient: <Type extends string, Rpcs extends Rpc.Any, LA, LE,
524524
services: services as any,
525525
concurrency: options?.concurrency ?? 1,
526526
build: entity.protocol.toHandlers(handlers as any).pipe(
527-
Effect.provideServices(services.pipe(
528-
ServiceMap.add(CurrentRunnerAddress, runnerAddress),
529-
ServiceMap.omit(Scope)
530-
))
527+
Effect.provideServices(ServiceMap.mutate(services, (services) =>
528+
services.pipe(
529+
ServiceMap.add(CurrentRunnerAddress, runnerAddress),
530+
ServiceMap.omit(Scope)
531+
)))
531532
) as any
532533
})
533534
return Effect.void

packages/effect/src/unstable/cluster/Sharding.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,11 +1279,12 @@ const make = Effect.gen(function*() {
12791279
runnerAddress,
12801280
sharding
12811281
}).pipe(
1282-
Effect.provideServices(services.pipe(
1283-
ServiceMap.add(EntityReaper, reaper),
1284-
ServiceMap.add(Scope.Scope, scope),
1285-
ServiceMap.add(Snowflake.Generator, snowflakeGen)
1286-
))
1282+
Effect.provideServices(ServiceMap.mutate(services, (services) =>
1283+
services.pipe(
1284+
ServiceMap.add(EntityReaper, reaper),
1285+
ServiceMap.add(Scope.Scope, scope),
1286+
ServiceMap.add(Snowflake.Generator, snowflakeGen)
1287+
)))
12871288
) as Effect.Effect<EntityManager.EntityManager>
12881289
const state: EntityManagerState = {
12891290
entity,

packages/effect/src/unstable/cluster/internal/entityManager.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,13 @@ export const make = Effect.fnUntraced(function*<
155155
// Initiate the behavior for the entity
156156
const handlers = yield* (entity.protocol.toHandlers(buildHandlers as any).pipe(
157157
Effect.provideService(CurrentLogAnnotations, {}),
158-
Effect.provideServices(services.pipe(
159-
ServiceMap.add(CurrentAddress, address),
160-
ServiceMap.add(CurrentRunnerAddress, options.runnerAddress),
161-
ServiceMap.add(KeepAliveLatch, keepAliveLatch),
162-
ServiceMap.add(Scope.Scope, scope)
163-
))
158+
Effect.provideServices(ServiceMap.mutate(services, (services) =>
159+
services.pipe(
160+
ServiceMap.add(CurrentAddress, address),
161+
ServiceMap.add(CurrentRunnerAddress, options.runnerAddress),
162+
ServiceMap.add(KeepAliveLatch, keepAliveLatch),
163+
ServiceMap.add(Scope.Scope, scope)
164+
)))
164165
) as Effect.Effect<ServiceMap.ServiceMap<Rpc.ToHandler<Rpcs>>>)
165166

166167
const server = yield* RpcServer.makeNoSerialization(entity.protocol, {

packages/effect/src/unstable/sql/SqlClient.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,11 @@ export const makeWithTransaction = <I, S>(options: {
215215
Effect.flatMap(() =>
216216
Effect.provideServices(
217217
restore(effect),
218-
ServiceMap.add(services, options.transactionService, [conn, id]).pipe(
219-
ServiceMap.add(Tracer.ParentSpan, span)
220-
)
218+
ServiceMap.mutate(services, (services) =>
219+
services.pipe(
220+
ServiceMap.add(options.transactionService, [conn, id]),
221+
ServiceMap.add(Tracer.ParentSpan, span)
222+
))
221223
)
222224
),
223225
Effect.exit,

packages/opentelemetry/src/Tracer.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,24 @@ export const makeExternalSpan = (options: {
101101
readonly traceFlags?: number | undefined
102102
readonly traceState?: string | Otel.TraceState | undefined
103103
}): Tracer.ExternalSpan => {
104-
let annotations = ServiceMap.empty()
105-
106-
if (options.traceFlags !== undefined) {
107-
annotations = ServiceMap.add(annotations, OtelTraceFlags, options.traceFlags)
108-
}
104+
const annotations = ServiceMap.mutate(ServiceMap.empty(), (annotations) => {
105+
let next = annotations
106+
if (options.traceFlags !== undefined) {
107+
next = ServiceMap.add(next, OtelTraceFlags, options.traceFlags)
108+
}
109109

110-
if (typeof options.traceState === "string") {
111-
try {
112-
const traceState = Otel.createTraceState(options.traceState)
113-
annotations = ServiceMap.add(annotations, OtelTraceState, traceState)
114-
} catch {
115-
//
110+
if (typeof options.traceState === "string") {
111+
try {
112+
next = ServiceMap.add(next, OtelTraceState, Otel.createTraceState(options.traceState))
113+
} catch {
114+
//
115+
}
116+
} else if (options.traceState) {
117+
next = ServiceMap.add(next, OtelTraceState, options.traceState)
116118
}
117-
} else if (options.traceState) {
118-
annotations = ServiceMap.add(annotations, OtelTraceState, options.traceState)
119-
}
119+
120+
return next
121+
})
120122

121123
return {
122124
_tag: "ExternalSpan",

0 commit comments

Comments
 (0)