Sanity Check on Framework Integration for Request-Scoped Services

re: framework integration, a quick sanity check.
I have services defined, some of which require nothing request-scoped, some of which have optional request-scoped dependencies, and some of which require request information. I've defined the request information as its own service dependency.

Does this approach work the way I think it does?
// no request dependencies
const BaseLive = pipe(...baseServices);
// services with optional request dependencies
const OptionalRequestLive = pipe(...requestOptionalServices, Layer.provideMerge(BaseLive));
const RequestRequiredLive = pipe(RequestRequiredServiceLive, ..., Layer.provideMerge(RequestScopedLive);
type RequestRequiredSuccess = Layer.Layer<Layer.Layer.Success<typeof RequestRequiredLive>>;

export function createRequestServiceLayer(
  requestScopedValues?: Layer.Layer<RequestScopedValues>,
): typeof RequestScopedLive | RequestRequiredSuccess {
  return requestScopedValues
    ? pipe(RequestRequiredLive, Layer.provide(requestScopedValues))
    : RequestScopedLive;
}


I'm pretty sure based on my reading that this will create new instances of all of these services on every request (which works) - if there were a way to memoize the base services while reconstructing all the others, that would be ideal, but I can't figure out how to do that.
Was this page helpful?