Skip to content

Commit 989a4db

Browse files
committed
build the accessor inside acquisuon
1 parent 2beb24e commit 989a4db

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

packages/effect/src/LayerMap.ts

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface LayerMap<in K, in out I, out S, out E = never> {
3131
/**
3232
* The internal RcMap that stores the resources.
3333
*/
34-
readonly rcMap: RcMap.RcMap<K, Context.Context<I>, E>
34+
readonly rcMap: RcMap.RcMap<K, readonly [Context.Context<I>, S], E>
3535

3636
/**
3737
* Retrieves an instance of the resource associated with the key.
@@ -100,18 +100,23 @@ export interface LayerMap<in K, in out I, out S, out E = never> {
100100
* )
101101
* ```
102102
*/
103-
export const make: <I, S, K, L extends Layer.Layer<I, any, any>>(
104-
tag: Context.Tag<I, S> | Effect.Effect<S, never, I>,
103+
export const make: <I, S, K, L extends Layer.Layer<Exclude<I, Scope.Scope>, any, any>, E = never>(
104+
tagOrAccessor: Context.Tag<I, S> | Effect.Effect<S, E, I>,
105105
lookup: (key: K) => L,
106106
options?: {
107107
readonly idleTimeToLive?: DurationInput | undefined
108108
} | undefined
109109
) => Effect.Effect<
110-
LayerMap<K, I, S, L extends Layer.Layer<infer _A, infer _E, infer _R> ? _E : never>,
110+
LayerMap<
111+
K,
112+
Exclude<I, Scope.Scope>,
113+
S,
114+
E | (L extends Layer.Layer<infer _A, infer _E, infer _R> ? _E : never)
115+
>,
111116
never,
112117
Scope.Scope | (L extends Layer.Layer<infer _A, infer _E, infer _R> ? _R : never)
113-
> = Effect.fnUntraced(function*<I, S, K, L extends Layer.Layer<I, any, any>>(
114-
tag: Context.Tag<I, S> | Effect.Effect<S, never, I>,
118+
> = Effect.fnUntraced(function*<I, S, K, L extends Layer.Layer<Exclude<I, Scope.Scope>, any, any>, E = never>(
119+
tagOrAccessor: Context.Tag<I, S> | Effect.Effect<S, E, I>,
115120
lookup: (key: K) => L,
116121
options?: {
117122
readonly idleTimeToLive?: DurationInput | undefined
@@ -126,27 +131,25 @@ export const make: <I, S, K, L extends Layer.Layer<I, any, any>>(
126131
: yield* Layer.makeMemoMap
127132

128133
const rcMap = yield* RcMap.make({
129-
lookup: (key: K) =>
130-
Effect.scopeWith((scope) =>
131-
Layer.buildWithMemoMap(lookup(key), memoMap, scope) as Effect.Effect<
132-
Context.Context<I>
133-
>
134-
),
134+
lookup: Effect.fnUntraced(function*(key: K) {
135+
const scope = yield* Effect.scope
136+
const context = yield* (Layer.buildWithMemoMap(lookup(key), memoMap, scope) as Effect.Effect<
137+
Context.Context<Exclude<I, Scope.Scope>>
138+
>)
139+
const service = yield* (Effect.provide(tagOrAccessor, context) as Effect.Effect<S>)
140+
return [context, service] as const
141+
}),
135142
idleTimeToLive: options?.idleTimeToLive
136143
})
137144

138-
return identity<LayerMap<K, I, S, any>>({
145+
return identity<LayerMap<K, Exclude<I, Scope.Scope>, S, any>>({
139146
[TypeId]: TypeId,
140147
rcMap,
141-
get: (key) =>
142-
Effect.flatMap(
143-
RcMap.get(rcMap, key),
144-
(context) => Effect.provide(tag, context)
145-
),
148+
get: (key) => Effect.map(RcMap.get(rcMap, key), ([, service]) => service),
146149
provide: (key) => (effect) =>
147150
Effect.flatMap(
148151
RcMap.get(rcMap, key),
149-
(context) => Effect.provide(effect, context)
152+
([context]) => Effect.provide(effect, context)
150153
),
151154
invalidate: (key) => RcMap.invalidate(rcMap, key)
152155
})
@@ -156,14 +159,24 @@ export const make: <I, S, K, L extends Layer.Layer<I, any, any>>(
156159
* @since 3.13.0
157160
* @category Constructors
158161
*/
159-
export const fromRecord = <I, S, const Layers extends Record<string, Layer.Layer<I, any, any>>>(
160-
tag: Context.Tag<I, S> | Effect.Effect<S, never, I>,
162+
export const fromRecord = <
163+
I,
164+
S,
165+
const Layers extends Record<string, Layer.Layer<Exclude<I, Scope.Scope>, any, any>>,
166+
E = never
167+
>(
168+
tagOrAccessor: Context.Tag<I, S> | Effect.Effect<S, E, I>,
161169
layers: Layers,
162170
options?: {
163171
readonly idleTimeToLive?: DurationInput | undefined
164172
} | undefined
165173
): Effect.Effect<
166-
LayerMap<keyof Layers, I, S, Layers[keyof Layers] extends Layer.Layer<infer _A, infer _E, infer _R> ? _E : never>,
174+
LayerMap<
175+
keyof Layers,
176+
Exclude<I, Scope.Scope>,
177+
S,
178+
E | (Layers[keyof Layers] extends Layer.Layer<infer _A, infer _E, infer _R> ? _E : never)
179+
>,
167180
never,
168181
Scope.Scope | (Layers[keyof Layers] extends Layer.Layer<infer _A, infer _E, infer _R> ? _R : never)
169-
> => make(tag, (key: keyof Layers) => layers[key], options)
182+
> => make(tagOrAccessor, (key: keyof Layers) => layers[key], options)

0 commit comments

Comments
 (0)