Lazily Creating a Service in TypeScript with Effect

hi,

how can I create a service "lazily", only if the effect where it is provided consumes it?

import { HttpServerRequest } from '@effect/platform';
import { Effect } from 'effect';

define let effect: Effect.Effect<string, never, HttpServerRequest.ParsedSearchParams>

const program = effect.pipe(
  Effect.provideServiceEffect(
    HttpServerRequest.ParsedSearchParams,
    // this should never run if the `effect` does nothing with ParsedSearchParams
    HttpServerRequest.HttpServerRequest.pipe(
      Effect.tap(() =>
        Effect.log(
          'this code should only run if the effect consumes HttpServerRequest.HttpServerRequest'
        )
      ),
      Effect.flatMap((request) =>
        Effect.orDie(HttpServerRequest.toURL(request))
      ),
      Effect.map((url) => HttpServerRequest.searchParamsFromURL(url))
    )
  ),
  Effect.provideService(
    HttpServerRequest.HttpServerRequest,
    HttpServerRequest.fromWeb(args.request)
  )
)
Was this page helpful?