Akka Streams, how to 'mock' source based on configuration

39 Views Asked by At

I'm looking for suggestion for this scenario: I have multiple sources that I want to merge at some point and I would like to enable or disable them according to configuration. For instance, in the following example a tick source:

sources {
  "enableTickSource": false
}

I would like to be able to do something like this

if (config.enableTickSource) {
  Source.tick(..., T)
} else {
  Source.never[T]
}

The problem is that the tick source is of type Source[T, Cancellable] while the never source is of type Source[T, NotUsed], so I would have to add more logic afterwards while I would love these two sources to be transparently interchangeable.

Potential solutions I haven't fully explored:

  • Discard the Cancellable materialized value
  • Map the materialized value to something else, like
Source.never[T]
  .mapMaterializedValue(_ => fakeCancellable)
  • Using options, like returning Option[Source[..., Cancellable]] and return None in the second case (I feel this would make code much more complex later)

Is there a recipe or best practice for this? Thanks for any suggestions.

0

There are 0 best solutions below