Effect CommunityEC
Effect Community12mo ago
24 replies
Dave Meehan

Help with understanding Effect.Service dependencies

I'm struggling to understand dependencies in
Effect.Service
, which seem to require a different shape than when specified in a test. The test setup seems more logical, but in the Service dependencies I can't just list them as an array, but find in order for the DefaultPowerToolsLogger to apply to all of the other dependencies, I have to merge them all into a single layer.

export class DecoderService extends Effect.Service<DecoderService>()('DecoderService', {
  effect: Effect.gen(function* () {
    const config = yield* DecoderConfig
    const devices = yield* DeviceRepository
    const events = yield* PayloadPublishingService

    return {
      handler: Effect.succeed((unknown: unknown, _context: Context) => Effect.gen(function* () {
        // ...
      })),
    }
  }),
  dependencies: [
    DecoderConfig.Default.pipe(
      Layer.provideMerge(DeviceRepository.Default),
      Layer.provideMerge(PayloadPublishingService.Default),
      Layer.provideMerge(DefaultPowerToolsLoggerLayer),
    ),
  ],
  accessors: true,
}) {}


Test setup:

    const layer = DecoderService.DefaultWithoutDependencies.pipe(
      Layer.provide(DecoderConfig.Default),
      Layer.provide(DeviceRepository.Default),
      Layer.provide(PayloadPublishingService.Default),
      Layer.provide(Layer.setConfigProvider(configProvider)),
      Layer.provide(DefaultPowerToolsLoggerLayer),
    )


NB: I appreciate in the test I could use the Default 'with dependencies' layer as the only difference is the injection of the ConfigProvider but what I'm struggling with is why the Layer.provide's produce a different result to the list of dependencies. How does
Effect.Service
under the hood treat the dependencies? (I tried searching the code base but couldn't find the relevant module).
Was this page helpful?