Skip to content

Commit

Permalink
Support providing an array of layers via Effect.provide
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi committed Sep 26, 2024
1 parent 32ab6c7 commit b95558e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-ways-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Support providing an array of layers via Effect.provide
21 changes: 20 additions & 1 deletion packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3325,11 +3325,30 @@ export const mapInputContext: {
* @category context
*/
export const provide: {
<const Layers extends [Layer.Layer<any, any, any>, ...Array<Layer.Layer<any, any, any>>]>(
layers: Layers
): <A, E, R>(
self: Effect<A, E, R>
) => Effect<
A,
E | { [k in keyof Layers]: Layer.Layer.Error<Layers[k]> }[number],
| { [k in keyof Layers]: Layer.Layer.Context<Layers[k]> }[number]
| Exclude<R, { [k in keyof Layers]: Layer.Layer.Success<Layers[k]> }[number]>
>
<ROut, E2, RIn>(
layer: Layer.Layer<ROut, E2, RIn>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E2 | E, RIn | Exclude<R, ROut>>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E | E2, RIn | Exclude<R, ROut>>
<R2>(context: Context.Context<R2>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, R2>>
<R2>(runtime: Runtime.Runtime<R2>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, R2>>
<A, E, R, const Layers extends [Layer.Layer<any, any, any>, ...Array<Layer.Layer<any, any, any>>]>(
self: Effect<A, E, R>,
layers: Layers
): Effect<
A,
E | { [k in keyof Layers]: Layer.Layer.Error<Layers[k]> }[number],
| { [k in keyof Layers]: Layer.Layer.Context<Layers[k]> }[number]
| Exclude<R, { [k in keyof Layers]: Layer.Layer.Success<Layers[k]> }[number]>
>
<A, E, R, ROut, E2, RIn>(
self: Effect<A, E, R>,
layer: Layer.Layer<ROut, E2, RIn>
Expand Down
30 changes: 28 additions & 2 deletions packages/effect/src/internal/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,16 @@ const provideSomeRuntime = dual<
/** @internal */
export const effect_provide = dual<
{
<const Layers extends [Layer.Layer<any, any, any>, ...Array<Layer.Layer<any, any, any>>]>(
layers: Layers
): <A, E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<
A,
E | { [k in keyof Layers]: Layer.Layer.Error<Layers[k]> }[number],
| { [k in keyof Layers]: Layer.Layer.Context<Layers[k]> }[number]
| Exclude<R, { [k in keyof Layers]: Layer.Layer.Success<Layers[k]> }[number]>
>
<ROut, E2, RIn>(
layer: Layer.Layer<ROut, E2, RIn>
): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E | E2, RIn | Exclude<R, ROut>>
Expand All @@ -1302,6 +1312,15 @@ export const effect_provide = dual<
): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, R2>>
},
{
<A, E, R, const Layers extends [Layer.Layer<any, any, any>, ...Array<Layer.Layer<any, any, any>>]>(
self: Effect.Effect<A, E, R>,
layers: Layers
): Effect.Effect<
A,
E | { [k in keyof Layers]: Layer.Layer.Error<Layers[k]> }[number],
| { [k in keyof Layers]: Layer.Layer.Context<Layers[k]> }[number]
| Exclude<R, { [k in keyof Layers]: Layer.Layer.Success<Layers[k]> }[number]>
>
<A, E, R, ROut, E2, RIn>(
self: Effect.Effect<A, E, R>,
layer: Layer.Layer<ROut, E2, RIn>
Expand All @@ -1319,9 +1338,16 @@ export const effect_provide = dual<
2,
<A, E, R, ROut>(
self: Effect.Effect<A, E, R>,
source: Layer.Layer<ROut, any, any> | Context.Context<ROut> | Runtime.Runtime<ROut>
source:
| Layer.Layer<ROut, any, any>
| Context.Context<ROut>
| Runtime.Runtime<ROut>
| Array<Layer.Layer<any, any, any>>
): Effect.Effect<any, any, Exclude<R, ROut>> =>
isLayer(source)
Array.isArray(source)
// @ts-expect-error
? provideSomeLayer(self, mergeAll(...source)) :
isLayer(source)
? provideSomeLayer(self, source as Layer.Layer<ROut, any, any>)
: Context.isContext(source)
? core.provideSomeContext(self, source)
Expand Down
6 changes: 5 additions & 1 deletion packages/effect/test/Effect/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ describe("Effect", () => {
expect(_tag).toEqual("Logger")
yield* Logger.info("Ok")
expect(messages).toEqual(["[PRE][Ok][POST]"])
const { prefix } = yield* Prefix
expect(prefix).toEqual("PRE")
const { postfix } = yield* Postfix
expect(postfix).toEqual("POST")
}).pipe(
Effect.provide(Logger)
Effect.provide([Logger, Prefix, Postfix])
))
})

0 comments on commit b95558e

Please sign in to comment.