Passing Layer Requirements in TypeScript Layer Implementations

Hello,

I have a question about passing a Layer requirement to a Layer implementation that is not defined by the Layer port.

Imagine a Layer such as:

interface FooLayer {
  readonly bar: () => void;
}
const FooLayer = Context.GenericTag<FooLayer>("FooLayer");


When providing an implementation of FooLayer, I would like to pass a requirement to the implementation. For example:

const FooLayerLive = Layer.effet(
  FooLayer,
  Effect.gen(function* () {
    // To be able to have my FooLayerLive know that it needs the BazzLayer
    const bazzLayer = yield* BazzLayer;

    return FooLayer.of({
      bar: () => Effect.gen(function* () {
        // I expect to be able to use BazzLayer here
        // However I don't want it to appear with the FooLayer.bar <R>
        // I 'd love BazzLayer to appear in FooLayerLive <R>
        const b = yield* someFunctionOutsideOfTheLayerImpl();
      }),
    });
  })
)

const someFunctionOutsideOfTheLayerImpl = () => Effect.gen(function* () {
  const bazzLayer = yield* BazzLayer;

  // ...
});


The only way i found to achieve what I want to do is to do:
const FooLayerLive = Layer.effet(
  FooLayer,
  Effect.gen(function* () {
    const bazzLayer = yield* BazzLayer;

    const deps = Context.empty().pipe(
      Context.add(BazzLayer, bazzLayer)
    )

    return FooLayer.of({
      bar: () => Effect.gen(function* () {
        // I expect to be able to use BazzLayer here
        // However I don't want it to appear with the FooLayer.bar <R>
        // I 'd love BazzLayer to appear in FooLayerLive <R>
        const b = yield* someFunctionOutsideOfTheLayerImpl();
        }).pipe(
          Effect.provie(deps)
        )
    });
  })
)

const someFunctionOutsideOfTheLayerImpl = () => Effect.gen(function* () {
  const bazzLayer = yield* BazzLayer;

  // ...
});


Which feels a bit cumbersome. So I wanted to know whether I missed something or not.

Basically I want to "forward" the requirement of Layer from one function to its caller without impacting it's signature.

I hope I was clear enough 😅

Any help appreciated.
Was this page helpful?